Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Src/IronPython/Lib/iptest/type_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ def remove_clr_specific_attrs(attr_list):
array_object = System.Array[object]
array_byte = System.Array[System.Byte]

# sample numberes?
clr_signed_types = (System.SByte, System.Int16, System.Int32, System.Int64, System.Decimal, System.Single, System.Double)
# sample numbers?
clr_float_types = (System.Decimal, System.Single, System.Double)
clr_int_signed_types = (System.SByte, System.Int16, System.Int32, System.Int64)
clr_signed_types = clr_int_signed_types + clr_float_types
clr_unsigned_types = (System.Byte, System.UInt16, System.UInt32, System.UInt64)
clr_int_types = clr_int_signed_types + clr_unsigned_types
clr_all_types = clr_signed_types + clr_unsigned_types

clr_all_plus1 = [t.Parse("1") for t in clr_all_types]
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython/Runtime/Binding/PythonProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal static DynamicMetaObject ConvertToBool(DynamicMetaObjectBinder/*!*/ con
callAsInt = DynamicExpression.Dynamic(
state.Convert(typeof(int), ConversionResultKind.ExplicitCast),
typeof(int),
call
Ast.Call(typeof(PythonOps).GetMethod(nameof(PythonOps.Index)), call)
);
}

Expand All @@ -104,7 +104,7 @@ internal static DynamicMetaObject ConvertToBool(DynamicMetaObjectBinder/*!*/ con
Ast.Constant("__len__() should return >= 0"),
Ast.NewArrayInit(typeof(object))
)
)
)
),
Ast.NotEqual(res, Ast.Constant(0))
);
Expand Down
52 changes: 36 additions & 16 deletions Src/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,9 @@ public static object Index(object? o) {
throw TypeError("'{0}' object cannot be interpreted as an integer", PythonTypeOps.GetName(o));
}

internal static bool TryToIndex(object? o, [NotNullWhen(true)]out object? index) {
internal static bool TryToIndex(object? o, [NotNullWhen(true)] out object? index) {
var context = DefaultContext.Default;

switch (o) {
case int i:
index = Int32Ops.__index__(i);
Expand Down Expand Up @@ -807,26 +809,42 @@ internal static bool TryToIndex(object? o, [NotNullWhen(true)]out object? index)
break;
}

if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, o, "__index__", out index)) {
if (!(index is int) && !(index is BigInteger))
throw TypeError("__index__ returned non-int (type {0})", PythonTypeOps.GetName(index));
return true;
if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__index__", out index)) {
if (index is int || index is BigInteger)
return true;
if (index is Extensible<int> || index is Extensible<BigInteger>) {
Warn(context, PythonExceptions.DeprecationWarning, $"__index__ returned non-int (type {PythonTypeOps.GetName(index)}). The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.");
return true;
}
throw TypeError("__index__ returned non-int (type {0})", PythonTypeOps.GetName(index));
}

index = default;
return false;
}

private static bool ObjectToInt(object o, out int res, out BigInteger longRes) {
if (o is BigInteger bi) {
if (!bi.AsInt32(out res)) {
longRes = bi;
return false;
}
} else if (o is int i) {
res = i;
} else {
res = Converter.ConvertToInt32(o);
private static bool IndexObjectToInt(object o, out int res, out BigInteger longRes) {
switch (o) {
case int i:
res = i;
break;
case Extensible<int> ei: // deprecated
res = ei;
break;
case BigInteger bi:
if (!bi.AsInt32(out res)) {
longRes = bi;
return false;
}
break;
case Extensible<BigInteger> ebi: // deprecated
if (!ebi.Value.AsInt32(out res)) {
longRes = ebi;
return false;
}
break;
default:
throw new InvalidOperationException();
}

longRes = default;
Expand All @@ -848,7 +866,9 @@ internal static bool Length(object? o, out int res, out BigInteger bigRes) {

object len = PythonContext.InvokeUnaryOperator(DefaultContext.Default, UnaryOperators.Length, o, $"object of type '{GetPythonTypeName(o)}' has no len()");

if (ObjectToInt(len, out res, out bigRes)) {
var indexObj = Index(len);

if (IndexObjectToInt(indexObj, out res, out bigRes)) {
if (res < 0) throw ValueError("__len__() should return >= 0");
return true;
} else {
Expand Down
3 changes: 0 additions & 3 deletions Src/IronPythonTest/Cases/IronPythonCasesManifest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ Timeout=120000 # 2 minute timeout
RunCondition=NOT $(IS_MONO)
Reason=Exception on adding DocTestSuite

[IronPython.test_class]
Ignore=true

[IronPython.test_cliclass]
IsolationLevel=PROCESS # TODO: figure out - wpf fails to load otherwise

Expand Down
Loading