diff --git a/src/core/IronPython.Modules/_collections.cs b/src/core/IronPython.Modules/_collections.cs index fb3113d35..8463c2878 100644 --- a/src/core/IronPython.Modules/_collections.cs +++ b/src/core/IronPython.Modules/_collections.cs @@ -56,24 +56,24 @@ public void __init__([ParamDictionary] IDictionary dict) { clear(); } - public void __init__(object iterable) { + public void __init__(CodeContext context, object iterable) { _maxLen = -1; clear(); - extend(iterable); + extend(context, iterable); } - public void __init__(object iterable, object maxlen) { + public void __init__(CodeContext context, object iterable, object maxlen) { _maxLen = VerifyMaxLenValue(maxlen); clear(); - extend(iterable); + extend(context, iterable); } - public void __init__(object iterable, [ParamDictionary] IDictionary dict) { + public void __init__(CodeContext context, object iterable, [ParamDictionary] IDictionary dict) { if (VerifyMaxLen(dict) < 0) { - __init__(iterable); + __init__(context, iterable); } else { - __init__(iterable, VerifyMaxLen(dict)); + __init__(context, iterable, VerifyMaxLen(dict)); } } @@ -187,7 +187,7 @@ public void clear() { public object copy(CodeContext context) => __copy__(context); - public void extend(object iterable) { + public void extend(CodeContext context, object iterable) { // d.extend(d) if (ReferenceEquals(iterable, this)) { WalkDeque(idx => { @@ -197,13 +197,13 @@ public void extend(object iterable) { return; } - IEnumerator e = PythonOps.GetEnumerator(iterable); + IEnumerator e = PythonOps.GetEnumerator(context, iterable); while (e.MoveNext()) { append(e.Current); } } - public void extendleft(object iterable) { + public void extendleft(CodeContext context, object iterable) { // d.extendleft(d) if (ReferenceEquals(iterable, this)) { WalkDeque(idx => { @@ -213,7 +213,7 @@ public void extendleft(object iterable) { return; } - IEnumerator e = PythonOps.GetEnumerator(iterable); + IEnumerator e = PythonOps.GetEnumerator(context, iterable); while (e.MoveNext()) { appendleft(e.Current); } @@ -511,7 +511,7 @@ public bool __contains__(CodeContext/*!*/ context, object key) { public object __copy__(CodeContext/*!*/ context) { if (GetType() == typeof(deque)) { deque res = new deque(_maxLen); - res.extend(((IEnumerable)this).GetEnumerator()); + res.extend(context, ((IEnumerable)this).GetEnumerator()); return res; } else { return PythonCalls.Call(context, DynamicHelpers.GetPythonType(this), ((IEnumerable)this).GetEnumerator()); @@ -564,8 +564,8 @@ public int __len__() { } [SpecialName] - public deque InPlaceAdd(object other) { - extend(other); + public deque InPlaceAdd(CodeContext context, object other) { + extend(context, other); return this; } @@ -582,18 +582,18 @@ public static deque Add(CodeContext context, [NotNone] deque x, object y) { public static deque Add(CodeContext context, [NotNone] deque x, [NotNone] deque y) { var d = (deque)__new__(context, DynamicHelpers.GetPythonType(x), null, null); if (x._maxLen > 0) { - d.__init__(x, x._maxLen); + d.__init__(context, x, x._maxLen); } else { - d.__init__(x); + d.__init__(context, x); } - d.extend(y); + d.extend(context, y); return d; } private static deque MultiplyWorker(deque self, int count) { var d = new deque(self._maxLen); if (count <= 0 || self._itemCnt == 0) return d; - d.extend(self); + d.extend(DefaultContext.Default, self); // TODO: context if (count == 1) return d; if (d._maxLen < 0 || d._itemCnt * count <= d._maxLen) { diff --git a/src/core/IronPython.Modules/_functools.cs b/src/core/IronPython.Modules/_functools.cs index 9ac3df34b..af381bb13 100644 --- a/src/core/IronPython.Modules/_functools.cs +++ b/src/core/IronPython.Modules/_functools.cs @@ -27,7 +27,7 @@ public static class FunctionTools { public const string __doc__ = "provides functionality for manipulating callable objects"; public static object? reduce(CodeContext/*!*/ context, SiteLocalStorage>> siteData, object? func, object? seq) { - IEnumerator i = PythonOps.GetEnumerator(seq); + IEnumerator i = PythonOps.GetEnumerator(context, seq); if (!i.MoveNext()) { throw PythonOps.TypeError("reduce() of empty sequence with no initial value"); } @@ -43,7 +43,7 @@ public static class FunctionTools { } public static object? reduce(CodeContext/*!*/ context, SiteLocalStorage>> siteData, object? func, object? seq, object? initializer) { - IEnumerator i = PythonOps.GetEnumerator(seq); + IEnumerator i = PythonOps.GetEnumerator(context, seq); EnsureReduceData(context, siteData); CallSite> site = siteData.Data; diff --git a/src/core/IronPython.Modules/_heapq.cs b/src/core/IronPython.Modules/_heapq.cs index afccbc42b..6db8b5810 100644 --- a/src/core/IronPython.Modules/_heapq.cs +++ b/src/core/IronPython.Modules/_heapq.cs @@ -87,7 +87,7 @@ public static PythonList nlargest(CodeContext/*!*/ context, int n, object? itera } PythonList ret = new PythonList(Math.Min(n, 4000)); // don't allocate anything too huge - IEnumerator en = PythonOps.GetEnumerator(iterable); + IEnumerator en = PythonOps.GetEnumerator(context, iterable); // populate list with first n items for (int i = 0; i < n; i++) { @@ -120,7 +120,7 @@ public static PythonList nsmallest(CodeContext/*!*/ context, int n, object? iter } PythonList ret = new PythonList(Math.Min(n, 4000)); // don't allocate anything too huge - IEnumerator en = PythonOps.GetEnumerator(iterable); + IEnumerator en = PythonOps.GetEnumerator(context, iterable); // populate list with first n items for (int i = 0; i < n; i++) { diff --git a/src/core/IronPython.Modules/_operator.cs b/src/core/IronPython.Modules/_operator.cs index 9152b1299..a28695b53 100644 --- a/src/core/IronPython.Modules/_operator.cs +++ b/src/core/IronPython.Modules/_operator.cs @@ -275,7 +275,7 @@ public static bool contains(CodeContext/*!*/ context, object? a, object? b) { } public static int countOf(CodeContext/*!*/ context, object? a, object? b) { - System.Collections.IEnumerator e = PythonOps.GetEnumerator(a); + System.Collections.IEnumerator e = PythonOps.GetEnumerator(context, a); int count = 0; while (e.MoveNext()) { if (PythonOps.IsOrEqualsRetBool(context, e.Current, b)) { @@ -294,7 +294,7 @@ public static object getitem(CodeContext/*!*/ context, object? a, object? b) { } public static int indexOf(CodeContext/*!*/ context, object? a, object? b) { - System.Collections.IEnumerator e = PythonOps.GetEnumerator(a); + System.Collections.IEnumerator e = PythonOps.GetEnumerator(context, a); int index = 0; while (e.MoveNext()) { if (PythonOps.IsOrEqualsRetBool(context, e.Current, b)) { diff --git a/src/core/IronPython.Modules/array.cs b/src/core/IronPython.Modules/array.cs index 216d89829..1102c5ed6 100644 --- a/src/core/IronPython.Modules/array.cs +++ b/src/core/IronPython.Modules/array.cs @@ -326,8 +326,8 @@ public void extend(object? iterable) { } } - public void fromlist([NotNone] PythonList iterable) { - IEnumerator ie = PythonOps.GetEnumerator(iterable); + public void fromlist(CodeContext context, [NotNone] PythonList iterable) { + IEnumerator ie = PythonOps.GetEnumerator(context, iterable); List items = new List(); while (ie.MoveNext()) { diff --git a/src/core/IronPython.Modules/select.cs b/src/core/IronPython.Modules/select.cs index f5f8c2ed3..ac764f9c0 100644 --- a/src/core/IronPython.Modules/select.cs +++ b/src/core/IronPython.Modules/select.cs @@ -90,7 +90,7 @@ private static void ProcessSocketSequence(CodeContext context, object sequence, socketToOriginal = new Dictionary(); socketList = new PythonList(); - IEnumerator cursor = PythonOps.GetEnumerator(sequence); + IEnumerator cursor = PythonOps.GetEnumerator(context, sequence); while (cursor.MoveNext()) { object original = cursor.Current; Socket socket = ObjectToSocket(context, original); diff --git a/src/core/IronPython/Modules/Builtin.cs b/src/core/IronPython/Modules/Builtin.cs index 250a5fcb3..db1000ea0 100644 --- a/src/core/IronPython/Modules/Builtin.cs +++ b/src/core/IronPython/Modules/Builtin.cs @@ -768,7 +768,7 @@ public static object locals(CodeContext/*!*/ context) { private static readonly object UndefinedKeywordArgument = new object(); public static object? max(CodeContext/*!*/ context, object? x) { - IEnumerator i = PythonOps.GetEnumerator(x); + IEnumerator i = PythonOps.GetEnumerator(context, x); if (!i.MoveNext()) throw PythonOps.ValueError("max() arg is an empty sequence"); object? ret = i.Current; @@ -804,7 +804,7 @@ public static object locals(CodeContext/*!*/ context) { } public static object? max(CodeContext/*!*/ context, object? x, [ParamDictionary] IDictionary dict) { - IEnumerator i = PythonOps.GetEnumerator(x); + IEnumerator i = PythonOps.GetEnumerator(context, x); var kwargTuple = GetMaxKwArg(dict, isDefaultAllowed: true); object? method = kwargTuple.Item1; @@ -881,7 +881,7 @@ public static object locals(CodeContext/*!*/ context) { } public static object? min(CodeContext/*!*/ context, object? x) { - IEnumerator i = PythonOps.GetEnumerator(x); + IEnumerator i = PythonOps.GetEnumerator(context, x); if (!i.MoveNext()) { throw PythonOps.ValueError("empty sequence"); } @@ -915,7 +915,7 @@ public static object locals(CodeContext/*!*/ context) { } public static object? min(CodeContext/*!*/ context, object? x, [ParamDictionary] IDictionary dict) { - IEnumerator i = PythonOps.GetEnumerator(x); + IEnumerator i = PythonOps.GetEnumerator(context, x); var kwargTuple = GetMinKwArg(dict, isDefaultAllowed: true); object? method = kwargTuple.Item1; object? def = kwargTuple.Item2; @@ -1371,7 +1371,7 @@ public static PythonList sorted(CodeContext/*!*/ context, object? iterable, [ParamDictionary] IDictionary kwArgs) { - IEnumerator iter = PythonOps.GetEnumerator(iterable); + IEnumerator iter = PythonOps.GetEnumerator(context, iterable); PythonList l = new PythonList(10); while (iter.MoveNext()) { l.AddNoLock(iter.Current); @@ -1395,7 +1395,7 @@ public static PythonList sorted(CodeContext/*!*/ context, } public static object? sum(CodeContext/*!*/ context, object? sequence, object? start) { - IEnumerator i = PythonOps.GetEnumerator(sequence); + IEnumerator i = PythonOps.GetEnumerator(context, sequence); ValidateSumStart(start); diff --git a/src/core/IronPython/Runtime/Binding/PythonBinaryOperationBinder.cs b/src/core/IronPython/Runtime/Binding/PythonBinaryOperationBinder.cs index bae68d0f9..6ff76474a 100644 --- a/src/core/IronPython/Runtime/Binding/PythonBinaryOperationBinder.cs +++ b/src/core/IronPython/Runtime/Binding/PythonBinaryOperationBinder.cs @@ -567,7 +567,7 @@ private object ListAdd(CallSite site, object self, object other) { private object ListAddAssign(CallSite site, object self, object other) { if (self != null && self.GetType() == typeof(PythonList) && other != null && other.GetType() == typeof(PythonList)) { - return ((PythonList)self).InPlaceAdd(other); + return ((PythonList)self).InPlaceAdd(DefaultContext.Default, other); } return ((CallSite>)site).Update(site, self, other); diff --git a/src/core/IronPython/Runtime/ByteArray.cs b/src/core/IronPython/Runtime/ByteArray.cs index ec83218b2..cde6ea61a 100644 --- a/src/core/IronPython/Runtime/ByteArray.cs +++ b/src/core/IronPython/Runtime/ByteArray.cs @@ -585,8 +585,8 @@ public bool isupper() { /// in the sequence seq. The separator between elements is the /// string providing this method /// - public ByteArray join(object? iterable) { - IEnumerator seq = PythonOps.GetEnumerator(iterable); + public ByteArray join(CodeContext context, object? iterable) { + IEnumerator seq = PythonOps.GetEnumerator(context, iterable); if (!seq.MoveNext()) { return new ByteArray(); } diff --git a/src/core/IronPython/Runtime/Bytes.cs b/src/core/IronPython/Runtime/Bytes.cs index 2518d86f9..cb89586a7 100644 --- a/src/core/IronPython/Runtime/Bytes.cs +++ b/src/core/IronPython/Runtime/Bytes.cs @@ -456,8 +456,8 @@ public int index(BigInteger @byte, object? start, object? end) /// in the sequence seq. The separator between elements is the /// string providing this method /// - public Bytes join(object? iterable) { - IEnumerator seq = PythonOps.GetEnumerator(iterable); + public Bytes join(CodeContext context, object? iterable) { + IEnumerator seq = PythonOps.GetEnumerator(context, iterable); if (!seq.MoveNext()) { return Empty; } diff --git a/src/core/IronPython/Runtime/DictionaryOps.cs b/src/core/IronPython/Runtime/DictionaryOps.cs index dec342ce0..6743aca33 100644 --- a/src/core/IronPython/Runtime/DictionaryOps.cs +++ b/src/core/IronPython/Runtime/DictionaryOps.cs @@ -138,17 +138,17 @@ private static void SlowUpdate(CodeContext/*!*/ context, PythonDictionary/*!*/ s } } else if (PythonOps.TryGetBoundAttr(other, "keys", out object keysFunc)) { // user defined dictionary - IEnumerator i = PythonOps.GetEnumerator(PythonCalls.Call(context, keysFunc)); + IEnumerator i = PythonOps.GetEnumerator(context, PythonCalls.Call(context, keysFunc)); while (i.MoveNext()) { self._storage.Add(ref self._storage, i.Current, PythonOps.GetIndex(context, other, i.Current)); } } else { // list of lists (key/value pairs), list of tuples, // tuple of tuples, etc... - IEnumerator i = PythonOps.GetEnumerator(other); + IEnumerator i = PythonOps.GetEnumerator(context, other); int index = 0; while (i.MoveNext()) { - if (!AddKeyValue(self, i.Current)) { + if (!AddKeyValue(context, self, i.Current)) { throw PythonOps.ValueError("dictionary update sequence element #{0} has bad length; 2 is required", index); } index++; @@ -196,8 +196,8 @@ internal static bool TryGetValueVirtual(CodeContext context, PythonDictionary se return false; } - internal static bool AddKeyValue(PythonDictionary self, object o) { - IEnumerator i = PythonOps.GetEnumerator(o); //c.GetEnumerator(); + internal static bool AddKeyValue(CodeContext context, PythonDictionary self, object o) { + IEnumerator i = PythonOps.GetEnumerator(context, o); //c.GetEnumerator(); if (i.MoveNext()) { object key = i.Current; if (i.MoveNext()) { diff --git a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs index a32f887de..c2e425015 100644 --- a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs +++ b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs @@ -775,8 +775,8 @@ internal static BaseException CreateBaseExceptionForRaise(CodeContext/*!*/ conte if (PythonOps.IsInstance(value, type)) { pyEx = value; - } else if (value is PythonTuple) { - pyEx = PythonOps.CallWithArgsTuple(type, [], value); + } else if (value is PythonTuple pt) { + pyEx = PythonOps.CallWithArgsTuple(type, [], pt); } else if (value != null) { pyEx = PythonCalls.Call(context, type, value); } else { diff --git a/src/core/IronPython/Runtime/Importer.cs b/src/core/IronPython/Runtime/Importer.cs index 24b90ccd6..2cbfa997b 100644 --- a/src/core/IronPython/Runtime/Importer.cs +++ b/src/core/IronPython/Runtime/Importer.cs @@ -588,7 +588,7 @@ private static bool TryGetNestedModule(CodeContext/*!*/ context, PythonModule/*! Assert.NotNull(context, scope, name); if (scope.__dict__.TryGetValue(name, out nested)) { if (nested is PythonModule pm) { - var fullPath = ".".join(SubArray(parts, current)); + var fullPath = string.Join(".", SubArray(parts, current)); // double check, some packages mess with package namespace // see cp35116 if (pm.GetName() == fullPath) { diff --git a/src/core/IronPython/Runtime/Operations/ArrayOps.cs b/src/core/IronPython/Runtime/Operations/ArrayOps.cs index afd40b1bc..33511c281 100644 --- a/src/core/IronPython/Runtime/Operations/ArrayOps.cs +++ b/src/core/IronPython/Runtime/Operations/ArrayOps.cs @@ -65,7 +65,7 @@ public static object __new__(CodeContext context, PythonType pythonType, object Array res = @base == 0 ? Array.CreateInstance(type, len) : Array.CreateInstance(type, [len], [@base]); - IEnumerator ie = PythonOps.GetEnumerator(items); + IEnumerator ie = PythonOps.GetEnumerator(context, items); int i = @base; while (ie.MoveNext()) { res.SetValue(Converter.Convert(ie.Current, type), i++); diff --git a/src/core/IronPython/Runtime/Operations/ByteOps.cs b/src/core/IronPython/Runtime/Operations/ByteOps.cs index e05d866b6..0689570e7 100644 --- a/src/core/IronPython/Runtime/Operations/ByteOps.cs +++ b/src/core/IronPython/Runtime/Operations/ByteOps.cs @@ -123,7 +123,7 @@ internal static IList GetBytes(object? value, bool useHint, CodeContext? c int len = 0; if (useHint) PythonOps.TryInvokeLengthHint(context ?? DefaultContext.Default, value, out len); List ret = new List(len); - IEnumerator ie = PythonOps.GetEnumerator(value); + IEnumerator ie = PythonOps.GetEnumerator(context ?? DefaultContext.Default, value); while (ie.MoveNext()) { ret.Add(GetByte(ie.Current)); } diff --git a/src/core/IronPython/Runtime/Operations/ObjectOps.cs b/src/core/IronPython/Runtime/Operations/ObjectOps.cs index bac14ec81..8c8ac2d2a 100644 --- a/src/core/IronPython/Runtime/Operations/ObjectOps.cs +++ b/src/core/IronPython/Runtime/Operations/ObjectOps.cs @@ -381,7 +381,7 @@ private static PythonTuple ReduceProtocol2(CodeContext/*!*/ context, object self object? listIterator = null; if (self is PythonList) { - listIterator = PythonOps.GetEnumerator(self); + listIterator = PythonOps.GetEnumerator(context, self); } object? dictIterator = null; diff --git a/src/core/IronPython/Runtime/Operations/PythonOps.cs b/src/core/IronPython/Runtime/Operations/PythonOps.cs index 0d45265a1..07ef3abed 100644 --- a/src/core/IronPython/Runtime/Operations/PythonOps.cs +++ b/src/core/IronPython/Runtime/Operations/PythonOps.cs @@ -438,7 +438,7 @@ internal static bool IsSubClass(CodeContext/*!*/ context, PythonType c, [NotNull IEnumerator ie = PythonOps.GetEnumerator(bases); while (ie.MoveNext()) { - if (!(ie.Current is PythonType baseType)) continue; + if (ie.Current is not PythonType baseType) continue; if (c.IsSubclassOf(baseType)) return true; } return false; @@ -1025,7 +1025,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence, } } - public static object? CallWithArgsTuple(object func, object?[] args, object argsTuple) { + public static object? CallWithArgsTuple(object func, object?[] args, IEnumerable argsTuple) { if (argsTuple is PythonTuple tp) { object?[] nargs = new object[args.Length + tp.__len__()]; for (int i = 0; i < args.Length; i++) nargs[i] = args[i]; @@ -1035,7 +1035,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence, PythonList allArgs = new PythonList(args.Length + 10); allArgs.AddRange(args); - IEnumerator e = PythonOps.GetEnumerator(argsTuple); + IEnumerator e = argsTuple.GetEnumerator(); while (e.MoveNext()) allArgs.AddNoLock(e.Current); return PythonCalls.Call(func, allArgs.GetObjectArray()); diff --git a/src/core/IronPython/Runtime/PythonDictionary.cs b/src/core/IronPython/Runtime/PythonDictionary.cs index 334f83058..bcf9a9256 100644 --- a/src/core/IronPython/Runtime/PythonDictionary.cs +++ b/src/core/IronPython/Runtime/PythonDictionary.cs @@ -385,7 +385,7 @@ private static object fromkeysAny(CodeContext/*!*/ context, PythonType cls, obje pyDict = new PythonDictionary(); } - IEnumerator i = PythonOps.GetEnumerator(o); + IEnumerator i = PythonOps.GetEnumerator(context, o); while (i.MoveNext()) { pyDict._storage.AddNoLock(ref pyDict._storage, i.Current, value); } @@ -399,14 +399,14 @@ private static object fromkeysAny(CodeContext/*!*/ context, PythonType cls, obje if (pyDict != null) { // then store all the keys with their associated value - IEnumerator i = PythonOps.GetEnumerator(o); + IEnumerator i = PythonOps.GetEnumerator(context, o); while (i.MoveNext()) { pyDict[i.Current] = value; } } else { // slow path, cls.__new__ returned a user defined dictionary instead of a PythonDictionary. PythonContext pc = context.LanguageContext; - IEnumerator i = PythonOps.GetEnumerator(o); + IEnumerator i = PythonOps.GetEnumerator(context, o); while (i.MoveNext()) { pc.SetIndex(dict, i.Current, value); } diff --git a/src/core/IronPython/Runtime/PythonList.cs b/src/core/IronPython/Runtime/PythonList.cs index f923f29c4..f8d1e9b8f 100644 --- a/src/core/IronPython/Runtime/PythonList.cs +++ b/src/core/IronPython/Runtime/PythonList.cs @@ -383,11 +383,11 @@ internal void AddRange(ICollection otherList) { } [SpecialName] - public virtual object InPlaceAdd(object? other) { + public virtual object InPlaceAdd(CodeContext context, object? other) { if (ReferenceEquals(this, other)) { InPlaceMultiply(2); } else { - IEnumerator e = PythonOps.GetEnumerator(other); + IEnumerator e = PythonOps.GetEnumerator(context, other); while (e.MoveNext()) { append(e.Current); }