- Notifications
You must be signed in to change notification settings - Fork749
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -230,5 +230,7 @@ public string ToString(string format, IFormatProvider formatProvider) | ||
using var _ = Py.GIL(); | ||
return ToBigInteger().ToString(format, formatProvider); | ||
} | ||
public override TypeCode GetTypeCode() => TypeCode.Int64; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm not sure I follow. switch(convertible.GetTypeCode()){caseTypeCode.Int64:returnConvert.ToInt64(convertible);} It doesn't mean that the objectis a Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. You seem to be correct on that. Still not sure | ||
} | ||
} |
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(); | ||
} | ||
} | ||
} |