Skip to content

Commit 71a8f5c

Browse files
authored
Use module context with iterables (#1949)
1 parent 74ce419 commit 71a8f5c

19 files changed

+57
-57
lines changed

src/core/IronPython.Modules/_collections.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ public void __init__([ParamDictionary] IDictionary<object, object> dict) {
5656
clear();
5757
}
5858

59-
public void __init__(object iterable) {
59+
public void __init__(CodeContext context, object iterable) {
6060
_maxLen = -1;
6161
clear();
62-
extend(iterable);
62+
extend(context, iterable);
6363
}
6464

65-
public void __init__(object iterable, object maxlen) {
65+
public void __init__(CodeContext context, object iterable, object maxlen) {
6666
_maxLen = VerifyMaxLenValue(maxlen);
6767

6868
clear();
69-
extend(iterable);
69+
extend(context, iterable);
7070
}
7171

72-
public void __init__(object iterable, [ParamDictionary] IDictionary<object, object> dict) {
72+
public void __init__(CodeContext context, object iterable, [ParamDictionary] IDictionary<object, object> dict) {
7373
if (VerifyMaxLen(dict) < 0) {
74-
__init__(iterable);
74+
__init__(context, iterable);
7575
} else {
76-
__init__(iterable, VerifyMaxLen(dict));
76+
__init__(context, iterable, VerifyMaxLen(dict));
7777
}
7878
}
7979

@@ -187,7 +187,7 @@ public void clear() {
187187
public object copy(CodeContext context)
188188
=> __copy__(context);
189189

190-
public void extend(object iterable) {
190+
public void extend(CodeContext context, object iterable) {
191191
// d.extend(d)
192192
if (ReferenceEquals(iterable, this)) {
193193
WalkDeque(idx => {
@@ -197,13 +197,13 @@ public void extend(object iterable) {
197197
return;
198198
}
199199

200-
IEnumerator e = PythonOps.GetEnumerator(iterable);
200+
IEnumerator e = PythonOps.GetEnumerator(context, iterable);
201201
while (e.MoveNext()) {
202202
append(e.Current);
203203
}
204204
}
205205

206-
public void extendleft(object iterable) {
206+
public void extendleft(CodeContext context, object iterable) {
207207
// d.extendleft(d)
208208
if (ReferenceEquals(iterable, this)) {
209209
WalkDeque(idx => {
@@ -213,7 +213,7 @@ public void extendleft(object iterable) {
213213
return;
214214
}
215215

216-
IEnumerator e = PythonOps.GetEnumerator(iterable);
216+
IEnumerator e = PythonOps.GetEnumerator(context, iterable);
217217
while (e.MoveNext()) {
218218
appendleft(e.Current);
219219
}
@@ -511,7 +511,7 @@ public bool __contains__(CodeContext/*!*/ context, object key) {
511511
public object __copy__(CodeContext/*!*/ context) {
512512
if (GetType() == typeof(deque)) {
513513
deque res = new deque(_maxLen);
514-
res.extend(((IEnumerable)this).GetEnumerator());
514+
res.extend(context, ((IEnumerable)this).GetEnumerator());
515515
return res;
516516
} else {
517517
return PythonCalls.Call(context, DynamicHelpers.GetPythonType(this), ((IEnumerable)this).GetEnumerator());
@@ -564,8 +564,8 @@ public int __len__() {
564564
}
565565

566566
[SpecialName]
567-
public deque InPlaceAdd(object other) {
568-
extend(other);
567+
public deque InPlaceAdd(CodeContext context, object other) {
568+
extend(context, other);
569569
return this;
570570
}
571571

@@ -582,18 +582,18 @@ public static deque Add(CodeContext context, [NotNone] deque x, object y) {
582582
public static deque Add(CodeContext context, [NotNone] deque x, [NotNone] deque y) {
583583
var d = (deque)__new__(context, DynamicHelpers.GetPythonType(x), null, null);
584584
if (x._maxLen > 0) {
585-
d.__init__(x, x._maxLen);
585+
d.__init__(context, x, x._maxLen);
586586
} else {
587-
d.__init__(x);
587+
d.__init__(context, x);
588588
}
589-
d.extend(y);
589+
d.extend(context, y);
590590
return d;
591591
}
592592

593593
private static deque MultiplyWorker(deque self, int count) {
594594
var d = new deque(self._maxLen);
595595
if (count <= 0 || self._itemCnt == 0) return d;
596-
d.extend(self);
596+
d.extend(DefaultContext.Default, self); // TODO: context
597597
if (count == 1) return d;
598598

599599
if (d._maxLen < 0 || d._itemCnt * count <= d._maxLen) {

src/core/IronPython.Modules/_functools.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class FunctionTools {
2727
public const string __doc__ = "provides functionality for manipulating callable objects";
2828

2929
public static object? reduce(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object?, object?, object?, object?>>> siteData, object? func, object? seq) {
30-
IEnumerator i = PythonOps.GetEnumerator(seq);
30+
IEnumerator i = PythonOps.GetEnumerator(context, seq);
3131
if (!i.MoveNext()) {
3232
throw PythonOps.TypeError("reduce() of empty sequence with no initial value");
3333
}
@@ -43,7 +43,7 @@ public static class FunctionTools {
4343
}
4444

4545
public static object? reduce(CodeContext/*!*/ context, SiteLocalStorage<CallSite<Func<CallSite, CodeContext, object?, object?, object?, object?>>> siteData, object? func, object? seq, object? initializer) {
46-
IEnumerator i = PythonOps.GetEnumerator(seq);
46+
IEnumerator i = PythonOps.GetEnumerator(context, seq);
4747
EnsureReduceData(context, siteData);
4848

4949
CallSite<Func<CallSite, CodeContext, object?, object?, object?, object?>> site = siteData.Data;

src/core/IronPython.Modules/_heapq.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static PythonList nlargest(CodeContext/*!*/ context, int n, object? itera
8787
}
8888

8989
PythonList ret = new PythonList(Math.Min(n, 4000)); // don't allocate anything too huge
90-
IEnumerator en = PythonOps.GetEnumerator(iterable);
90+
IEnumerator en = PythonOps.GetEnumerator(context, iterable);
9191

9292
// populate list with first n items
9393
for (int i = 0; i < n; i++) {
@@ -120,7 +120,7 @@ public static PythonList nsmallest(CodeContext/*!*/ context, int n, object? iter
120120
}
121121

122122
PythonList ret = new PythonList(Math.Min(n, 4000)); // don't allocate anything too huge
123-
IEnumerator en = PythonOps.GetEnumerator(iterable);
123+
IEnumerator en = PythonOps.GetEnumerator(context, iterable);
124124

125125
// populate list with first n items
126126
for (int i = 0; i < n; i++) {

src/core/IronPython.Modules/_operator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public static bool contains(CodeContext/*!*/ context, object? a, object? b) {
275275
}
276276

277277
public static int countOf(CodeContext/*!*/ context, object? a, object? b) {
278-
System.Collections.IEnumerator e = PythonOps.GetEnumerator(a);
278+
System.Collections.IEnumerator e = PythonOps.GetEnumerator(context, a);
279279
int count = 0;
280280
while (e.MoveNext()) {
281281
if (PythonOps.IsOrEqualsRetBool(context, e.Current, b)) {
@@ -294,7 +294,7 @@ public static object getitem(CodeContext/*!*/ context, object? a, object? b) {
294294
}
295295

296296
public static int indexOf(CodeContext/*!*/ context, object? a, object? b) {
297-
System.Collections.IEnumerator e = PythonOps.GetEnumerator(a);
297+
System.Collections.IEnumerator e = PythonOps.GetEnumerator(context, a);
298298
int index = 0;
299299
while (e.MoveNext()) {
300300
if (PythonOps.IsOrEqualsRetBool(context, e.Current, b)) {

src/core/IronPython.Modules/array.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ public void extend(object? iterable) {
326326
}
327327
}
328328

329-
public void fromlist([NotNone] PythonList iterable) {
330-
IEnumerator ie = PythonOps.GetEnumerator(iterable);
329+
public void fromlist(CodeContext context, [NotNone] PythonList iterable) {
330+
IEnumerator ie = PythonOps.GetEnumerator(context, iterable);
331331

332332
List<object> items = new List<object>();
333333
while (ie.MoveNext()) {

src/core/IronPython.Modules/select.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private static void ProcessSocketSequence(CodeContext context, object sequence,
9090
socketToOriginal = new Dictionary<Socket, object>();
9191
socketList = new PythonList();
9292

93-
IEnumerator cursor = PythonOps.GetEnumerator(sequence);
93+
IEnumerator cursor = PythonOps.GetEnumerator(context, sequence);
9494
while (cursor.MoveNext()) {
9595
object original = cursor.Current;
9696
Socket socket = ObjectToSocket(context, original);

src/core/IronPython/Modules/Builtin.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ public static object locals(CodeContext/*!*/ context) {
768768
private static readonly object UndefinedKeywordArgument = new object();
769769

770770
public static object? max(CodeContext/*!*/ context, object? x) {
771-
IEnumerator i = PythonOps.GetEnumerator(x);
771+
IEnumerator i = PythonOps.GetEnumerator(context, x);
772772
if (!i.MoveNext())
773773
throw PythonOps.ValueError("max() arg is an empty sequence");
774774
object? ret = i.Current;
@@ -804,7 +804,7 @@ public static object locals(CodeContext/*!*/ context) {
804804
}
805805

806806
public static object? max(CodeContext/*!*/ context, object? x, [ParamDictionary] IDictionary<string, object?> dict) {
807-
IEnumerator i = PythonOps.GetEnumerator(x);
807+
IEnumerator i = PythonOps.GetEnumerator(context, x);
808808

809809
var kwargTuple = GetMaxKwArg(dict, isDefaultAllowed: true);
810810
object? method = kwargTuple.Item1;
@@ -881,7 +881,7 @@ public static object locals(CodeContext/*!*/ context) {
881881
}
882882

883883
public static object? min(CodeContext/*!*/ context, object? x) {
884-
IEnumerator i = PythonOps.GetEnumerator(x);
884+
IEnumerator i = PythonOps.GetEnumerator(context, x);
885885
if (!i.MoveNext()) {
886886
throw PythonOps.ValueError("empty sequence");
887887
}
@@ -915,7 +915,7 @@ public static object locals(CodeContext/*!*/ context) {
915915
}
916916

917917
public static object? min(CodeContext/*!*/ context, object? x, [ParamDictionary] IDictionary<string, object?> dict) {
918-
IEnumerator i = PythonOps.GetEnumerator(x);
918+
IEnumerator i = PythonOps.GetEnumerator(context, x);
919919
var kwargTuple = GetMinKwArg(dict, isDefaultAllowed: true);
920920
object? method = kwargTuple.Item1;
921921
object? def = kwargTuple.Item2;
@@ -1371,7 +1371,7 @@ public static PythonList sorted(CodeContext/*!*/ context,
13711371
object? iterable,
13721372
[ParamDictionary] IDictionary<string, object> kwArgs) {
13731373

1374-
IEnumerator iter = PythonOps.GetEnumerator(iterable);
1374+
IEnumerator iter = PythonOps.GetEnumerator(context, iterable);
13751375
PythonList l = new PythonList(10);
13761376
while (iter.MoveNext()) {
13771377
l.AddNoLock(iter.Current);
@@ -1395,7 +1395,7 @@ public static PythonList sorted(CodeContext/*!*/ context,
13951395
}
13961396

13971397
public static object? sum(CodeContext/*!*/ context, object? sequence, object? start) {
1398-
IEnumerator i = PythonOps.GetEnumerator(sequence);
1398+
IEnumerator i = PythonOps.GetEnumerator(context, sequence);
13991399

14001400
ValidateSumStart(start);
14011401

src/core/IronPython/Runtime/Binding/PythonBinaryOperationBinder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ private object ListAdd(CallSite site, object self, object other) {
567567
private object ListAddAssign(CallSite site, object self, object other) {
568568
if (self != null && self.GetType() == typeof(PythonList) &&
569569
other != null && other.GetType() == typeof(PythonList)) {
570-
return ((PythonList)self).InPlaceAdd(other);
570+
return ((PythonList)self).InPlaceAdd(DefaultContext.Default, other);
571571
}
572572

573573
return ((CallSite<Func<CallSite, object, object, object>>)site).Update(site, self, other);

src/core/IronPython/Runtime/ByteArray.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ public bool isupper() {
585585
/// in the sequence seq. The separator between elements is the
586586
/// string providing this method
587587
/// </summary>
588-
public ByteArray join(object? iterable) {
589-
IEnumerator seq = PythonOps.GetEnumerator(iterable);
588+
public ByteArray join(CodeContext context, object? iterable) {
589+
IEnumerator seq = PythonOps.GetEnumerator(context, iterable);
590590
if (!seq.MoveNext()) {
591591
return new ByteArray();
592592
}

src/core/IronPython/Runtime/Bytes.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ public int index(BigInteger @byte, object? start, object? end)
456456
/// in the sequence seq. The separator between elements is the
457457
/// string providing this method
458458
/// </summary>
459-
public Bytes join(object? iterable) {
460-
IEnumerator seq = PythonOps.GetEnumerator(iterable);
459+
public Bytes join(CodeContext context, object? iterable) {
460+
IEnumerator seq = PythonOps.GetEnumerator(context, iterable);
461461
if (!seq.MoveNext()) {
462462
return Empty;
463463
}

src/core/IronPython/Runtime/DictionaryOps.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,17 @@ private static void SlowUpdate(CodeContext/*!*/ context, PythonDictionary/*!*/ s
138138
}
139139
} else if (PythonOps.TryGetBoundAttr(other, "keys", out object keysFunc)) {
140140
// user defined dictionary
141-
IEnumerator i = PythonOps.GetEnumerator(PythonCalls.Call(context, keysFunc));
141+
IEnumerator i = PythonOps.GetEnumerator(context, PythonCalls.Call(context, keysFunc));
142142
while (i.MoveNext()) {
143143
self._storage.Add(ref self._storage, i.Current, PythonOps.GetIndex(context, other, i.Current));
144144
}
145145
} else {
146146
// list of lists (key/value pairs), list of tuples,
147147
// tuple of tuples, etc...
148-
IEnumerator i = PythonOps.GetEnumerator(other);
148+
IEnumerator i = PythonOps.GetEnumerator(context, other);
149149
int index = 0;
150150
while (i.MoveNext()) {
151-
if (!AddKeyValue(self, i.Current)) {
151+
if (!AddKeyValue(context, self, i.Current)) {
152152
throw PythonOps.ValueError("dictionary update sequence element #{0} has bad length; 2 is required", index);
153153
}
154154
index++;
@@ -196,8 +196,8 @@ internal static bool TryGetValueVirtual(CodeContext context, PythonDictionary se
196196
return false;
197197
}
198198

199-
internal static bool AddKeyValue(PythonDictionary self, object o) {
200-
IEnumerator i = PythonOps.GetEnumerator(o); //c.GetEnumerator();
199+
internal static bool AddKeyValue(CodeContext context, PythonDictionary self, object o) {
200+
IEnumerator i = PythonOps.GetEnumerator(context, o); //c.GetEnumerator();
201201
if (i.MoveNext()) {
202202
object key = i.Current;
203203
if (i.MoveNext()) {

src/core/IronPython/Runtime/Exceptions/PythonExceptions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,8 @@ internal static BaseException CreateBaseExceptionForRaise(CodeContext/*!*/ conte
775775

776776
if (PythonOps.IsInstance(value, type)) {
777777
pyEx = value;
778-
} else if (value is PythonTuple) {
779-
pyEx = PythonOps.CallWithArgsTuple(type, [], value);
778+
} else if (value is PythonTuple pt) {
779+
pyEx = PythonOps.CallWithArgsTuple(type, [], pt);
780780
} else if (value != null) {
781781
pyEx = PythonCalls.Call(context, type, value);
782782
} else {

src/core/IronPython/Runtime/Importer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ private static bool TryGetNestedModule(CodeContext/*!*/ context, PythonModule/*!
588588
Assert.NotNull(context, scope, name);
589589
if (scope.__dict__.TryGetValue(name, out nested)) {
590590
if (nested is PythonModule pm) {
591-
var fullPath = ".".join(SubArray(parts, current));
591+
var fullPath = string.Join(".", SubArray(parts, current));
592592
// double check, some packages mess with package namespace
593593
// see cp35116
594594
if (pm.GetName() == fullPath) {

src/core/IronPython/Runtime/Operations/ArrayOps.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static object __new__(CodeContext context, PythonType pythonType, object
6565
Array res = @base == 0 ?
6666
Array.CreateInstance(type, len) : Array.CreateInstance(type, [len], [@base]);
6767

68-
IEnumerator ie = PythonOps.GetEnumerator(items);
68+
IEnumerator ie = PythonOps.GetEnumerator(context, items);
6969
int i = @base;
7070
while (ie.MoveNext()) {
7171
res.SetValue(Converter.Convert(ie.Current, type), i++);

src/core/IronPython/Runtime/Operations/ByteOps.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ internal static IList<byte> GetBytes(object? value, bool useHint, CodeContext? c
123123
int len = 0;
124124
if (useHint) PythonOps.TryInvokeLengthHint(context ?? DefaultContext.Default, value, out len);
125125
List<byte> ret = new List<byte>(len);
126-
IEnumerator ie = PythonOps.GetEnumerator(value);
126+
IEnumerator ie = PythonOps.GetEnumerator(context ?? DefaultContext.Default, value);
127127
while (ie.MoveNext()) {
128128
ret.Add(GetByte(ie.Current));
129129
}

src/core/IronPython/Runtime/Operations/ObjectOps.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ private static PythonTuple ReduceProtocol2(CodeContext/*!*/ context, object self
381381

382382
object? listIterator = null;
383383
if (self is PythonList) {
384-
listIterator = PythonOps.GetEnumerator(self);
384+
listIterator = PythonOps.GetEnumerator(context, self);
385385
}
386386

387387
object? dictIterator = null;

src/core/IronPython/Runtime/Operations/PythonOps.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ internal static bool IsSubClass(CodeContext/*!*/ context, PythonType c, [NotNull
438438

439439
IEnumerator ie = PythonOps.GetEnumerator(bases);
440440
while (ie.MoveNext()) {
441-
if (!(ie.Current is PythonType baseType)) continue;
441+
if (ie.Current is not PythonType baseType) continue;
442442
if (c.IsSubclassOf(baseType)) return true;
443443
}
444444
return false;
@@ -1025,7 +1025,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence,
10251025
}
10261026
}
10271027

1028-
public static object? CallWithArgsTuple(object func, object?[] args, object argsTuple) {
1028+
public static object? CallWithArgsTuple(object func, object?[] args, IEnumerable argsTuple) {
10291029
if (argsTuple is PythonTuple tp) {
10301030
object?[] nargs = new object[args.Length + tp.__len__()];
10311031
for (int i = 0; i < args.Length; i++) nargs[i] = args[i];
@@ -1035,7 +1035,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence,
10351035

10361036
PythonList allArgs = new PythonList(args.Length + 10);
10371037
allArgs.AddRange(args);
1038-
IEnumerator e = PythonOps.GetEnumerator(argsTuple);
1038+
IEnumerator e = argsTuple.GetEnumerator();
10391039
while (e.MoveNext()) allArgs.AddNoLock(e.Current);
10401040

10411041
return PythonCalls.Call(func, allArgs.GetObjectArray());

0 commit comments

Comments
 (0)