Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
TypeFlags is an enum
  • Loading branch information
lostmsu committed Mar 30, 2021
commit 65debff27e8dff68d6f36880331af77712bf3e83
2 changes: 1 addition & 1 deletion src/runtime/clrobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal CLRObject(object ob, IntPtr tp)
System.Diagnostics.Debug.Assert(tp != IntPtr.Zero);
IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);

long flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Subclass) != 0)
{
IntPtr dict = Marshal.ReadIntPtr(py, ObjectOffset.TypeDictOffset(tp));
Expand Down
56 changes: 30 additions & 26 deletions src/runtime/interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,38 +344,42 @@ public static void FreeModuleDef(IntPtr ptr)
/// Note that the two values reserved for stackless have been put
/// to good use as PythonNet specific flags (Managed and Subclass)
/// </summary>
internal class TypeFlags
// Py_TPFLAGS_*
[Flags]
public enum TypeFlags: int
{
public const int HeapType = (1 << 9);
public const int BaseType = (1 << 10);
public const int Ready = (1 << 12);
public const int Readying = (1 << 13);
public const int HaveGC = (1 << 14);
HeapType = (1 << 9),
BaseType = (1 << 10),
Ready = (1 << 12),
Readying = (1 << 13),
HaveGC = (1 << 14),
// 15 and 16 are reserved for stackless
public const int HaveStacklessExtension = 0;
HaveStacklessExtension = 0,
/* XXX Reusing reserved constants */
public const int Managed = (1 << 15); // PythonNet specific
public const int Subclass = (1 << 16); // PythonNet specific
public const int HaveIndex = (1 << 17);
/// <remarks>PythonNet specific</remarks>
Managed = (1 << 15),
/// <remarks>PythonNet specific</remarks>
Subclass = (1 << 16),
HaveIndex = (1 << 17),
/* Objects support nb_index in PyNumberMethods */
public const int HaveVersionTag = (1 << 18);
public const int ValidVersionTag = (1 << 19);
public const int IsAbstract = (1 << 20);
public const int HaveNewBuffer = (1 << 21);
HaveVersionTag = (1 << 18),
ValidVersionTag = (1 << 19),
IsAbstract = (1 << 20),
HaveNewBuffer = (1 << 21),
// TODO: Implement FastSubclass functions
public const int IntSubclass = (1 << 23);
public const int LongSubclass = (1 << 24);
public const int ListSubclass = (1 << 25);
public const int TupleSubclass = (1 << 26);
public const int StringSubclass = (1 << 27);
public const int UnicodeSubclass = (1 << 28);
public const int DictSubclass = (1 << 29);
public const int BaseExceptionSubclass = (1 << 30);
public const int TypeSubclass = (1 << 31);

public const int Default = (
IntSubclass = (1 << 23),
LongSubclass = (1 << 24),
ListSubclass = (1 << 25),
TupleSubclass = (1 << 26),
StringSubclass = (1 << 27),
UnicodeSubclass = (1 << 28),
DictSubclass = (1 << 29),
BaseExceptionSubclass = (1 << 30),
TypeSubclass = (1 << 31),

Default = (
HaveStacklessExtension |
HaveVersionTag);
HaveVersionTag),
}


Expand Down
6 changes: 3 additions & 3 deletions src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ internal static ManagedType GetManagedObject(IntPtr ob)
tp = ob;
}

var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Managed) != 0)
{
IntPtr op = tp == ob
Expand All @@ -117,7 +117,7 @@ internal static ManagedType GetManagedObjectType(IntPtr ob)
if (ob != IntPtr.Zero)
{
IntPtr tp = Runtime.PyObject_TYPE(ob);
var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Managed) != 0)
{
tp = Marshal.ReadIntPtr(tp, TypeOffset.magic());
Expand Down Expand Up @@ -152,7 +152,7 @@ internal static bool IsManagedType(IntPtr ob)
tp = ob;
}

var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Managed) != 0)
{
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/metatype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
return IntPtr.Zero;
}

int flags = TypeFlags.Default;
var flags = TypeFlags.Default;
flags |= TypeFlags.Managed;
flags |= TypeFlags.HeapType;
flags |= TypeFlags.BaseType;
flags |= TypeFlags.Subclass;
flags |= TypeFlags.HaveGC;
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);

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

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

var flags = Util.ReadCLong(tp, TypeOffset.tp_flags);
var flags = (TypeFlags)Util.ReadCLong(tp, TypeOffset.tp_flags);
if ((flags & TypeFlags.Subclass) == 0)
{
IntPtr gc = Marshal.ReadIntPtr(tp, TypeOffset.magic());
Expand Down
16 changes: 8 additions & 8 deletions src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ internal static IntPtr CreateType(Type impl)
SlotsHolder slotsHolder = CreateSolotsHolder(type);
InitializeSlots(type, impl, slotsHolder);

int flags = TypeFlags.Default | TypeFlags.Managed |
var flags = TypeFlags.Default | TypeFlags.Managed |
TypeFlags.HeapType | TypeFlags.HaveGC;
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);

if (Runtime.PyType_Ready(type) != 0)
{
Expand Down Expand Up @@ -286,12 +286,12 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType)
Runtime.XIncref(base_);
}

const int flags = TypeFlags.Default
const TypeFlags flags = TypeFlags.Default
| TypeFlags.Managed
| TypeFlags.HeapType
| TypeFlags.BaseType
| TypeFlags.HaveGC;
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);

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

const int flags = TypeFlags.Default
const TypeFlags flags = TypeFlags.Default
| TypeFlags.Managed
| TypeFlags.HeapType
| TypeFlags.HaveGC;
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);

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

int flags = TypeFlags.Default;
var flags = TypeFlags.Default;
flags |= TypeFlags.Managed;
flags |= TypeFlags.HeapType;
flags |= TypeFlags.HaveGC;
Util.WriteCLong(type, TypeOffset.tp_flags, flags);
Util.WriteCLong(type, TypeOffset.tp_flags, (int)flags);

CopySlot(base_, type, TypeOffset.tp_traverse);
CopySlot(base_, type, TypeOffset.tp_clear);
Expand Down