Skip to content

Commit

Permalink
Eliminate contextless PythonList constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp committed Aug 28, 2023
1 parent b762549 commit 5a955c7
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 38 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
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
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
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
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/Operations/InstanceOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static object NextMethod(object self) {
/// __dir__(self) -> Returns the list of members defined on a foreign IDynamicMetaObjectProvider.
/// </summary>
public static PythonList DynamicDir(CodeContext/*!*/ context, IDynamicMetaObjectProvider self) {
PythonList res = new PythonList(self.GetMetaObject(Expression.Parameter(typeof(object))).GetDynamicMemberNames());
PythonList res = new PythonList(context, self.GetMetaObject(Expression.Parameter(typeof(object))).GetDynamicMemberNames());

// add in the non-dynamic members from the dynamic objects base class.
Type t = self.GetType();
Expand Down
6 changes: 3 additions & 3 deletions Src/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ internal static IList<string> GetStringMemberList(IPythonMembersList pyMemList)
}

if (o is IMembersList memList) {
return new PythonList(memList.GetMemberNames());
return new PythonList(context, memList.GetMemberNames());
}

if (o is IPythonObject po) {
Expand Down Expand Up @@ -2637,8 +2637,8 @@ public static void VerifyUnduplicatedByName(PythonFunction function, string name
}


public static PythonList CopyAndVerifyParamsList(PythonFunction function, object list) {
return new PythonList(list);
public static PythonList CopyAndVerifyParamsList(CodeContext context, PythonFunction function, object list) {
return new PythonList(context, list);
}

public static PythonTuple UserMappingToPythonTuple(CodeContext/*!*/ context, object list, string funcName) {
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython/Runtime/PythonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ private void UnhookAssemblyResolve() {
public override ICollection<string> GetSearchPaths() {
List<string> result = new List<string>();
if (TryGetSystemPath(out PythonList paths)) {
IEnumerator ie = PythonOps.GetEnumerator(paths);
IEnumerator ie = PythonOps.GetEnumerator(SharedContext, paths);
while (ie.MoveNext()) {
if (TryConvertToString(ie.Current, out string str)) {
result.Add(str);
Expand All @@ -1271,7 +1271,7 @@ public override ICollection<string> GetSearchPaths() {
}

public override void SetSearchPaths(ICollection<string> paths) {
SetSystemStateValue("path", new PythonList(paths));
SetSystemStateValue("path", new PythonList(SharedContext, paths));
}

public override void Shutdown() {
Expand Down
30 changes: 25 additions & 5 deletions Src/IronPython/Runtime/PythonList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal PythonList(int capacity) {
}
}

private PythonList(params object?[] items) {
private PythonList(object?[] items) {
_data = items;
_size = _data.Length;
}
Expand All @@ -160,7 +160,7 @@ public PythonList()
}
#endif

internal PythonList(object sequence) {
internal PythonList(CodeContext context, object sequence) {
if (sequence is ICollection items) {
_data = new object[items.Count];
int i = 0;
Expand All @@ -169,12 +169,12 @@ internal PythonList(object sequence) {
}
_size = i;
} else {
if (!PythonOps.TryInvokeLengthHint(DefaultContext.Default, sequence, out int len)) {
if (!PythonOps.TryInvokeLengthHint(context, sequence, out int len)) {
len = INITIAL_SIZE;
}

_data = new object[len];
ExtendNoLengthCheck(DefaultContext.Default, sequence);
ExtendNoLengthCheck(context, sequence);
}
}

Expand All @@ -188,6 +188,26 @@ internal PythonList(ICollection items)
_size = i;
}

internal static PythonList FromGenericCollection<T>(ICollection<T> items) {
var list = new PythonList(items.Count);

int i = 0;
foreach (object? item in items) {
list._data[i++] = item;
}
list._size = i;
return list;
}

internal static PythonList FromEnumerable(IEnumerable items) {
var enumerator = items.GetEnumerator();
try {
return new PythonList(enumerator);
} finally {
(enumerator as IDisposable)?.Dispose();
}
}

/// <summary>
/// Creates a new list with the data in the array and a size
/// the same as the length of the array. The array is held
Expand Down Expand Up @@ -445,7 +465,7 @@ public virtual object? this[[NotNone] Slice slice] {
set {
if (slice.step != null && (!(slice.step is int) || !slice.step.Equals(_boxedOne))) {
// try to assign back to self: make a copy first
if (this == value) value = new PythonList(value);
if (this == value) value = new PythonList((ICollection)value);

if (ValueRequiresNoLocks(value)) {
// we don't need to worry about lock ordering of accesses to the
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/Set.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ public PythonTuple __reduce__(CodeContext/*!*/ context) {
context.TryLookupBuiltin("iter", out iter);
if (_cnt < 0)
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(new PythonList()));
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(new PythonList(_items)), _cnt);
return PythonTuple.MakeTuple(iter, PythonTuple.MakeTuple(new PythonList(context, _items)), _cnt);
}

public int __length_hint__() {
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/Types/PythonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ private PythonList TryGetCustomDir(CodeContext context, object self) {
? context.LanguageContext.GetSiteCacheForSystemType(UnderlyingSystemType).GetDirSite(context)
: _siteCache.GetDirSite(context);

return new PythonList(dirSite.Target(dirSite, context, dir));
return new PythonList(context, dirSite.Target(dirSite, context, dir));
}
}

Expand Down

0 comments on commit 5a955c7

Please sign in to comment.