Skip to content

Commit 2ea430f

Browse files
authored
Re-enable some tests (#1453)
* Re-enable test_range * Re-enable test_richcmp * Re-enable test_mmap * Add issue numbers * Fix failing test * Re-enable test_list * Re-enable ctypes.test_frombuffer * Re-enable ctypes.test_cast * Disable ctypes.test_cast on Linux * Re-enable test_deque * Disable on test_deque Mono * Re-enable test_bytes * Re-enable test_long_stdlib * Re-enable test_complex_stdlib * Re-enable test_unicode_stdlib * Re-enable test_unpack * Fix test_tuple
1 parent 537ac73 commit 2ea430f

27 files changed

Lines changed: 169 additions & 171 deletions

Src/IronPython.Modules/_collections.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -551,20 +551,13 @@ public void __delitem__(CodeContext/*!*/ context, object index) {
551551
}
552552
}
553553

554-
public PythonTuple __reduce__() {
555-
lock (_lockObj) {
556-
object[] items = new object[_itemCnt];
557-
int curItem = 0;
558-
WalkDeque(delegate(int curIndex) {
559-
items[curItem++] = _data[curIndex];
560-
return true;
561-
});
562-
563-
return PythonTuple.MakeTuple(
564-
DynamicHelpers.GetPythonType(this),
565-
PythonTuple.MakeTuple(PythonList.FromArrayNoCopy(items))
566-
);
567-
}
554+
public PythonTuple __reduce__(CodeContext context) {
555+
return PythonTuple.MakeTuple(
556+
DynamicHelpers.GetPythonType(this),
557+
_maxLen == -1 ? PythonTuple.EMPTY : PythonTuple.MakeTuple(PythonTuple.EMPTY, maxlen),
558+
GetType() == typeof(deque) ? null : PythonOps.GetBoundAttr(context, this, "__dict__"),
559+
PythonOps.GetEnumeratorObject(context, this)
560+
);
568561
}
569562

570563
public int __len__() {

Src/IronPython.Modules/_ctypes/CData.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ internal void SetAddress(IntPtr address) {
7272

7373
internal void InitializeFromBuffer(object? data, int offset, int size) {
7474
var bp = data as IBufferProtocol
75-
?? throw PythonOps.TypeErrorForBadInstance("{0} object does not have the buffer interface", data);
76-
// Python 3.5: PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
75+
?? throw PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
7776

7877
IPythonBuffer buffer = bp.GetBuffer(BufferFlags.FullRO);
7978
if (buffer.IsReadOnly) {
@@ -97,8 +96,7 @@ internal void InitializeFromBuffer(object? data, int offset, int size) {
9796

9897
internal void InitializeFromBufferCopy(object? data, int offset, int size) {
9998
var bp = data as IBufferProtocol
100-
?? throw PythonOps.TypeErrorForBadInstance("{0} object does not have the buffer interface", data);
101-
// Python 3.5: PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
99+
?? throw PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
102100

103101
using IPythonBuffer buffer = bp.GetBuffer();
104102
var span = buffer.AsReadOnlySpan();

Src/IronPython.Modules/_ctypes/CFuncPtrType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ internal static PythonType MakeSystemType(Type underlyingSystemType) {
6969
return PythonType.SetPythonType(underlyingSystemType, new CFuncPtrType(underlyingSystemType));
7070
}
7171

72+
public object from_buffer(object obj)
73+
=> throw PythonOps.TypeError("abstract class");
74+
75+
public object from_buffer_copy(object obj)
76+
=> throw PythonOps.TypeError("abstract class");
77+
7278
/// <summary>
7379
/// Converts an object into a function call parameter.
7480
/// </summary>

Src/IronPython.Modules/_ctypes/PointerType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ private PointerType(Type underlyingSystemType)
4747
: base(underlyingSystemType) {
4848
}
4949

50+
public object from_buffer(object obj)
51+
=> throw PythonOps.TypeError("abstract class");
52+
53+
public object from_buffer_copy(object obj)
54+
=> throw PythonOps.TypeError("abstract class");
55+
5056
public object from_param([NotNone] CData obj) {
5157
return new NativeArgument((CData)PythonCalls.Call(this, obj), "P");
5258
}

Src/IronPython.Modules/_ctypes/_ctypes.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,21 @@ private static IntPtr Cast(IntPtr data, IntPtr obj, IntPtr type) {
101101
GCHandle typeHandle = GCHandle.FromIntPtr(type);
102102
try {
103103
CData cdata = objHandle.Target as CData;
104-
PythonType pt = (PythonType)typeHandle.Target;
104+
PythonType pt = typeHandle.Target as PythonType;
105105

106-
CData res = (CData)pt.CreateInstance(pt.Context.SharedContext);
107-
if (IsPointer(pt)) {
108-
res.MemHolder = new MemoryHolder(IntPtr.Size);
109-
if (IsPointer(DynamicHelpers.GetPythonType(cdata))) {
110-
res.MemHolder.WriteIntPtr(0, cdata.MemHolder.ReadIntPtr(0));
111-
} else {
112-
res.MemHolder.WriteIntPtr(0, data);
113-
}
106+
if (!IsPointer(pt)) throw PythonOps.TypeError("cast() argument 2 must be a pointer type, not {0}", PythonOps.GetPythonTypeName(typeHandle.Target));
114107

115-
if (cdata != null) {
116-
res.MemHolder.Objects = cdata.MemHolder.Objects;
117-
res.MemHolder.AddObject(IdDispenser.GetId(cdata), cdata);
118-
}
108+
CData res = (CData)pt.CreateInstance(pt.Context.SharedContext);
109+
res.MemHolder = new MemoryHolder(IntPtr.Size);
110+
if (IsPointer(DynamicHelpers.GetPythonType(cdata))) {
111+
res.MemHolder.WriteIntPtr(0, cdata.MemHolder.ReadIntPtr(0));
119112
} else {
120-
if (cdata != null) {
121-
res.MemHolder = new MemoryHolder(data, ((INativeType)pt).Size, cdata.MemHolder);
122-
} else {
123-
res.MemHolder = new MemoryHolder(data, ((INativeType)pt).Size);
124-
}
113+
res.MemHolder.WriteIntPtr(0, data);
114+
}
115+
116+
if (cdata != null) {
117+
res.MemHolder.Objects = cdata.MemHolder.Objects;
118+
res.MemHolder.AddObject(IdDispenser.GetId(cdata), cdata);
125119
}
126120

127121
return GCHandle.ToIntPtr(GCHandle.Alloc(res));

Src/IronPython.Modules/mmap.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -727,20 +727,23 @@ public object tell() {
727727
}
728728
}
729729

730-
public void write([BytesLike] IList<byte> s) {
730+
public int write([NotNone] IBufferProtocol s) {
731+
using var buffer = s.GetBuffer();
731732
using (new MmapLocker(this)) {
732733
EnsureWritable();
733734

734735
long pos = Position;
735736

736-
if (_view.Capacity - pos < s.Count) {
737+
if (_view.Capacity - pos < buffer.AsReadOnlySpan().Length) {
737738
throw PythonOps.ValueError("data out of range");
738739
}
739740

740-
byte[] data = s as byte[] ?? (s is Bytes b ? b.UnsafeByteArray : s.ToArray());
741-
_view.WriteArray(pos, data, 0, s.Count);
741+
byte[] data = buffer.AsUnsafeArray() ?? buffer.ToArray();
742+
_view.WriteArray(pos, data, 0, data.Length);
742743

743-
Position = pos + s.Count;
744+
Position = pos + data.Length;
745+
746+
return data.Length;
744747
}
745748
}
746749

Src/IronPython/Compiler/Ast/TupleExpression.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,10 @@ public TupleExpression(bool expandable, params Expression[] items)
1717
}
1818

1919
internal override string? CheckAssign() {
20-
if (Items.Count == 0) {
21-
// TODO: remove this when we get to 3.6
22-
return "can't assign to ()";
23-
}
24-
2520
return base.CheckAssign();
2621
}
2722

2823
internal override string? CheckDelete() {
29-
if (Items.Count == 0)
30-
return "can't delete ()"; // TODO: remove this when we get to 3.6
3124
return base.CheckDelete();
3225
}
3326

Src/IronPython/Modules/Builtin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static string bin(object? number) {
135135

136136
public static PythonType bytes => DynamicHelpers.GetPythonTypeFromType(typeof(Bytes));
137137

138-
public static PythonType bytearray => DynamicHelpers.GetPythonTypeFromType(typeof(ByteArray));
138+
public static PythonType bytearray => TypeCache.ByteArray;
139139

140140
[Documentation("callable(object) -> bool\n\nReturn whether the object is callable (i.e., some kind of function).")]
141141
public static bool callable(CodeContext/*!*/ context, object? o) {

Src/IronPython/Runtime/ByteArray.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
using System.Collections.Generic;
1010
using System.Numerics;
1111
using System.Runtime.CompilerServices;
12-
using System.Runtime.InteropServices;
1312
using System.Text;
1413

15-
using Microsoft.Scripting.Runtime;
16-
using Microsoft.Scripting.Utils;
17-
1814
using IronPython.Runtime.Exceptions;
1915
using IronPython.Runtime.Operations;
2016
using IronPython.Runtime.Types;
2117

18+
using Microsoft.Scripting;
19+
using Microsoft.Scripting.Runtime;
20+
using Microsoft.Scripting.Utils;
21+
2222
namespace IronPython.Runtime {
2323
/// <summary>
2424
/// bytearray(string, encoding[, errors]) -> bytearray
@@ -56,6 +56,12 @@ internal ByteArray(IEnumerable<byte> bytes) {
5656
_bytes = new ArrayData<byte>(bytes);
5757
}
5858

59+
[StaticExtensionMethod]
60+
public static object __new__(CodeContext context, [NotNone] PythonType cls, [ParamDictionary, NotNone] IDictionary<object, object> dict, [NotNone] params object[] args) {
61+
if (cls == TypeCache.ByteArray) return new ByteArray();
62+
return cls.CreateInstance(context);
63+
}
64+
5965
public void __init__() {
6066
lock (this) {
6167
_bytes.Clear();
@@ -74,7 +80,7 @@ public void __init__(int source) {
7480
}
7581

7682
public void __init__([NotNone] IBufferProtocol source) {
77-
if (Converter.TryConvertToIndex(source, out int size, throwNonInt: false)) {
83+
if (Converter.TryConvertToIndex(source, out int size, throwTypeError: false)) {
7884
__init__(size);
7985
} else {
8086
lock (this) {
@@ -86,7 +92,7 @@ public void __init__([NotNone] IBufferProtocol source) {
8692
}
8793

8894
public void __init__(CodeContext context, object? source) {
89-
if (Converter.TryConvertToIndex(source, out int size, throwNonInt: false)) {
95+
if (Converter.TryConvertToIndex(source, out int size, throwTypeError: false)) {
9096
__init__(size);
9197
} else if (source is IEnumerable<byte> en) {
9298
lock (this) {
@@ -466,8 +472,13 @@ public int find(BigInteger @byte, object? start, object? end) {
466472
}
467473
}
468474

469-
public static ByteArray fromhex([NotNone] string @string) {
470-
return new ByteArray(IListOfByteOps.FromHex(@string));
475+
[ClassMethod]
476+
public static object fromhex(CodeContext context, [NotNone] PythonType cls, [NotNone] string @string) {
477+
var hex = IListOfByteOps.FromHex(@string);
478+
if (cls == TypeCache.ByteArray) {
479+
return new ByteArray(hex);
480+
}
481+
return PythonTypeOps.CallParams(context, cls, new Bytes(hex));
471482
}
472483

473484
public string hex() => Bytes.ToHex(_bytes.AsByteSpan()); // new in CPython 3.5

Src/IronPython/Runtime/Bytes.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static object __new__(CodeContext context, [NotNone] PythonType cls, [Not
6161
return source;
6262
} else if (TryInvokeBytesOperator(context, source, out Bytes? res)) {
6363
return res;
64-
} else if (Converter.TryConvertToIndex(source, out int size, throwNonInt: false)) {
64+
} else if (Converter.TryConvertToIndex(source, out int size, throwTypeError: false)) {
6565
if (size < 0) throw PythonOps.ValueError("negative count");
6666
return new Bytes(new byte[size]);
6767
} else {
@@ -79,7 +79,7 @@ public static object __new__(CodeContext context, [NotNone] PythonType cls, obje
7979
return @object;
8080
} else if (TryInvokeBytesOperator(context, @object, out Bytes? res)) {
8181
return res;
82-
} else if (Converter.TryConvertToIndex(@object, out int size, throwNonInt: false)) {
82+
} else if (Converter.TryConvertToIndex(@object, out int size, throwTypeError: false)) {
8383
if (size < 0) throw PythonOps.ValueError("negative count");
8484
return new Bytes(new byte[size]);
8585
} else {
@@ -367,8 +367,13 @@ public int find(BigInteger @byte, object? start, object? end) {
367367
}
368368

369369
[ClassMethod]
370-
public static object fromhex(CodeContext context, [NotNone] PythonType cls, [NotNone] string @string)
371-
=> __new__(context, cls, IListOfByteOps.FromHex(@string));
370+
public static object fromhex(CodeContext context, [NotNone] PythonType cls, [NotNone] string @string) {
371+
var hex = IListOfByteOps.FromHex(@string);
372+
if (cls == TypeCache.Bytes) {
373+
return new Bytes(hex);
374+
}
375+
return PythonTypeOps.CallParams(context, cls, new Bytes(hex));
376+
}
372377

373378
public string hex() => ToHex(_bytes.AsSpan()); // new in CPython 3.5
374379

@@ -1181,6 +1186,13 @@ public bool __eq__(CodeContext context, [NotNone] string value) {
11811186

11821187
public bool __eq__(CodeContext context, [NotNone] Extensible<string> value) => __eq__(context, value.Value);
11831188

1189+
public bool __eq__(CodeContext context, [NotNone] int value) {
1190+
if (context.LanguageContext.PythonOptions.BytesWarning != Microsoft.Scripting.Severity.Ignore) {
1191+
PythonOps.Warn(context, PythonExceptions.BytesWarning, "Comparison between bytes and int");
1192+
}
1193+
return false;
1194+
}
1195+
11841196
[return: MaybeNotImplemented]
11851197
public NotImplementedType __eq__(CodeContext context, object? value) => NotImplementedType.Value;
11861198

@@ -1190,6 +1202,8 @@ public bool __eq__(CodeContext context, [NotNone] string value) {
11901202

11911203
public bool __ne__(CodeContext context, [NotNone] Extensible<string> value) => !__eq__(context, value);
11921204

1205+
public bool __ne__(CodeContext context, [NotNone] int value) => !__eq__(context, value);
1206+
11931207
[return: MaybeNotImplemented]
11941208
public NotImplementedType __ne__(CodeContext context, object? value) => NotImplementedType.Value;
11951209

0 commit comments

Comments
 (0)