Skip to content

Commit a07d402

Browse files
committed
refactoring in CreateSubType
1 parent 44a36dc commit a07d402

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/runtime/typemanager.cs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -311,42 +311,27 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
311311
object assembly = null;
312312
object namespaceStr = null;
313313

314-
var disposeList = new List<PyObject>();
315-
try
314+
using (var assemblyKey = new PyString("__assembly__"))
316315
{
317-
var assemblyKey = new PyObject(Converter.ToPython("__assembly__", typeof(string)));
318-
disposeList.Add(assemblyKey);
319-
if (0 != Runtime.PyMapping_HasKey(py_dict, assemblyKey.Handle))
316+
IntPtr assemblyPtr = Runtime.PyDict_GetItem(py_dict, assemblyKey.Handle);
317+
if (assemblyPtr == IntPtr.Zero) return IntPtr.Zero;
318+
319+
if (!Converter.ToManagedValue(assemblyPtr, typeof(string), out assembly, false))
320320
{
321-
var pyAssembly = new PyObject(Runtime.PyDict_GetItem(py_dict, assemblyKey.Handle));
322-
Runtime.XIncref(pyAssembly.Handle);
323-
disposeList.Add(pyAssembly);
324-
if (!Converter.ToManagedValue(pyAssembly.Handle, typeof(string), out assembly, false))
325-
{
326-
throw new InvalidCastException("Couldn't convert __assembly__ value to string");
327-
}
321+
return Exceptions.RaiseTypeError("Couldn't convert __assembly__ value to string");
328322
}
329323

330-
var namespaceKey = new PyObject(Converter.ToPythonImplicit("__namespace__"));
331-
disposeList.Add(namespaceKey);
332-
if (0 != Runtime.PyMapping_HasKey(py_dict, namespaceKey.Handle))
324+
using (var namespaceKey = new PyString("__namespace__"))
333325
{
334-
var pyNamespace = new PyObject(Runtime.PyDict_GetItem(py_dict, namespaceKey.Handle));
335-
Runtime.XIncref(pyNamespace.Handle);
336-
disposeList.Add(pyNamespace);
337-
if (!Converter.ToManagedValue(pyNamespace.Handle, typeof(string), out namespaceStr, false))
326+
IntPtr pyNamespace = Runtime.PyDict_GetItem(py_dict, namespaceKey.Handle);
327+
if (pyNamespace == IntPtr.Zero) return IntPtr.Zero;
328+
329+
if (!Converter.ToManagedValue(pyNamespace, typeof(string), out namespaceStr, false))
338330
{
339-
throw new InvalidCastException("Couldn't convert __namespace__ value to string");
331+
return Exceptions.RaiseTypeError("Couldn't convert __namespace__ value to string");
340332
}
341333
}
342334
}
343-
finally
344-
{
345-
foreach (PyObject o in disposeList)
346-
{
347-
o.Dispose();
348-
}
349-
}
350335

351336
// create the new managed type subclassing the base managed type
352337
var baseClass = ManagedType.GetManagedObject(py_base_type) as ClassBase;
@@ -370,7 +355,7 @@ internal static IntPtr CreateSubType(IntPtr py_name, IntPtr py_base_type, IntPtr
370355
// by default the class dict will have all the C# methods in it, but as this is a
371356
// derived class we want the python overrides in there instead if they exist.
372357
IntPtr cls_dict = Marshal.ReadIntPtr(py_type, TypeOffset.tp_dict);
373-
Runtime.PyDict_Update(cls_dict, py_dict);
358+
PythonException.ThrowIfIsNotZero(Runtime.PyDict_Update(cls_dict, py_dict));
374359
Runtime.XIncref(py_type);
375360
// Update the __classcell__ if it exists
376361
var cell = new BorrowedReference(Runtime.PyDict_GetItemString(cls_dict, "__classcell__"));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy