Skip to content

Commit dc9a4d8

Browse files
committed
TypeFlags is an enum
1 parent 6f1219f commit dc9a4d8

File tree

5 files changed

+45
-41
lines changed

5 files changed

+45
-41
lines changed

src/runtime/clrobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal CLRObject(object ob, IntPtr tp)
1414
System.Diagnostics.Debug.Assert(tp != IntPtr.Zero);
1515
IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);
1616

17-
long flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
17+
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
1818
if ((flags & TypeFlags.Subclass) != 0)
1919
{
2020
IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.TypeDictOffset(tp));

src/runtime/interop.cs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,38 +344,42 @@ public static void FreeModuleDef(IntPtr ptr)
344344
/// Note that the two values reserved for stackless have been put
345345
/// to good use as PythonNet specific flags (Managed and Subclass)
346346
/// </summary>
347-
internal class TypeFlags
347+
// Py_TPFLAGS_*
348+
[Flags]
349+
public enum TypeFlags: int
348350
{
349-
public const int HeapType = (1 << 9);
350-
public const int BaseType = (1 << 10);
351-
public const int Ready = (1 << 12);
352-
public const int Readying = (1 << 13);
353-
public const int HaveGC = (1 << 14);
351+
HeapType = (1 << 9),
352+
BaseType = (1 << 10),
353+
Ready = (1 << 12),
354+
Readying = (1 << 13),
355+
HaveGC = (1 << 14),
354356
// 15 and 16 are reserved for stackless
355-
public const int HaveStacklessExtension = 0;
357+
HaveStacklessExtension = 0,
356358
/* XXX Reusing reserved constants */
357-
public const int Managed = (1 << 15); // PythonNet specific
358-
public const int Subclass = (1 << 16); // PythonNet specific
359-
public const int HaveIndex = (1 << 17);
359+
/// <remarks>PythonNet specific</remarks>
360+
Managed = (1 << 15),
361+
/// <remarks>PythonNet specific</remarks>
362+
Subclass = (1 << 16),
363+
HaveIndex = (1 << 17),
360364
/* Objects support nb_index in PyNumberMethods */
361-
public const int HaveVersionTag = (1 << 18);
362-
public const int ValidVersionTag = (1 << 19);
363-
public const int IsAbstract = (1 << 20);
364-
public const int HaveNewBuffer = (1 << 21);
365+
HaveVersionTag = (1 << 18),
366+
ValidVersionTag = (1 << 19),
367+
IsAbstract = (1 << 20),
368+
HaveNewBuffer = (1 << 21),
365369
// TODO: Implement FastSubclass functions
366-
public const int IntSubclass = (1 << 23);
367-
public const int LongSubclass = (1 << 24);
368-
public const int ListSubclass = (1 << 25);
369-
public const int TupleSubclass = (1 << 26);
370-
public const int StringSubclass = (1 << 27);
371-
public const int UnicodeSubclass = (1 << 28);
372-
public const int DictSubclass = (1 << 29);
373-
public const int BaseExceptionSubclass = (1 << 30);
374-
public const int TypeSubclass = (1 << 31);
375-
376-
public const int Default = (
370+
IntSubclass = (1 << 23),
371+
LongSubclass = (1 << 24),
372+
ListSubclass = (1 << 25),
373+
TupleSubclass = (1 << 26),
374+
StringSubclass = (1 << 27),
375+
UnicodeSubclass = (1 << 28),
376+
DictSubclass = (1 << 29),
377+
BaseExceptionSubclass = (1 << 30),
378+
TypeSubclass = (1 << 31),
379+
380+
Default = (
377381
HaveStacklessExtension |
378-
HaveVersionTag);
382+
HaveVersionTag),
379383
}
380384

381385

src/runtime/managedtype.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal static ManagedType GetManagedObject(IntPtr ob)
9292
tp = ob;
9393
}
9494

