Skip to content

Commit 9764b25

Browse files
committed
switched typemanager.cs to the new style references
1 parent 178a359 commit 9764b25

File tree

8 files changed

+132
-135
lines changed

8 files changed

+132
-135
lines changed

src/runtime/NewReference.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public NewReference(BorrowedReference reference, bool canBeNull = false)
2424
this.pointer = address;
2525
}
2626

27+
/// <summary>Creates a <see cref="NewReference"/> pointing to the same object</summary>
28+
public NewReference(in NewReference reference, bool canBeNull = false)
29+
: this(reference.BorrowNullable(), canBeNull) { }
30+
2731
/// <summary>
2832
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
2933
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
@@ -32,9 +36,7 @@ public PyObject MoveToPyObject()
3236
{
3337
if (this.IsNull()) throw new NullReferenceException();
3438

35-
var result = new PyObject(this.pointer);
36-
this.pointer = IntPtr.Zero;
37-
return result;
39+
return new PyObject(this.StealNullable());
3840
}
3941

4042
/// <summary>Moves ownership of this instance to unmanged pointer</summary>

src/runtime/arrayobject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,13 @@ static IntPtr AllocateBufferProcs()
518518
/// <summary>
519519
/// <see cref="TypeManager.InitializeSlots(IntPtr, Type, SlotsHolder)"/>
520520
/// </summary>
521-
public static void InitializeSlots(IntPtr type, ISet<string> initialized, SlotsHolder slotsHolder)
521+
public static void InitializeSlots(PyType type, ISet<string> initialized, SlotsHolder slotsHolder)
522522
{
523523
if (initialized.Add(nameof(TypeOffset.tp_as_buffer)))
524524
{
525525
// TODO: only for unmanaged arrays
526526
int offset = TypeOffset.GetSlotOffset(nameof(TypeOffset.tp_as_buffer));
527-
Marshal.WriteIntPtr(type, offset, BufferProcsAddress);
527+
Util.WriteIntPtr(type, offset, BufferProcsAddress);
528528
}
529529
}
530530
}

src/runtime/classderived.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, strin
674674
var pyargs = new PyObject[args.Length];
675675
for (var i = 0; i < args.Length; ++i)
676676
{
677-
pyargs[i] = new PyObject(Converter.ToPythonImplicit(args[i]).Steal());
677+
pyargs[i] = Converter.ToPythonImplicit(args[i]).MoveToPyObject();
678678
disposeList.Add(pyargs[i]);
679679
}
680680

@@ -735,7 +735,7 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
735735
var pyargs = new PyObject[args.Length];
736736
for (var i = 0; i < args.Length; ++i)
737737
{
738-
pyargs[i] = new PyObject(Converter.ToPythonImplicit(args[i]).Steal());
738+
pyargs[i] = Converter.ToPythonImplicit(args[i]).MoveToPyObject();
739739
disposeList.Add(pyargs[i]);
740740
}
741741

@@ -806,7 +806,7 @@ public static void InvokeSetProperty<T>(IPythonDerivedType obj, string propertyN
806806
try
807807
{
808808
using var pyself = new PyObject(self.ObjectReference);
809-
using var pyvalue = new PyObject(Converter.ToPythonImplicit(value).Steal());
809+
using var pyvalue = Converter.ToPythonImplicit(value).MoveToPyObject();
810810
pyself.SetAttr(propertyName, pyvalue);
811811
}
812812
finally

src/runtime/exceptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ internal static NewReference RaiseTypeError(string message)
392392
typeError.Normalize();
393393

394394
Runtime.PyException_SetCause(
395-
typeError.Value!.Reference,
396-
new NewReference(cause.Value!.Reference).Steal());
395+
typeError.Value,
396+
new NewReference(cause.Value).Steal());
397397
typeError.Restore();
398398

399399
return default;

src/runtime/metatype.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal class MetaType : ManagedType
2323
/// <summary>
2424
/// Metatype initialization. This bootstraps the CLR metatype to life.
2525
/// </summary>
26-
public static PyObject Initialize()
26+
public static PyType Initialize()
2727
{
2828
PyCLRMetaType = TypeManager.CreateMetaType(typeof(MetaType), out _metaSlotsHodler);
2929
return PyCLRMetaType;

src/runtime/operatormethod.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ public static bool IsComparisonOp(MethodInfo method)
9595
/// For the operator methods of a CLR type, set the special slots of the
9696
/// corresponding Python type's operator methods.
9797
/// </summary>
98-
/// <param name="pyType"></param>
99-
/// <param name="clrType"></param>
100-
public static void FixupSlots(IntPtr pyType, Type clrType)
98+
public static void FixupSlots(BorrowedReference pyType, Type clrType)
10199
{
102100
const BindingFlags flags = BindingFlags.Public | BindingFlags.Static;
103101
Debug.Assert(_opType != null);
@@ -117,12 +115,12 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
117115
int offset = OpMethodMap[method.Name].TypeOffset;
118116
// Copy the default implementation of e.g. the nb_add slot,
119117
// which simply calls __add__ on the type.
120-
IntPtr func = Marshal.ReadIntPtr(_opType.Handle, offset);
118+
IntPtr func = Util.ReadIntPtr(_opType, offset);
121119
// Write the slot definition of the target Python type, so
122120
// that we can later modify __add___ and it will be called
123121
// when used with a Python operator.
124122
// https://tenthousandmeters.com/blog/python-behind-the-scenes-6-how-python-object-system-works/
125-
Marshal.WriteIntPtr(pyType, offset, func);
123+
Util.WriteIntPtr(pyType, offset, func);
126124
}
127125
}
128126

src/runtime/runtime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ private static void MoveClrInstancesOnwershipToPython()
503503
internal static PyObject PyBaseObjectType;
504504
internal static PyObject PyModuleType;
505505
internal static PyObject PySuper_Type;
506-
internal static PyObject PyCLRMetaType;
506+
internal static PyType PyCLRMetaType;
507507
internal static PyObject PyMethodType;
508508
internal static PyObject PyWrapperDescriptorType;
509509

@@ -516,7 +516,7 @@ private static void MoveClrInstancesOnwershipToPython()
516516
internal static PyObject PyFloatType;
517517
internal static PyObject PyBoolType;
518518
internal static PyObject PyNoneType;
519-
internal static PyObject PyTypeType;
519+
internal static PyType PyTypeType;
520520

521521
internal static int* Py_NoSiteFlag;
522522

0 commit comments

Comments
 (0)