Skip to content

Commit 0c9866f

Browse files
authored
Eliminate contextless PythonTuple constructor (IronLanguages#1946)
1 parent 2505d91 commit 0c9866f

File tree

14 files changed

+30
-30
lines changed

14 files changed

+30
-30
lines changed

src/core/IronPython.Modules/IterTools.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public class product : IterBase {
763763
private PythonTuple[] tuples;
764764

765765
public product(CodeContext context, [NotNone] params object[] iterables) {
766-
tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(PythonOps.GetEnumerator(x)));
766+
tuples = ArrayUtils.ConvertAll(iterables, x => new PythonTuple(context, PythonOps.GetEnumerator(x)));
767767
InnerEnumerator = Yielder(tuples);
768768
}
769769

@@ -788,7 +788,7 @@ public product(CodeContext context, [ParamDictionary] IDictionary<object, object
788788
tuples = new PythonTuple[iterables.Length * iRepeat];
789789
for (int i = 0; i < iRepeat; i++) {
790790
for (int j = 0; j < iterables.Length; j++) {
791-
tuples[i * iterables.Length + j] = new PythonTuple(iterables[j]);
791+
tuples[i * iterables.Length + j] = new PythonTuple(context, iterables[j]);
792792
}
793793
}
794794
InnerEnumerator = Yielder(tuples);

src/core/IronPython.Modules/nt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ public PythonTuple __reduce__() {
13891389

13901390
return MakeTuple(
13911391
DynamicHelpers.GetPythonTypeFromType(typeof(stat_result)),
1392-
MakeTuple(new PythonTuple(this), timeDict)
1392+
MakeTuple(new PythonTuple(DefaultContext.Default, this), timeDict)
13931393
);
13941394
}
13951395
}

src/core/IronPython.Modules/resource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private struct_rusage(object?[] sequence) : base(sequence) {
245245
throw PythonOps.TypeError("resource.struct_rusage() takes a {0}-sequence ({1}-sequence given)", n_sequence_fields, __len__());
246246
}
247247

248-
private struct_rusage(object o) : base(o) {
248+
private struct_rusage(object o) : base(DefaultContext.Default, o) {
249249
if (__len__() != n_sequence_fields)
250250
throw PythonOps.TypeError("resource.struct_rusage() takes a {0}-sequence ({1}-sequence given)", n_sequence_fields, __len__());
251251
}

src/core/IronPython.Modules/time.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ internal struct_time(int year, int month, int day, int hour, int minute, int sec
512512
}
513513

514514
internal struct_time(PythonTuple sequence)
515-
: base(sequence) {
515+
: base(DefaultContext.Default, sequence) {
516516
}
517517

518518
public static struct_time __new__(CodeContext context, [NotNone] PythonType cls, int year, int month, int day, int hour, int minute, int second, int dayOfWeek, int dayOfYear, int isDst) {

src/core/IronPython/Runtime/ClrModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ private static void SortModules(List<string> modules) {
11301130
/// All times are expressed in the same unit of measure as DateTime.Ticks
11311131
/// </summary>
11321132
public static PythonTuple GetProfilerData(CodeContext/*!*/ context, bool includeUnused = false) {
1133-
return new PythonTuple(Profiler.GetProfiler(context.LanguageContext).GetProfile(includeUnused));
1133+
return new PythonTuple(DefaultContext.Default, Profiler.GetProfiler(context.LanguageContext).GetProfile(includeUnused));
11341134
}
11351135

11361136
/// <summary>

src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static object __new__([NotNone] PythonType/*!*/ cls, [NotNone] params obj
9090
} else {
9191
res = (BaseException)Activator.CreateInstance(cls.UnderlyingSystemType, cls)!;
9292
}
93-
res._args = new PythonTuple(args);
93+
res._args = new PythonTuple(DefaultContext.Default, args);
9494
return res;
9595
}
9696

src/core/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ internal static bool TryInvokeLengthHint(CodeContext context, object? sequence,
10001000
List<object?> largs;
10011001

10021002
if (argsTuple != null && args.Length == names.Length) {
1003-
if (!(argsTuple is PythonTuple tuple)) tuple = new PythonTuple(argsTuple);
1003+
if (!(argsTuple is PythonTuple tuple)) tuple = new PythonTuple(DefaultContext.Default, argsTuple);
10041004

10051005
largs = new List<object?>(tuple);
10061006
largs.AddRange(args);
@@ -1813,7 +1813,7 @@ public static void DictUpdate(CodeContext context, PythonDictionary dict, object
18131813
/// LIST_TO_TUPLE
18141814
/// </summary>
18151815
[EditorBrowsable(EditorBrowsableState.Never)]
1816-
public static PythonTuple ListToTuple(PythonList list) => new PythonTuple(list);
1816+
public static PythonTuple ListToTuple(PythonList list) => new PythonTuple(DefaultContext.Default, list);
18171817

18181818
/// <summary>
18191819
/// SET_ADD

src/core/IronPython/Runtime/PythonFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public PythonTuple __defaults__ {
139139
get {
140140
if (_defaults.Length == 0) return null;
141141

142-
return new PythonTuple(_defaults);
142+
return new PythonTuple(DefaultContext.Default, _defaults);
143143
}
144144
set {
145145
_defaults = value == null ? [] : value.ToArray();

src/core/IronPython/Runtime/PythonList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) {
13021302
int res;
13031303
CompareUtil.Push(this);
13041304
try {
1305-
res = ((IStructuralEquatable)new PythonTuple(this)).GetHashCode(comparer);
1305+
res = ((IStructuralEquatable)PythonTuple.Make(GetObjectArray())).GetHashCode(comparer);
13061306
} finally {
13071307
CompareUtil.Pop(this);
13081308
}

src/core/IronPython/Runtime/PythonTuple.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public class PythonTuple : IList, IList<object?>, ICodeFormattable, IExpressionS
2929

3030
internal static readonly PythonTuple EMPTY = new PythonTuple();
3131

32-
public PythonTuple([AllowNull]object o) {
33-
_data = MakeItems(o);
32+
public PythonTuple(CodeContext context, [AllowNull]object o) {
33+
_data = MakeItems(context, o);
3434
}
3535

3636
protected PythonTuple(object?[] items) {
@@ -55,19 +55,19 @@ public static PythonTuple __new__(CodeContext context, [NotNone] PythonType cls)
5555
if (cls == TypeCache.PythonTuple) {
5656
return EMPTY;
5757
} else {
58-
if (!(cls.CreateInstance(context) is PythonTuple tupObj)) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls);
58+
if (cls.CreateInstance(context) is not PythonTuple tupObj) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls);
5959
return tupObj;
6060
}
6161
}
6262

6363
public static PythonTuple __new__(CodeContext context, [NotNone] PythonType cls, object? sequence) {
64-
if (sequence == null) return new PythonTuple(sequence); // this will throw the proper exception
64+
if (sequence == null) return new PythonTuple(context, sequence); // this will throw the proper exception
6565

6666
if (cls == TypeCache.PythonTuple) {
6767
if (sequence.GetType() == typeof(PythonTuple)) return (PythonTuple)sequence;
68-
return new PythonTuple(sequence);
68+
return new PythonTuple(context, sequence);
6969
} else {
70-
if (!(cls.CreateInstance(context, sequence) is PythonTuple tupObj)) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls);
70+
if (cls.CreateInstance(context, sequence) is not PythonTuple tupObj) throw PythonOps.TypeError("{0} is not a subclass of tuple", cls);
7171
return tupObj;
7272
}
7373
}
@@ -115,15 +115,15 @@ public int count(object? obj) {
115115

116116
internal static PythonTuple Make(object o) {
117117
if (o is PythonTuple t) return t;
118-
return new PythonTuple(o);
118+
return new PythonTuple(DefaultContext.Default, o);
119119
}
120120

121121
internal static PythonTuple MakeTuple(params object?[] items) {
122122
if (items.Length == 0) return EMPTY;
123-
return new PythonTuple(items);
123+
return new PythonTuple(DefaultContext.Default, items);
124124
}
125125

126-
private static object?[] MakeItems(object? o) {
126+
private static object?[] MakeItems(CodeContext context, object? o) {
127127
var t = o?.GetType();
128128
// Only use fast paths if we have an exact tuple/list, otherwise use iter
129129
if (t == typeof(PythonTuple)) {
@@ -142,7 +142,7 @@ internal static PythonTuple MakeTuple(params object?[] items) {
142142
PerfTrack.NoteEvent(PerfTrack.Categories.OverAllocate, "TupleOA: " + PythonOps.GetPythonTypeName(o));
143143

144144
var l = new List<object?>();
145-
IEnumerator i = PythonOps.GetEnumerator(o);
145+
IEnumerator i = PythonOps.GetEnumerator(context, o);
146146
while (i.MoveNext()) {
147147
l.Add(i.Current);
148148
}
@@ -315,8 +315,8 @@ public IEnumerator GetEnumerator() {
315315
}
316316

317317
public object __getnewargs__() {
318-
// Call "new Tuple()" to force result to be a Tuple (otherwise, it could possibly be a Tuple subclass)
319-
return PythonTuple.MakeTuple(new PythonTuple(this));
318+
// Call "MakeTuple()" to force result to be specifically a Tuple (otherwise, it could possibly be a Tuple subclass)
319+
return PythonTuple.MakeTuple(_data);
320320
}
321321

322322
#region IEnumerable<object> Members

0 commit comments

Comments
 (0)