Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use actual code context for enumerating #1721

Merged
merged 6 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ public class zip_longest : IEnumerator {

[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 class product : IterBase {
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 @@ public class product : IterBase {
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 @@ public class combinations : IterBase {
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 @@ public class combinations_with_replacement : IterBase {
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 @@ public class starmap : IterBase {
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 class _SSLContext {

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 @@ public class struct_group : PythonTuple {

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 partial class Builtin {
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 @@ private class IdentityConversion<T> {
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 @@ private class IdentityConversion<T> {
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 class IdentityConversion<T> {
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 class IdentityConversion<T> {
);
}

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 class FunctionBinderHelper {
_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 Bytes __rmod__(CodeContext context, [NotNone] Bytes value)
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 @@ public RuntimeReturnChecker(object instance, [NotNone] object function, [NotNone
/// <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
Loading