Skip to content

Commit

Permalink
Use actual code context for enumerating (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp authored Sep 2, 2023
1 parent 54081df commit e8ed79b
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 112 deletions.
26 changes: 13 additions & 13 deletions Src/IronPython.Modules/IterTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,11 @@ private static Exception UnexpectedKeywordArgument(IDictionary<object, object> p

[PythonType]
public class product : IterBase {
public product(params object[] iterables) {
InnerEnumerator = Yielder(ArrayUtils.ConvertAll(iterables, x => new PythonList(PythonOps.GetEnumerator(x))));
public product(CodeContext context, params object[] iterables) {
InnerEnumerator = Yielder(ArrayUtils.ConvertAll(iterables, x => new PythonList(context, PythonOps.GetEnumerator(x))));
}

public product([ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
public product(CodeContext context, [ParamDictionary]IDictionary<object, object> paramDict, params object[] iterables) {
object repeat;
int iRepeat = 1;
if (paramDict.TryGetValue("repeat", out repeat)) {
Expand All @@ -768,7 +768,7 @@ public product([ParamDictionary]IDictionary<object, object> paramDict, params ob
PythonList[] finalIterables = new PythonList[iterables.Length * iRepeat];
for (int i = 0; i < iRepeat; i++) {
for (int j = 0; j < iterables.Length; j++) {
finalIterables[i * iterables.Length + j] = new PythonList(iterables[j]);
finalIterables[i * iterables.Length + j] = new PythonList(context, iterables[j]);
}
}
InnerEnumerator = Yielder(finalIterables);
Expand Down Expand Up @@ -823,8 +823,8 @@ private IEnumerator<object> Yielder(PythonList[] iterables) {
public class combinations : IterBase {
private readonly PythonList _data;

public combinations(object iterable, object r) {
_data = new PythonList(iterable);
public combinations(CodeContext context, object iterable, object r) {
_data = new PythonList(context, iterable);

InnerEnumerator = Yielder(GetR(r, _data));
}
Expand Down Expand Up @@ -893,8 +893,8 @@ private IEnumerator<object> Yielder(int r) {
public class combinations_with_replacement : IterBase {
private readonly PythonList _data;

public combinations_with_replacement(object iterable, object r) {
_data = new PythonList(iterable);
public combinations_with_replacement(CodeContext context, object iterable, object r) {
_data = new PythonList(context, iterable);

InnerEnumerator = Yielder(GetR(r, _data));
}
Expand Down Expand Up @@ -962,14 +962,14 @@ private IEnumerator<object> Yielder(int r) {
public class permutations : IterBase {
private readonly PythonList _data;

public permutations(object iterable) {
_data = new PythonList(iterable);
public permutations(CodeContext context, object iterable) {
_data = new PythonList(context, iterable);

InnerEnumerator = Yielder(_data.Count);
}

public permutations(object iterable, object r) {
_data = new PythonList(iterable);
public permutations(CodeContext context, object iterable, object r) {
_data = new PythonList(context, iterable);

InnerEnumerator = Yielder(GetR(r, _data));
}
Expand Down Expand Up @@ -1160,7 +1160,7 @@ private IEnumerator<object> Yielder(CodeContext context, object function, IEnume
objargs[i] = args[i];
}
} else {
PythonList argsList = new PythonList(PythonOps.GetEnumerator(iter.Current));
PythonList argsList = new PythonList(context, PythonOps.GetEnumerator(iter.Current));
objargs = ArrayUtils.ToArray(argsList);
}

Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython.Modules/_ssl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void load_cert_chain(CodeContext context, string certfile, string keyfile

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

public void load_verify_locations(CodeContext context, object cafile = null, string capath = null, object cadata = null) {
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython.Modules/grp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal struct_group(string gr_name, string gr_passwd, int gr_gid, PythonList g

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

private static IEnumerable<string> MarshalStringArray(IntPtr arrayPtr)
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython.Modules/xxsubtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public spamlist()
: base() {
}

public spamlist(object sequence)
: base(sequence) {
public spamlist(CodeContext context, object sequence)
: base(context, sequence) {
}

private int _state;
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython.SQLite/Cursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private object queryExecute(CodeContext context, bool multiple, object operation
if(multiple)
{
if(args != null)
parameters_iter = PythonOps.CreatePythonEnumerator(args);
parameters_iter = PythonOps.CreatePythonEnumerator(context, args);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython/Modules/Builtin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ public static void delattr(CodeContext/*!*/ context, object? o, [NotNone] string
public static PythonType dict => TypeCache.Dict;

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

res.Sort(context);
return res;
}

public static PythonList dir(CodeContext/*!*/ context, object? o) {
IList<object?> ret = PythonOps.GetAttrNames(context, o);
PythonList lret = new PythonList(ret);
PythonList lret = new PythonList(context, ret);
lret.Sort(context);
return lret;
}
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython/Modules/_ast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ public Global(PythonList names, [Optional] int? lineno, [Optional] int? col_offs

internal Global(GlobalStatement stmt)
: this() {
names = new PythonList(stmt.Names);
names = PythonList.FromGenericCollection(stmt.Names);
}

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

internal Nonlocal(NonlocalStatement stmt)
: this() {
names = new PythonList(stmt.Names);
names = PythonList.FromGenericCollection(stmt.Names);
}

internal override Statement Revert() {
Expand Down
7 changes: 5 additions & 2 deletions Src/IronPython/Runtime/Binding/ConversionBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ internal static DynamicMetaObject ConvertToIEnumerable(DynamicMetaObjectBinder/*
PythonTypeSlot pts;

if (pt.TryResolveSlot(context, "__iter__", out pts)) {
return MakeIterRule(metaUserObject, nameof(PythonOps.CreatePythonEnumerable));
return MakeIterRule(metaUserObject, pyContext, nameof(PythonOps.CreatePythonEnumerable));
} else if (pt.TryResolveSlot(context, "__getitem__", out pts)) {
return MakeGetItemIterable(metaUserObject, pyContext, pts, nameof(PythonOps.CreateItemEnumerable));
}
Expand All @@ -804,6 +804,7 @@ internal static DynamicMetaObject ConvertToIEnumerator(DynamicMetaObjectBinder/*
new[] { tmp },
Expression.Call(
typeof(PythonOps).GetMethod(nameof(PythonOps.CreatePythonEnumerator)),
AstUtils.Constant(context),
Ast.Block(
MetaPythonObject.MakeTryGetTypeMember(
state,
Expand Down Expand Up @@ -839,6 +840,7 @@ private static DynamicMetaObject MakeGetItemIterable(DynamicMetaObject metaUserO
new[] { tmp },
Expression.Call(
typeof(PythonOps).GetMethod(method),
AstUtils.Constant(state.SharedContext),
AstUtils.Convert(metaUserObject.Expression, typeof(object)),
Ast.Block(
MetaPythonObject.MakeTryGetTypeMember(
Expand Down Expand Up @@ -867,10 +869,11 @@ private static DynamicMetaObject MakeGetItemIterable(DynamicMetaObject metaUserO
);
}

private static DynamicMetaObject/*!*/ MakeIterRule(DynamicMetaObject/*!*/ self, string methodName) {
private static DynamicMetaObject/*!*/ MakeIterRule(DynamicMetaObject/*!*/ self, PythonContext state, string methodName) {
return new DynamicMetaObject(
Ast.Call(
typeof(PythonOps).GetMethod(methodName),
AstUtils.Constant(state.SharedContext),
AstUtils.Convert(self.Expression, typeof(object))
),
self.Restrictions
Expand Down
1 change: 1 addition & 0 deletions Src/IronPython/Runtime/Binding/MetaPythonFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@ private void MakeParamsCopy(Expression/*!*/ userList) {
_params,
Ast.Call(
typeof(PythonOps).GetMethod(nameof(PythonOps.CopyAndVerifyParamsList)),
_codeContext ?? AstUtils.Constant(DefaultContext.Default),
AstUtils.Convert(GetFunctionParam(), typeof(PythonFunction)),
AstUtils.Convert(userList, typeof(object))
)
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ public IEnumerator<byte> GetEnumerator() {
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
// workaround for https://github.com/IronLanguages/ironpython3/issues/1519
if (GetType() != typeof(Bytes) && PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, this, "__iter__", out object? iter)) {
return new PythonEnumerator(iter);
return new PythonEnumerator(DefaultContext.Default, iter);
}
return _bytes.GetEnumerator();
}
Expand Down
8 changes: 4 additions & 4 deletions Src/IronPython/Runtime/ClrModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -837,19 +837,19 @@ private static bool IsInstanceOf(object obj, PythonType pt) {
/// <summary>
/// returns the result of dir(o) as-if "import clr" has not been performed.
/// </summary>
public static PythonList Dir(object o) {
public static PythonList Dir(CodeContext context, object o) {
IList<object> ret = PythonOps.GetAttrNames(DefaultContext.Default, o);
PythonList lret = new PythonList(ret);
PythonList lret = new PythonList(context, ret);
lret.Sort(DefaultContext.Default);
return lret;
}

/// <summary>
/// Returns the result of dir(o) as-if "import clr" has been performed.
/// </summary>
public static PythonList DirClr(object o) {
public static PythonList DirClr(CodeContext context, object o) {
IList<object> ret = PythonOps.GetAttrNames(DefaultContext.DefaultCLS, o);
PythonList lret = new PythonList(ret);
PythonList lret = new PythonList(context, ret);
lret.Sort(DefaultContext.DefaultCLS);
return lret;
}
Expand Down
Loading

0 comments on commit e8ed79b

Please sign in to comment.