Skip to content

Commit 9322b72

Browse files
authored
Enable test_class (#895)
* Enable test_class * Use assertRaises * Fix issues * Fix test failure
1 parent 684be43 commit 9322b72

5 files changed

Lines changed: 278 additions & 748 deletions

File tree

Src/IronPython/Lib/iptest/type_util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ def remove_clr_specific_attrs(attr_list):
4646
array_object = System.Array[object]
4747
array_byte = System.Array[System.Byte]
4848

49-
# sample numberes?
50-
clr_signed_types = (System.SByte, System.Int16, System.Int32, System.Int64, System.Decimal, System.Single, System.Double)
49+
# sample numbers?
50+
clr_float_types = (System.Decimal, System.Single, System.Double)
51+
clr_int_signed_types = (System.SByte, System.Int16, System.Int32, System.Int64)
52+
clr_signed_types = clr_int_signed_types + clr_float_types
5153
clr_unsigned_types = (System.Byte, System.UInt16, System.UInt32, System.UInt64)
54+
clr_int_types = clr_int_signed_types + clr_unsigned_types
5255
clr_all_types = clr_signed_types + clr_unsigned_types
5356

5457
clr_all_plus1 = [t.Parse("1") for t in clr_all_types]

Src/IronPython/Runtime/Binding/PythonProtocol.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internal static DynamicMetaObject ConvertToBool(DynamicMetaObjectBinder/*!*/ con
8989
callAsInt = DynamicExpression.Dynamic(
9090
state.Convert(typeof(int), ConversionResultKind.ExplicitCast),
9191
typeof(int),
92-
call
92+
Ast.Call(typeof(PythonOps).GetMethod(nameof(PythonOps.Index)), call)
9393
);
9494
}
9595

@@ -104,7 +104,7 @@ internal static DynamicMetaObject ConvertToBool(DynamicMetaObjectBinder/*!*/ con
104104
Ast.Constant("__len__() should return >= 0"),
105105
Ast.NewArrayInit(typeof(object))
106106
)
107-
)
107+
)
108108
),
109109
Ast.NotEqual(res, Ast.Constant(0))
110110
);

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,9 @@ public static object Index(object? o) {
774774
throw TypeError("'{0}' object cannot be interpreted as an integer", PythonTypeOps.GetName(o));
775775
}
776776

777-
internal static bool TryToIndex(object? o, [NotNullWhen(true)]out object? index) {
777+
internal static bool TryToIndex(object? o, [NotNullWhen(true)] out object? index) {
778+
var context = DefaultContext.Default;
779+
778780
switch (o) {
779781
case int i:
780782
index = Int32Ops.__index__(i);
@@ -807,26 +809,42 @@ internal static bool TryToIndex(object? o, [NotNullWhen(true)]out object? index)
807809
break;
808810
}
809811

810-
if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, o, "__index__", out index)) {
811-
if (!(index is int) && !(index is BigInteger))
812-
throw TypeError("__index__ returned non-int (type {0})", PythonTypeOps.GetName(index));
813-
return true;
812+
if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__index__", out index)) {
813+
if (index is int || index is BigInteger)
814+
return true;
815+
if (index is Extensible<int> || index is Extensible<BigInteger>) {
816+
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.");
817+
return true;
818+
}
819+
throw TypeError("__index__ returned non-int (type {0})", PythonTypeOps.GetName(index));
814820
}
815821

816822
index = default;
817823
return false;
818824
}
819825

820-
private static bool ObjectToInt(object o, out int res, out BigInteger longRes) {
821-
if (o is BigInteger bi) {
822-
if (!bi.AsInt32(out res)) {
823-
longRes = bi;
824-
return false;
825-
}
826-
} else if (o is int i) {
827-
res = i;
828-
} else {
829-
res = Converter.ConvertToInt32(o);
826+
private static bool IndexObjectToInt(object o, out int res, out BigInteger longRes) {
827+
switch (o) {
828+
case int i:
829+
res = i;
830+
break;
831+
case Extensible<int> ei: // deprecated
832+
res = ei;
833+
break;
834+
case BigInteger bi:
835+
if (!bi.AsInt32(out res)) {
836+
longRes = bi;
837+
return false;
838+
}
839+
break;
840+
case Extensible<BigInteger> ebi: // deprecated
841+
if (!ebi.Value.AsInt32(out res)) {
842+
longRes = ebi;
843+
return false;
844+
}
845+
break;
846+
default:
847+
throw new InvalidOperationException();
830848
}
831849

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

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

851-
if (ObjectToInt(len, out res, out bigRes)) {
869+
var indexObj = Index(len);
870+
871+
if (IndexObjectToInt(indexObj, out res, out bigRes)) {
852872
if (res < 0) throw ValueError("__len__() should return >= 0");
853873
return true;
854874
} else {

Src/IronPythonTest/Cases/IronPythonCasesManifest.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ Timeout=120000 # 2 minute timeout
88
RunCondition=NOT $(IS_MONO)
99
Reason=Exception on adding DocTestSuite
1010

11-
[IronPython.test_class]
12-
Ignore=true
13-
1411
[IronPython.test_cliclass]
1512
IsolationLevel=PROCESS # TODO: figure out - wpf fails to load otherwise
1613

0 commit comments

Comments
 (0)