Skip to content

Commit e8ed79b

Browse files
authored
Use actual code context for enumerating (#1721)
1 parent 54081df commit e8ed79b

18 files changed

Lines changed: 160 additions & 112 deletions

File tree

Src/IronPython.Modules/IterTools.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -743,11 +743,11 @@ private static Exception UnexpectedKeywordArgument(IDictionary<object, object> p
743743

744744
[PythonType]
745745
public class product : IterBase {
746-
public product(params object[] iterables) {
747-
InnerEnumerator = Yielder(ArrayUtils.ConvertAll(iterables, x => new PythonList(PythonOps.GetEnumerator(x))));
746+
public product(CodeContext context, params object[] iterables) {
747+
InnerEnumerator = Yielder(ArrayUtils.ConvertAll(iterables, x => new PythonList(context, PythonOps.GetEnumerator(x))));
748748
}
749749

750-
public product([ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
750+
public product(CodeContext context, [ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
751751
object repeat;
752752
int iRepeat = 1;
753753
if (paramDict.TryGetValue("repeat", out repeat)) {
@@ -768,7 +768,7 @@ public product([ParamDictionary]IDictionary<object, object> paramDict, params ob
768768
PythonList[] finalIterables = new PythonList[iterables.Length * iRepeat];
769769
for (int i = 0; i < iRepeat; i++) {
770770
for (int j = 0; j < iterables.Length; j++) {
771-
finalIterables[i * iterables.Length + j] = new PythonList(iterables[j]);
771+
finalIterables[i * iterables.Length + j] = new PythonList(context, iterables[j]);
772772
}
773773
}
774774
InnerEnumerator = Yielder(finalIterables);
@@ -823,8 +823,8 @@ private IEnumerator<object> Yielder(PythonList[] iterables) {
823823
public class combinations : IterBase {
824824
private readonly PythonList _data;
825825

826-
public combinations(object iterable, object r) {
827-
_data = new PythonList(iterable);
826+
public combinations(CodeContext context, object iterable, object r) {
827+
_data = new PythonList(context, iterable);
828828

829829
InnerEnumerator = Yielder(GetR(r, _data));
830830
}
@@ -893,8 +893,8 @@ private IEnumerator<object> Yielder(int r) {
893893
public class combinations_with_replacement : IterBase {
894894
private readonly PythonList _data;
895895

896-
public combinations_with_replacement(object iterable, object r) {
897-
_data = new PythonList(iterable);
896+
public combinations_with_replacement(CodeContext context, object iterable, object r) {
897+
_data = new PythonList(context, iterable);
898898

899899
InnerEnumerator = Yielder(GetR(r, _data));
900900
}
@@ -962,14 +962,14 @@ private IEnumerator<object> Yielder(int r) {
962962
public class permutations : IterBase {
963963
private readonly PythonList _data;
964964

965-
public permutations(object iterable) {
966-
_data = new PythonList(iterable);
965+
public permutations(CodeContext context, object iterable) {
966+
_data = new PythonList(context, iterable);
967967

968968
InnerEnumerator = Yielder(_data.Count);
969969
}
970970

971-
public permutations(object iterable, object r) {
972-
_data = new PythonList(iterable);
971+
public permutations(CodeContext context, object iterable, object r) {
972+
_data = new PythonList(context, iterable);
973973

974974
InnerEnumerator = Yielder(GetR(r, _data));
975975
}
@@ -1160,7 +1160,7 @@ private IEnumerator<object> Yielder(CodeContext context, object function, IEnume
11601160
objargs[i] = args[i];
11611161
}
11621162
} else {
1163-
PythonList argsList = new PythonList(PythonOps.GetEnumerator(iter.Current));
1163+
PythonList argsList = new PythonList(context, PythonOps.GetEnumerator(iter.Current));
11641164
objargs = ArrayUtils.ToArray(argsList);
11651165
}
11661166

Src/IronPython.Modules/_ssl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void load_cert_chain(CodeContext context, string certfile, string keyfile
208208

209209
public PythonList get_ca_certs(CodeContext context, bool binary_form = false) {
210210
if (binary_form) throw new NotImplementedException(nameof(binary_form));
211-
return new PythonList(_cert_store.Cast<X509Certificate2>().Select(c => CertificateToPython(context, c)));
211+
return PythonList.FromEnumerable(_cert_store.Cast<X509Certificate2>().Select(c => CertificateToPython(context, c)));
212212
}
213213

214214
public void load_verify_locations(CodeContext context, object cafile = null, string capath = null, object cadata = null) {

Src/IronPython.Modules/grp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal struct_group(string gr_name, string gr_passwd, int gr_gid, PythonList g
7676

7777
private static struct_group Make(IntPtr pwd) {
7878
group g = (group)Marshal.PtrToStructure(pwd, typeof(group));
79-
return new struct_group(g.gr_name, g.gr_passwd, g.gr_gid, new PythonList(MarshalStringArray(g.gr_mem)));
79+
return new struct_group(g.gr_name, g.gr_passwd, g.gr_gid, PythonList.FromEnumerable(MarshalStringArray(g.gr_mem)));
8080
}
8181

8282
private static IEnumerable<string> MarshalStringArray(IntPtr arrayPtr)

Src/IronPython.Modules/xxsubtype.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public spamlist()
2424
: base() {
2525
}
2626

27-
public spamlist(object sequence)
28-
: base(sequence) {
27+
public spamlist(CodeContext context, object sequence)
28+
: base(context, sequence) {
2929
}
3030

3131
private int _state;

Src/IronPython.SQLite/Cursor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private object queryExecute(CodeContext context, bool multiple, object operation
121121
if(multiple)
122122
{
123123
if(args != null)
124-
parameters_iter = PythonOps.CreatePythonEnumerator(args);
124+
parameters_iter = PythonOps.CreatePythonEnumerator(context, args);
125125
}
126126
else
127127
{

Src/IronPython/Modules/Builtin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ public static void delattr(CodeContext/*!*/ context, object? o, [NotNone] string
250250
public static PythonType dict => TypeCache.Dict;
251251

252252
public static PythonList dir(CodeContext/*!*/ context) {
253-
PythonList res = new PythonList(context.Dict.Keys);
253+
PythonList res = new PythonList(context, context.Dict.Keys);
254254

255255
res.Sort(context);
256256
return res;
257257
}
258258

259259
public static PythonList dir(CodeContext/*!*/ context, object? o) {
260260
IList<object?> ret = PythonOps.GetAttrNames(context, o);
261-
PythonList lret = new PythonList(ret);
261+
PythonList lret = new PythonList(context, ret);
262262
lret.Sort(context);
263263
return lret;
264264
}

Src/IronPython/Modules/_ast.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ public Global(PythonList names, [Optional] int? lineno, [Optional] int? col_offs
17731773

17741774
internal Global(GlobalStatement stmt)
17751775
: this() {
1776-
names = new PythonList(stmt.Names);
1776+
names = PythonList.FromGenericCollection(stmt.Names);
17771777
}
17781778

17791779
internal override Statement Revert() {
@@ -2345,7 +2345,7 @@ public Nonlocal(PythonList names, [Optional] int? lineno, [Optional] int? col_of
23452345

23462346
internal Nonlocal(NonlocalStatement stmt)
23472347
: this() {
2348-
names = new PythonList(stmt.Names);
2348+
names = PythonList.FromGenericCollection(stmt.Names);
23492349
}
23502350

23512351
internal override Statement Revert() {

Src/IronPython/Runtime/Binding/ConversionBinder.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ internal static DynamicMetaObject ConvertToIEnumerable(DynamicMetaObjectBinder/*
781781
PythonTypeSlot pts;
782782

783783
if (pt.TryResolveSlot(context, "__iter__", out pts)) {
784-
return MakeIterRule(metaUserObject, nameof(PythonOps.CreatePythonEnumerable));
784+
return MakeIterRule(metaUserObject, pyContext, nameof(PythonOps.CreatePythonEnumerable));
785785
} else if (pt.TryResolveSlot(context, "__getitem__", out pts)) {
786786
return MakeGetItemIterable(metaUserObject, pyContext, pts, nameof(PythonOps.CreateItemEnumerable));
787787
}
@@ -804,6 +804,7 @@ internal static DynamicMetaObject ConvertToIEnumerator(DynamicMetaObjectBinder/*
804804
new[] { tmp },
805805
Expression.Call(
806806
typeof(PythonOps).GetMethod(nameof(PythonOps.CreatePythonEnumerator)),
807+
AstUtils.Constant(context),
807808
Ast.Block(
808809
MetaPythonObject.MakeTryGetTypeMember(
809810
state,
@@ -839,6 +840,7 @@ private static DynamicMetaObject MakeGetItemIterable(DynamicMetaObject metaUserO
839840
new[] { tmp },
840841
Expression.Call(
841842
typeof(PythonOps).GetMethod(method),
843+
AstUtils.Constant(state.SharedContext),
842844
AstUtils.Convert(metaUserObject.Expression, typeof(object)),
843845
Ast.Block(
844846
MetaPythonObject.MakeTryGetTypeMember(
@@ -867,10 +869,11 @@ private static DynamicMetaObject MakeGetItemIterable(DynamicMetaObject metaUserO
867869
);
868870
}
869871

870-
private static DynamicMetaObject/*!*/ MakeIterRule(DynamicMetaObject/*!*/ self, string methodName) {
872+
private static DynamicMetaObject/*!*/ MakeIterRule(DynamicMetaObject/*!*/ self, PythonContext state, string methodName) {
871873
return new DynamicMetaObject(
872874
Ast.Call(
873875
typeof(PythonOps).GetMethod(methodName),
876+
AstUtils.Constant(state.SharedContext),
874877
AstUtils.Convert(self.Expression, typeof(object))
875878
),
876879
self.Restrictions

Src/IronPython/Runtime/Binding/MetaPythonFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ private void MakeParamsCopy(Expression/*!*/ userList) {
955955
_params,
956956
Ast.Call(
957957
typeof(PythonOps).GetMethod(nameof(PythonOps.CopyAndVerifyParamsList)),
958+
_codeContext ?? AstUtils.Constant(DefaultContext.Default),
958959
AstUtils.Convert(GetFunctionParam(), typeof(PythonFunction)),
959960
AstUtils.Convert(userList, typeof(object))
960961
)

Src/IronPython/Runtime/Bytes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ public IEnumerator<byte> GetEnumerator() {
11651165
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
11661166
// workaround for https://github.com/IronLanguages/ironpython3/issues/1519
11671167
if (GetType() != typeof(Bytes) && PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, this, "__iter__", out object? iter)) {
1168-
return new PythonEnumerator(iter);
1168+
return new PythonEnumerator(DefaultContext.Default, iter);
11691169
}
11701170
return _bytes.GetEnumerator();
11711171
}

0 commit comments

Comments
 (0)