implemented non-confusing PyModule_AddObject

This commit is contained in:
Victor Nova 2021-10-18 13:56:36 -07:00
Родитель 2ac952a969
Коммит 7adf98a8a0
2 изменённых файлов: 12 добавлений и 11 удалений

Просмотреть файл

@ -127,7 +127,7 @@ class DotNetFinder(importlib.abc.MetaPathFinder):
Runtime.PyTuple_SetItem(args.Borrow(), 1, mod_dict); Runtime.PyTuple_SetItem(args.Borrow(), 1, mod_dict);
Runtime.PyObject_Call(exec, args.Borrow(), default).Dispose(); Runtime.PyObject_Call(exec, args.Borrow(), default).Dispose();
// Set as a sub-module of clr. // Set as a sub-module of clr.
if(Runtime.PyModule_AddObject(ClrModuleReference, "loader", import_hook_module) != 0) if(Runtime.PyModule_AddObject(ClrModuleReference, "loader", import_hook_module.Steal()) != 0)
{ {
throw PythonException.ThrowLastAsClrException(); throw PythonException.ThrowLastAsClrException();
} }

Просмотреть файл

@ -1612,21 +1612,22 @@ namespace Python.Runtime
internal static NewReference PyImport_Import(BorrowedReference name) => Delegates.PyImport_Import(name); internal static NewReference PyImport_Import(BorrowedReference name) => Delegates.PyImport_Import(name);
/// <summary>
/// We can't use a StolenReference here because the reference is stolen only on success.
/// </summary>
/// <param name="module">The module to add the object to.</param> /// <param name="module">The module to add the object to.</param>
/// <param name="name">The key that will refer to the object.</param> /// <param name="name">The key that will refer to the object.</param>
/// <param name="stolenObject"> /// <param name="value">The object to add to the module.</param>
/// The object to add to the module. The reference will be stolen only if the
/// method returns 0.
/// </param>
/// <returns>Return -1 on error, 0 on success.</returns> /// <returns>Return -1 on error, 0 on success.</returns>
[Obsolete("Make two overloads for regular and stolen references")] internal static int PyModule_AddObject(BorrowedReference module, string name, StolenReference value)
internal static int PyModule_AddObject(BorrowedReference module, string name, IntPtr stolenObject)
{ {
using var namePtr = new StrPtr(name, Encoding.UTF8); using var namePtr = new StrPtr(name, Encoding.UTF8);
return Delegates.PyModule_AddObject(module, namePtr, stolenObject); IntPtr valueAddr = value.DangerousGetAddressOrNull();
int res = Delegates.PyModule_AddObject(module, namePtr, valueAddr);
// We can't just exit here because the reference is stolen only on success.
if (res != 0)
{
XDecref(StolenReference.TakeNullable(ref valueAddr));
}
return res;
} }
/// <summary> /// <summary>