95-
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
95+
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
9696
if ((flags & TypeFlags.Managed) != 0)
9797
{
9898
IntPtr op = tp == ob
@@ -117,7 +117,7 @@ internal static ManagedType GetManagedObjectType(IntPtr ob)
117117
if (ob != IntPtr.Zero)
118118
{
119119
IntPtr tp = Runtime.PyObject_TYPE(ob);
120-
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
120+
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
121121
if ((flags & TypeFlags.Managed) != 0)
122122
{
123123
tp = Marshal.ReadIntPtr(tp, TypeOffset.magic());
@@ -152,7 +152,7 @@ internal static bool IsManagedType(IntPtr ob)
152152
tp = ob;
153153
}
154154

155-
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
155+
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
156156
if ((flags & TypeFlags.Managed) != 0)
157157
{
158158
return true;

src/runtime/metatype.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
147147
return IntPtr.Zero;
148148
}
149149

150-
int flags = TypeFlags.Default;
150+
var flags = TypeFlags.Default;
151151
flags |= TypeFlags.Managed;
152152
flags |= TypeFlags.HeapType;
153153
flags |= TypeFlags.BaseType;
154154
flags |= TypeFlags.Subclass;
155155
flags |= TypeFlags.HaveGC;
156-
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
156+
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);
157157

158158
TypeManager.CopySlot(base_type, type, TypeOffset.tp_dealloc);
159159

@@ -285,7 +285,7 @@ public static void tp_dealloc(IntPtr tp)
285285
{
286286
// Fix this when we dont cheat on the handle for subclasses!
287287

288-
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
288+
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
289289
if ((flags & TypeFlags.Subclass) == 0)
290290
{
291291
IntPtr gc = Marshal.ReadIntPtr(tp, TypeOffset.magic());

src/runtime/typemanager.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ internal static IntPtr CreateType(Type impl)
171171
SlotsHolder slotsHolder = CreateSolotsHolder(type);
172172
InitializeSlots(type, impl, slotsHolder);
173173

174-
int flags = TypeFlags.Default | TypeFlags.Managed |
174+
var flags = TypeFlags.Default | TypeFlags.Managed |
175175
TypeFlags.HeapType | TypeFlags.HaveGC;
176-
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
176+
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);
177177

178178
if (Runtime.PyType_Ready(type) != 0)
179179
{
@@ -286,12 +286,12 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
286286
Runtime.XIncref(base_);
287287
}
288288

289-
const int flags = TypeFlags.Default
289+
const TypeFlags flags = TypeFlags.Default
290290
| TypeFlags.Managed
291291
| TypeFlags.HeapType
292292
| TypeFlags.BaseType
293293
| TypeFlags.HaveGC;
294-
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
294+
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);
295295

296296
OperatorMethod.FixupSlots(type, clrType);
297297
// Leverage followup initialization from the Python runtime. Note
@@ -457,11 +457,11 @@ internal static IntPtr CreateMetaType(Type impl, out SlotsHolder slotsHolder)
457457
int size = TypeOffset.magic() + IntPtr.Size;
458458
Marshal.WriteIntPtr(type, TypeOffset.tp_basicsize, new IntPtr(size));
459459

460-
const int flags = TypeFlags.Default
460+
const TypeFlags flags = TypeFlags.Default
461461
| TypeFlags.Managed
462462
| TypeFlags.HeapType
463463
| TypeFlags.HaveGC;
464-
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
464+
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);
465465

466466
// Slots will inherit from TypeType, it's not neccesary for setting them.
467467
// Inheried slots:
@@ -562,11 +562,11 @@ internal static IntPtr BasicSubType(string name, IntPtr base_, Type impl)
562562
Marshal.WriteIntPtr(type, TypeOffset.tp_base, base_);
563563
Runtime.XIncref(base_);
564564

565-
int flags = TypeFlags.Default;
565+
var flags = TypeFlags.Default;
566566
flags |= TypeFlags.Managed;
567567
flags |= TypeFlags.HeapType;
568568
flags |= TypeFlags.HaveGC;
569-
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
569+
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);
570570

571571
CopySlot(base_, type, TypeOffset.tp_traverse);
572572
CopySlot(base_, type, TypeOffset.tp_clear);

0 commit comments

Comments
 (0)