Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita07d402

Browse files
committed
refactoring in CreateSubType
1 parent44a36dc commita07d402

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
objectassembly=null;
312312
objectnamespaceStr=null;
313313

314-
vardisposeList=newList<PyObject>();
315-
try
314+
using(varassemblyKey=newPyString("__assembly__"))
316315
{
317-
varassemblyKey=newPyObject(Converter.ToPython("__assembly__",typeof(string)));
318-
disposeList.Add(assemblyKey);
319-
if(0!=Runtime.PyMapping_HasKey(py_dict,assemblyKey.Handle))
316+
IntPtrassemblyPtr=Runtime.PyDict_GetItem(py_dict,assemblyKey.Handle);
317+
if(assemblyPtr==IntPtr.Zero)returnIntPtr.Zero;
318+
319+
if(!Converter.ToManagedValue(assemblyPtr,typeof(string),outassembly,false))
320320
{
321-
varpyAssembly=newPyObject(Runtime.PyDict_GetItem(py_dict,assemblyKey.Handle));
322-
Runtime.XIncref(pyAssembly.Handle);
323-
disposeList.Add(pyAssembly);
324-
if(!Converter.ToManagedValue(pyAssembly.Handle,typeof(string),outassembly,false))
325-
{
326-
thrownewInvalidCastException("Couldn't convert __assembly__ value to string");
327-
}
321+
returnExceptions.RaiseTypeError("Couldn't convert __assembly__ value to string");
328322
}
329323

330-
varnamespaceKey=newPyObject(Converter.ToPythonImplicit("__namespace__"));
331-
disposeList.Add(namespaceKey);
332-
if(0!=Runtime.PyMapping_HasKey(py_dict,namespaceKey.Handle))
324+
using(varnamespaceKey=newPyString("__namespace__"))
333325
{
334-
varpyNamespace=newPyObject(Runtime.PyDict_GetItem(py_dict,namespaceKey.Handle));
335-
Runtime.XIncref(pyNamespace.Handle);
336-
disposeList.Add(pyNamespace);
337-
if(!Converter.ToManagedValue(pyNamespace.Handle,typeof(string),outnamespaceStr,false))
326+
IntPtrpyNamespace=Runtime.PyDict_GetItem(py_dict,namespaceKey.Handle);
327+
if(pyNamespace==IntPtr.Zero)returnIntPtr.Zero;
328+
329+
if(!Converter.ToManagedValue(pyNamespace,typeof(string),outnamespaceStr,false))
338330
{
339-
thrownewInvalidCastException("Couldn't convert __namespace__ value to string");
331+
returnExceptions.RaiseTypeError("Couldn't convert __namespace__ value to string");
340332
}
341333
}
342334
}
343-
finally
344-
{
345-
foreach(PyObjectoindisposeList)
346-
{
347-
o.Dispose();
348-
}
349-
}
350335

351336
// create the new managed type subclassing the base managed type
352337
varbaseClass=ManagedType.GetManagedObject(py_base_type)asClassBase;
@@ -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
IntPtrcls_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
varcell=newBorrowedReference(Runtime.PyDict_GetItemString(cls_dict,"__classcell__"));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp