Skip to content

Commit ee4c364

Browse files
authored
Use TypeCache where appropriate (#1450)
1 parent 1fec6a5 commit ee4c364

6 files changed

Lines changed: 22 additions & 22 deletions

File tree

Src/IronPython/Modules/Builtin.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ public static string bin(object? number) {
131131
}
132132
}
133133

134-
public static PythonType @bool => DynamicHelpers.GetPythonTypeFromType(typeof(bool));
134+
public static PythonType @bool => TypeCache.Boolean;
135135

136-
public static PythonType bytes => DynamicHelpers.GetPythonTypeFromType(typeof(Bytes));
136+
public static PythonType bytes => TypeCache.Bytes;
137137

138138
public static PythonType bytearray => DynamicHelpers.GetPythonTypeFromType(typeof(ByteArray));
139139

@@ -237,13 +237,13 @@ private static object CompileHelper(CodeContext/*!*/ context, SourceUnit sourceU
237237
(object)_ast.BuildAst(context, sourceUnit, opts, mode);
238238
}
239239

240-
public static PythonType complex => DynamicHelpers.GetPythonTypeFromType(typeof(Complex));
240+
public static PythonType complex => TypeCache.Complex;
241241

242242
public static void delattr(CodeContext/*!*/ context, object? o, [NotNone] string name) {
243243
PythonOps.DeleteAttr(context, o, name);
244244
}
245245

246-
public static PythonType dict => DynamicHelpers.GetPythonTypeFromType(typeof(PythonDictionary));
246+
public static PythonType dict => TypeCache.Dict;
247247

248248
public static PythonList dir(CodeContext/*!*/ context) {
249249
PythonList res = new PythonList(context.Dict.Keys);
@@ -263,7 +263,7 @@ public static object divmod(CodeContext/*!*/ context, object? x, object? y) {
263263
return context.LanguageContext.DivMod(x, y);
264264
}
265265

266-
public static PythonType enumerate => DynamicHelpers.GetPythonTypeFromType(typeof(Enumerate));
266+
public static PythonType enumerate => TypeCache.Enumerate;
267267

268268
internal static PythonDictionary? GetAttrLocals(CodeContext/*!*/ context, object? locals) {
269269
PythonDictionary? attrLocals = null;
@@ -380,12 +380,12 @@ public static void exec(CodeContext/*!*/ context, [NotNone] IBufferProtocol code
380380

381381
public static PythonType filter => DynamicHelpers.GetPythonTypeFromType(typeof(Filter));
382382

383-
public static PythonType @float => DynamicHelpers.GetPythonTypeFromType(typeof(double));
383+
public static PythonType @float => TypeCache.Double;
384384

385385
public static string format(CodeContext/*!*/ context, object? argValue, [NotNone] string formatSpec = "")
386386
=> PythonOps.Format(context, argValue, formatSpec);
387387

388-
public static PythonType frozenset => DynamicHelpers.GetPythonTypeFromType(typeof(FrozenSetCollection));
388+
public static PythonType frozenset => TypeCache.FrozenSet;
389389

390390
public static object? getattr(CodeContext/*!*/ context, object? o, [NotNone] string name) {
391391
return PythonOps.GetBoundAttr(context, o, name);
@@ -633,7 +633,7 @@ public static object id(object? o) {
633633
return (BigInteger)res;
634634
}
635635

636-
public static PythonType @int => DynamicHelpers.GetPythonTypeFromType(typeof(BigInteger));
636+
public static PythonType @int => TypeCache.BigInteger;
637637

638638
public static bool isinstance(object? o, [NotNone] PythonType typeinfo) {
639639
return PythonOps.IsInstance(o, typeinfo);
@@ -742,9 +742,9 @@ public static object len(object? o) {
742742
return PythonOps.Length(o, out int res, out BigInteger bigRes) ? (object)res : bigRes;
743743
}
744744

745-
public static PythonType set => DynamicHelpers.GetPythonTypeFromType(typeof(SetCollection));
745+
public static PythonType set => TypeCache.Set;
746746

747-
public static PythonType list => DynamicHelpers.GetPythonTypeFromType(typeof(PythonList));
747+
public static PythonType list => TypeCache.PythonList;
748748

749749
public static object locals(CodeContext/*!*/ context) {
750750
PythonDictionary dict = context.Dict;
@@ -1075,7 +1075,7 @@ public static object locals(CodeContext/*!*/ context) {
10751075
}
10761076
}
10771077

1078-
public static PythonType @object => DynamicHelpers.GetPythonTypeFromType(typeof(object));
1078+
public static PythonType @object => TypeCache.Object;
10791079

10801080
public static string oct(object? number) {
10811081
object index = PythonOps.Index(number);
@@ -1564,13 +1564,13 @@ public object? CurrentValue {
15641564

15651565
#endregion
15661566

1567-
public static PythonType super => DynamicHelpers.GetPythonTypeFromType(typeof(Super));
1567+
public static PythonType super => TypeCache.Super;
15681568

1569-
public static PythonType str => DynamicHelpers.GetPythonTypeFromType(typeof(string));
1569+
public static PythonType str => TypeCache.String;
15701570

1571-
public static PythonType tuple => DynamicHelpers.GetPythonTypeFromType(typeof(PythonTuple));
1571+
public static PythonType tuple => TypeCache.PythonTuple;
15721572

1573-
public static PythonType type => DynamicHelpers.GetPythonTypeFromType(typeof(PythonType));
1573+
public static PythonType type => TypeCache.PythonType;
15741574

15751575
[Documentation("vars([object]) -> dictionary\n\nWithout arguments, equivalent to locals().\nWith an argument, equivalent to object.__dict__.")]
15761576
public static object vars(CodeContext/*!*/ context) {
@@ -1587,7 +1587,7 @@ public static object vars(CodeContext/*!*/ context) {
15871587

15881588
public static PythonType zip => DynamicHelpers.GetPythonTypeFromType(typeof(Zip));
15891589

1590-
public static PythonType BaseException => DynamicHelpers.GetPythonTypeFromType(typeof(PythonExceptions.BaseException));
1590+
public static PythonType BaseException => TypeCache.BaseException;
15911591

15921592
// OSError aliases
15931593
public static PythonType EnvironmentError => PythonExceptions.OSError;

Src/IronPython/Runtime/Descriptors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public object __get__(object instance, object owner = null) {
9999

100100
if (instance is int) {
101101
// IronPython uses Int32 objects as "int" for performance reasons (GH #52)
102-
owner = DynamicHelpers.GetPythonTypeFromType(typeof(System.Numerics.BigInteger));
102+
owner = TypeCache.BigInteger;
103103
} else {
104104
owner = DynamicHelpers.GetPythonType(instance);
105105
}

Src/IronPython/Runtime/Operations/ObjectOps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static object __new__(CodeContext/*!*/ context, PythonType cls, [ParamDic
136136
/// Runs the pickle protocol
137137
/// </summary>
138138
public static object? __reduce_ex__(CodeContext/*!*/ context, object self, object protocol) {
139-
object objectReduce = PythonOps.GetBoundAttr(context, DynamicHelpers.GetPythonTypeFromType(typeof(object)), "__reduce__");
139+
object objectReduce = PythonOps.GetBoundAttr(context, TypeCache.Object, "__reduce__");
140140
if (PythonOps.TryGetBoundAttr(context, DynamicHelpers.GetPythonType(self), "__reduce__", out object? myReduce)) {
141141
if (!PythonOps.IsRetBool(myReduce, objectReduce)) {
142142
// A derived class overrode __reduce__ but not __reduce_ex__, so call

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ public static object MakeClass(FunctionCode funcCode, Func<CodeContext, CodeCont
14501450
}
14511451
// this makes sure that object is a base
14521452
if (bases.Count == 0) {
1453-
bases = PythonTuple.MakeTuple(DynamicHelpers.GetPythonTypeFromType(typeof(object)));
1453+
bases = PythonTuple.MakeTuple(TypeCache.Object);
14541454
}
14551455
PythonDictionary vars = func(parentContext).Dict;
14561456
return PythonType.__new__(parentContext, TypeCache.PythonType, name, bases, vars, selfNames);
@@ -1479,7 +1479,7 @@ public static object MakeClass(FunctionCode funcCode, Func<CodeContext, CodeCont
14791479

14801480
// Ensure the class derives from `object`
14811481
if (obj is PythonType newType && newType.BaseTypes.Count == 0) {
1482-
newType.BaseTypes.Add(DynamicHelpers.GetPythonTypeFromType(typeof(object)));
1482+
newType.BaseTypes.Add(TypeCache.Object);
14831483
}
14841484

14851485
return obj;

Src/IronPython/Runtime/Types/EmptyType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public sealed class NoneTypeOps {
8787

8888
[StaticExtensionMethod]
8989
public static object? __new__(CodeContext/*!*/ context, [NotNone] PythonType cls) {
90-
if (cls == DynamicHelpers.GetPythonTypeFromType(typeof(DynamicNull)))
90+
if (cls == TypeCache.Null)
9191
return null;
9292
throw PythonOps.TypeError("{0} is not a subtype of NoneType", cls.Name);
9393
}

Src/IronPython/Runtime/Types/ReflectedGetterSetter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public virtual PythonType PropertyType {
9393
if (Getter != null && Getter.Length > 0) {
9494
return DynamicHelpers.GetPythonTypeFromType(Getter[0].ReturnType);
9595
}
96-
return DynamicHelpers.GetPythonTypeFromType(typeof(object));
96+
return TypeCache.Object;
9797
}
9898
}
9999

0 commit comments

Comments
 (0)