From da40497a7bf4954dc99207904f7a41d691c7b839 Mon Sep 17 00:00:00 2001 From: Pavel Koneski Date: Sun, 6 Apr 2025 13:05:23 -0700 Subject: [PATCH] Eliminate contextless PythonTuple constructor --- src/core/IronPython.Modules/IterTools.cs | 4 ++-- src/core/IronPython.Modules/nt.cs | 2 +- src/core/IronPython.Modules/resource.cs | 2 +- src/core/IronPython.Modules/time.cs | 2 +- src/core/IronPython/Runtime/ClrModule.cs | 2 +- .../PythonExceptions.BaseException.cs | 2 +- .../Runtime/Operations/PythonOps.cs | 4 ++-- src/core/IronPython/Runtime/PythonFunction.cs | 2 +- src/core/IronPython/Runtime/PythonList.cs | 2 +- src/core/IronPython/Runtime/PythonTuple.cs | 24 +++++++++---------- src/extensions/IronPython.SQLite/Cursor.cs | 6 ++--- src/extensions/IronPython.SQLite/Statement.cs | 4 ++-- src/extensions/IronPython.SQLite/_sqlite.cs | 2 +- tests/IronPython.Tests/EngineTest.cs | 2 +- 14 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/core/IronPython.Modules/IterTools.cs b/src/core/IronPython.Modules/IterTools.cs index ee6ae37e5..831add58f 100644 --- a/src/core/IronPython.Modules/IterTools.cs +++ b/src/core/IronPython.Modules/IterTools.cs @@ -763,7 +763,7 @@ public class product : IterBase { private PythonTuple[] tuples; public product(CodeContext context, [NotNone] params object[] iterables) { - tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(PythonOps.GetEnumerator(x))); + tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(context, PythonOps.GetEnumerator(x))); InnerEnumerator = Yielder(tuples); } @@ -788,7 +788,7 @@ public product(CodeContext context, [ParamDictionary] IDictionary modules) { /// All times are expressed in the same unit of measure as DateTime.Ticks /// public static PythonTuple GetProfilerData(CodeContext/*!*/ context, bool includeUnused = false) { - return new PythonTuple(Profiler.GetProfiler(context.LanguageContext).GetProfile(includeUnused)); + return new PythonTuple(DefaultContext.Default, Profiler.GetProfiler(context.LanguageContext).GetProfile(includeUnused)); } /// diff --git a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs index b9bccdaf1..514bb42c1 100644 --- a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs +++ b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs @@ -90,7 +90,7 @@ public static object __new__([NotNone] PythonType/*!*/ cls, [NotNone] params obj } else { res = (BaseException)Activator.CreateInstance(cls.UnderlyingSystemType, cls)!; } - res._args = new PythonTuple(args); + res._args = new PythonTuple(DefaultContext.Default, args); return res; } diff --git a/src/core/IronPython/Runtime/Operations/PythonOps.cs b/src/core/IronPython/Runtime/Operations/PythonOps.cs index 0efee5179..0d45265a1 100644 --- a/src/core/IronPython/Runtime/Operations/PythonOps.cs +++ b/src/core/IronPython/Runtime/Operations/PythonOps.cs @@ -1000,7 +1000,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence, List largs; if (argsTuple != null && args.Length == names.Length) { - if (!(argsTuple is PythonTuple tuple)) tuple = new PythonTuple(argsTuple); + if (!(argsTuple is PythonTuple tuple)) tuple = new PythonTuple(DefaultContext.Default, argsTuple); largs = new List(tuple); largs.AddRange(args); @@ -1813,7 +1813,7 @@ public static void DictUpdate(CodeContext context, PythonDictionary dict, object /// LIST_TO_TUPLE /// [EditorBrowsable(EditorBrowsableState.Never)] - public static PythonTuple ListToTuple(PythonList list) => new PythonTuple(list); + public static PythonTuple ListToTuple(PythonList list) => new PythonTuple(DefaultContext.Default, list); /// /// SET_ADD diff --git a/src/core/IronPython/Runtime/PythonFunction.cs b/src/core/IronPython/Runtime/PythonFunction.cs index effde504f..275c80027 100644 --- a/src/core/IronPython/Runtime/PythonFunction.cs +++ b/src/core/IronPython/Runtime/PythonFunction.cs @@ -139,7 +139,7 @@ public PythonTuple __defaults__ { get { if (_defaults.Length == 0) return null; - return new PythonTuple(_defaults); + return new PythonTuple(DefaultContext.Default, _defaults); } set { _defaults = value == null ? [] : value.ToArray(); diff --git a/src/core/IronPython/Runtime/PythonList.cs b/src/core/IronPython/Runtime/PythonList.cs index 918f8c6e7..f923f29c4 100644 --- a/src/core/IronPython/Runtime/PythonList.cs +++ b/src/core/IronPython/Runtime/PythonList.cs @@ -1302,7 +1302,7 @@ int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { int res; CompareUtil.Push(this); try { - res = ((IStructuralEquatable)new PythonTuple(this)).GetHashCode(comparer); + res = ((IStructuralEquatable)PythonTuple.Make(GetObjectArray())).GetHashCode(comparer); } finally { CompareUtil.Pop(this); } diff --git a/src/core/IronPython/Runtime/PythonTuple.cs b/src/core/IronPython/Runtime/PythonTuple.cs index bdc5fd887..76057e2aa 100644 --- a/src/core/IronPython/Runtime/PythonTuple.cs +++ b/src/core/IronPython/Runtime/PythonTuple.cs @@ -29,8 +29,8 @@ public class PythonTuple : IList, IList, ICodeFormattable, IExpressionS internal static readonly PythonTuple EMPTY = new PythonTuple(); - public PythonTuple([AllowNull]object o) { - _data = MakeItems(o); + public PythonTuple(CodeContext context, [AllowNull]object o) { + _data = MakeItems(context, o); } protected PythonTuple(object?[] items) { @@ -55,19 +55,19 @@ public static PythonTuple __new__(CodeContext context, [NotNone] PythonType cls) if (cls == TypeCache.PythonTuple) { return EMPTY; } else { - if (!(cls.CreateInstance(context) is PythonTuple tupObj)) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls); + if (cls.CreateInstance(context) is not PythonTuple tupObj) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls); return tupObj; } } public static PythonTuple __new__(CodeContext context, [NotNone] PythonType cls, object? sequence) { - if (sequence == null) return new PythonTuple(sequence); // this will throw the proper exception + if (sequence == null) return new PythonTuple(context, sequence); // this will throw the proper exception if (cls == TypeCache.PythonTuple) { if (sequence.GetType() == typeof(PythonTuple)) return (PythonTuple)sequence; - return new PythonTuple(sequence); + return new PythonTuple(context, sequence); } else { - if (!(cls.CreateInstance(context, sequence) is PythonTuple tupObj)) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls); + if (cls.CreateInstance(context, sequence) is not PythonTuple tupObj) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls); return tupObj; } } @@ -115,15 +115,15 @@ public int count(object? obj) { internal static PythonTuple Make(object o) { if (o is PythonTuple t) return t; - return new PythonTuple(o); + return new PythonTuple(DefaultContext.Default, o); } internal static PythonTuple MakeTuple(params object?[] items) { if (items.Length == 0) return EMPTY; - return new PythonTuple(items); + return new PythonTuple(DefaultContext.Default, items); } - private static object?[] MakeItems(object? o) { + private static object?[] MakeItems(CodeContext context, object? o) { var t = o?.GetType(); // Only use fast paths if we have an exact tuple/list, otherwise use iter if (t == typeof(PythonTuple)) { @@ -142,7 +142,7 @@ internal static PythonTuple MakeTuple(params object?[] items) { PerfTrack.NoteEvent(PerfTrack.Categories.OverAllocate, "TupleOA: " + PythonOps.GetPythonTypeName(o)); var l = new List(); - IEnumerator i = PythonOps.GetEnumerator(o); + IEnumerator i = PythonOps.GetEnumerator(context, o); while (i.MoveNext()) { l.Add(i.Current); } @@ -315,8 +315,8 @@ public IEnumerator GetEnumerator() { } public object __getnewargs__() { - // Call "new Tuple()" to force result to be a Tuple (otherwise, it could possibly be a Tuple subclass) - return PythonTuple.MakeTuple(new PythonTuple(this)); + // Call "MakeTuple()" to force result to be specifically a Tuple (otherwise, it could possibly be a Tuple subclass) + return PythonTuple.MakeTuple(_data); } #region IEnumerable Members diff --git a/src/extensions/IronPython.SQLite/Cursor.cs b/src/extensions/IronPython.SQLite/Cursor.cs index 8570ad7ef..84b211f6b 100644 --- a/src/extensions/IronPython.SQLite/Cursor.cs +++ b/src/extensions/IronPython.SQLite/Cursor.cs @@ -239,10 +239,10 @@ private object queryExecute(CodeContext context, bool multiple, object operation (bool?)null }; - new_description[i] = new PythonTuple(descriptor); + new_description[i] = new PythonTuple(DefaultContext.Default, descriptor); } - this.description = new PythonTuple(new_description); + this.description = new PythonTuple(DefaultContext.Default, new_description); } } @@ -362,7 +362,7 @@ private object fetchOneRow(CodeContext context) row[i] = converted; } - return new PythonTuple(row); + return new PythonTuple(DefaultContext.Default, row); } public Cursor executescript(string operation) diff --git a/src/extensions/IronPython.SQLite/Statement.cs b/src/extensions/IronPython.SQLite/Statement.cs index 373787b47..b48874bae 100644 --- a/src/extensions/IronPython.SQLite/Statement.cs +++ b/src/extensions/IronPython.SQLite/Statement.cs @@ -213,7 +213,7 @@ value is string || object proto = DynamicHelpers.GetPythonTypeFromType(typeof(PythonSQLite.PrepareProtocol)); object type = DynamicHelpers.GetPythonType(value); - object key = new PythonTuple(new[] { type, proto }); + object key = new PythonTuple(DefaultContext.Default, new[] { type, proto }); return PythonSQLite.adapters.ContainsKey(key); } @@ -228,7 +228,7 @@ private object adaptValue(CodeContext context, object value) object proto = DynamicHelpers.GetPythonTypeFromType(typeof(PythonSQLite.PrepareProtocol)); object type = DynamicHelpers.GetPythonType(value); - object key = new PythonTuple(new[] { type, proto }); + object key = new PythonTuple(DefaultContext.Default, new[] { type, proto }); object adapter; if(PythonSQLite.adapters.TryGetValue(key, out adapter)) diff --git a/src/extensions/IronPython.SQLite/_sqlite.cs b/src/extensions/IronPython.SQLite/_sqlite.cs index 732a93db3..34efd46c3 100644 --- a/src/extensions/IronPython.SQLite/_sqlite.cs +++ b/src/extensions/IronPython.SQLite/_sqlite.cs @@ -59,7 +59,7 @@ public static object connect(CodeContext context, public static void register_adapter(CodeContext context, PythonType type, object adapter) { object proto = DynamicHelpers.GetPythonTypeFromType(typeof(PrepareProtocol)); - object key = new PythonTuple(new object[] { type, proto }); + object key = new PythonTuple(DefaultContext.Default, new object[] { type, proto }); adapters[key] = adapter; } diff --git a/tests/IronPython.Tests/EngineTest.cs b/tests/IronPython.Tests/EngineTest.cs index 67fe9d3d6..27ef5f36e 100644 --- a/tests/IronPython.Tests/EngineTest.cs +++ b/tests/IronPython.Tests/EngineTest.cs @@ -1682,7 +1682,7 @@ public void ScenarioEvaluateInAnonymousEngineModule() { [Test] public void ScenarioObjectOperations() { var ops = _pe.Operations; - Assert.That(ops.Format(new PythonTuple(new object[] { 1, 2, 3 })), Is.EqualTo("(1, 2, 3)")); + Assert.That(ops.Format(PythonTuple.Make(new object[] { 1, 2, 3 })), Is.EqualTo("(1, 2, 3)")); var scope = _pe.CreateScope(); scope.SetVariable("ops", ops);