-
Notifications
You must be signed in to change notification settings - Fork 748
Implement IConvertible on PyObject #1762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
|
||
namespace Python.Runtime; | ||
|
||
public partial class PyObject : IConvertible | ||
{ | ||
public virtual TypeCode GetTypeCode() => TypeCode.Object; | ||
|
||
private T DoConvert<T>() | ||
{ | ||
using var _ = Py.GIL(); | ||
if (Converter.ToPrimitive(Reference, typeof(T), out object? result, setError: false)) | ||
{ | ||
return (T)result!; | ||
} | ||
else | ||
{ | ||
throw new InvalidCastException(); | ||
} | ||
} | ||
|
||
public bool ToBoolean(IFormatProvider provider) => DoConvert<bool>(); | ||
public byte ToByte(IFormatProvider provider) => DoConvert<byte>(); | ||
public char ToChar(IFormatProvider provider) => DoConvert<char>(); | ||
public short ToInt16(IFormatProvider provider) => DoConvert<short>(); | ||
public int ToInt32(IFormatProvider provider) => DoConvert<int>(); | ||
public long ToInt64(IFormatProvider provider) => DoConvert<long>(); | ||
public sbyte ToSByte(IFormatProvider provider) => DoConvert<sbyte>(); | ||
public ushort ToUInt16(IFormatProvider provider) => DoConvert<ushort>(); | ||
public uint ToUInt32(IFormatProvider provider) => DoConvert<uint>(); | ||
public ulong ToUInt64(IFormatProvider provider) => DoConvert<ulong>(); | ||
|
||
public float ToSingle(IFormatProvider provider) => DoConvert<float>(); | ||
public double ToDouble(IFormatProvider provider) => DoConvert<double>(); | ||
|
||
public string ToString(IFormatProvider provider) => DoConvert<string>(); | ||
|
||
public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException(); | ||
public decimal ToDecimal(IFormatProvider provider) => throw new InvalidCastException(); | ||
|
||
public object ToType(Type conversionType, IFormatProvider provider) | ||
{ | ||
if (Converter.ToManaged(Reference, conversionType, out object? result, setError: false)) | ||
{ | ||
return result!; | ||
} | ||
else | ||
{ | ||
throw new InvalidCastException(); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, this is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs are a bit unclear on this function. I used this as an "should almost always work" value, the
InvalidCastException
might still be thrown if the conversion fails. We could drop theGetTypeCode
overrides entirely and just returnTypeCode.Object
if you'd prefer that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't say for others, just for this one. Honestly, not sure if anyone ever uses this. Our own code assumes
TypeCode.Int64
refers toSystem.Int64
, so removing might be a good idea.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow.
GetTypeCode
is supposed to be a hint that allows you to write aswitch
likeIt doesn't mean that the object is a
System.Int64
already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to be correct on that. Still not sure
Int64
is valid for this specific case, but float and string probably are. I am fine either way though. I don't think we will get people complaining about this returningInt64
any time soon.