| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,7 @@ when .NET expects an integer [#1342][i1342] | |||
| 36 | 36 | - BREAKING: Methods with `ref` or `out` parameters and void return type return a tuple of only the `ref` and `out` parameters. | |
| 37 | 37 | - BREAKING: to call Python from .NET `Runtime.PythonDLL` property must be set to Python DLL name | |
| 38 | 38 | or the DLL must be loaded in advance. This must be done before calling any other Python.NET functions. | |
| 39 | + - BREAKING: `PyObject.Length()` now raises a `PythonException` when object does not support a concept of length. | ||
| 39 | 40 | - Sign Runtime DLL with a strong name | |
| 40 | 41 | - Implement loading through `clr_loader` instead of the included `ClrModule`, enables | |
| 41 | 42 | support for .NET Core | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,7 +34,9 @@ public void SetUp() | |||
| 34 | 34 | TestContext.Out.WriteLine(testPath); | |
| 35 | 35 | ||
| 36 | 36 | IntPtr str = Runtime.Runtime.PyString_FromString(testPath); | |
| 37 | + Assert.IsFalse(str == IntPtr.Zero); | ||
| 37 | 38 | BorrowedReference path = Runtime.Runtime.PySys_GetObject("path"); | |
| 39 | + Assert.IsFalse(path.IsNull); | ||
| 38 | 40 | Runtime.Runtime.PyList_Append(path, str); | |
| 39 | 41 | Runtime.Runtime.XDecref(str); | |
| 40 | 42 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,6 +61,13 @@ static void RestoreImport() | |||
| 61 | 61 | { | |
| 62 | 62 | IntPtr builtins = Runtime.GetBuiltins(); | |
| 63 | 63 | ||
| 64 | + IntPtr existing = Runtime.PyObject_GetAttr(builtins, PyIdentifier.__import__); | ||
| 65 | + Runtime.XDecref(existing); | ||
| 66 | + if (existing != hook.ptr) | ||
| 67 | + { | ||
| 68 | + throw new NotSupportedException("Unable to restore original __import__."); | ||
| 69 | + } | ||
| 70 | + | ||
| 64 | 71 | int res = Runtime.PyObject_SetAttr(builtins, PyIdentifier.__import__, py_import); | |
| 65 | 72 | PythonException.ThrowIfIsNotZero(res); | |
| 66 | 73 | Runtime.XDecref(py_import); | |
@@ -88,7 +95,7 @@ internal static unsafe void Initialize() | |||
| 88 | 95 | ||
| 89 | 96 | // both dicts are borrowed references | |
| 90 | 97 | BorrowedReference mod_dict = Runtime.PyModule_GetDict(ClrModuleReference); | |
| 91 | - BorrowedReference clr_dict = *Runtime._PyObject_GetDictPtr(root.ObjectReference); | ||
| 98 | + using var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference); | ||
| 92 | 99 | ||
| 93 | 100 | Runtime.PyDict_Update(mod_dict, clr_dict); | |
| 94 | 101 | BorrowedReference dict = Runtime.PyImport_GetModuleDict(); | |
@@ -150,8 +157,10 @@ public static unsafe NewReference GetCLRModule(BorrowedReference fromList = defa | |||
| 150 | 157 | // update the module dictionary with the contents of the root dictionary | |
| 151 | 158 | root.LoadNames(); | |
| 152 | 159 | BorrowedReference py_mod_dict = Runtime.PyModule_GetDict(ClrModuleReference); | |
| 153 | - BorrowedReference clr_dict = *Runtime._PyObject_GetDictPtr(root.ObjectReference); | ||
| 154 | - Runtime.PyDict_Update(py_mod_dict, clr_dict); | ||
| 160 | + using (var clr_dict = Runtime.PyObject_GenericGetDict(root.ObjectReference)) | ||
| 161 | + { | ||
| 162 | + Runtime.PyDict_Update(py_mod_dict, clr_dict); | ||
| 163 | + } | ||
| 155 | 164 | ||
| 156 | 165 | // find any items from the from list and get them from the root if they're not | |
| 157 | 166 | // already in the module dictionary | |
@@ -250,7 +259,6 @@ public static IntPtr __import__(IntPtr self, IntPtr argsRaw, IntPtr kw) | |||
| 250 | 259 | } | |
| 251 | 260 | ||
| 252 | 261 | string realname = mod_name; | |
| 253 | - string clr_prefix = null; | ||
| 254 | 262 | ||
| 255 | 263 | // 2010-08-15: Always seemed smart to let python try first... | |
| 256 | 264 | // This shaves off a few tenths of a second on test_module.py | |
@@ -308,10 +316,7 @@ public static IntPtr __import__(IntPtr self, IntPtr argsRaw, IntPtr kw) | |||
| 308 | 316 | } | |
| 309 | 317 | return new NewReference(module).DangerousMoveToPointer(); | |
| 310 | 318 | } | |
| 311 | - if (clr_prefix != null) | ||
| 312 | - { | ||
| 313 | - return GetCLRModule(fromList).DangerousMoveToPointerOrNull(); | ||
| 314 | - } | ||
| 319 | + | ||
| 315 | 320 | module = Runtime.PyDict_GetItemString(modules, names[0]); | |
| 316 | 321 | return new NewReference(module, canBeNull: true).DangerousMoveToPointer(); | |
| 317 | 322 | } | |
@@ -351,12 +356,6 @@ public static IntPtr __import__(IntPtr self, IntPtr argsRaw, IntPtr kw) | |||
| 351 | 356 | ||
| 352 | 357 | // Add the module to sys.modules | |
| 353 | 358 | Runtime.PyDict_SetItemString(modules, tail.moduleName, tail.ObjectReference); | |
| 354 | - | ||
| 355 | - // If imported from CLR add clr.<modulename> to sys.modules as well | ||
| 356 | - if (clr_prefix != null) | ||
| 357 | - { | ||
| 358 | - Runtime.PyDict_SetItemString(modules, clr_prefix + tail.moduleName, tail.ObjectReference); | ||
| 359 | - } | ||
| 360 | 359 | } | |
| 361 | 360 | ||
| 362 | 361 | { | |
@@ -374,6 +373,8 @@ public static IntPtr __import__(IntPtr self, IntPtr argsRaw, IntPtr kw) | |||
| 374 | 373 | ||
| 375 | 374 | private static bool IsLoadAll(BorrowedReference fromList) | |
| 376 | 375 | { | |
| 376 | + if (fromList == null) throw new ArgumentNullException(nameof(fromList)); | ||
| 377 | + | ||
| 377 | 378 | if (CLRModule.preload) | |
| 378 | 379 | { | |
| 379 | 380 | return false; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -615,19 +615,15 @@ public virtual void DelItem(int index) | |||
| 615 | 615 | ||
| 616 | 616 | ||
| 617 | 617 | /// <summary> | |
| 618 | - /// Length Method | ||
| 619 | - /// </summary> | ||
| 620 | - /// <remarks> | ||
| 621 | 618 | /// Returns the length for objects that support the Python sequence | |
| 622 | - /// protocol, or 0 if the object does not support the protocol. | ||
| 623 | - /// </remarks> | ||
| 619 | + /// protocol. | ||
| 620 | + /// </summary> | ||
| 624 | 621 | public virtual long Length() | |
| 625 | 622 | { | |
| 626 | - var s = Runtime.PyObject_Size(obj); | ||
| 623 | + var s = Runtime.PyObject_Size(Reference); | ||
| 627 | 624 | if (s < 0) | |
| 628 | 625 | { | |
| 629 | - Runtime.PyErr_Clear(); | ||
| 630 | - return 0; | ||
| 626 | + throw new PythonException(); | ||
| 631 | 627 | } | |
| 632 | 628 | return s; | |
| 633 | 629 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1121,13 +1121,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) | |||
| 1121 | 1121 | ||
| 1122 | 1122 | internal static int PyObject_Not(IntPtr pointer) => Delegates.PyObject_Not(pointer); | |
| 1123 | 1123 | ||
| 1124 | - internal static long PyObject_Size(IntPtr pointer) | ||
| 1125 | - { | ||
| 1126 | - return (long)_PyObject_Size(pointer); | ||
| 1127 | - } | ||
| 1128 | - | ||
| 1129 | - | ||
| 1130 | - private static IntPtr _PyObject_Size(IntPtr pointer) => Delegates._PyObject_Size(pointer); | ||
| 1124 | + internal static nint PyObject_Size(BorrowedReference pointer) => Delegates.PyObject_Size(pointer); | ||
| 1131 | 1125 | ||
| 1132 | 1126 | ||
| 1133 | 1127 | internal static nint PyObject_Hash(IntPtr op) => Delegates.PyObject_Hash(op); | |
@@ -2028,9 +2022,8 @@ internal static IntPtr PyType_GenericAlloc(IntPtr type, long n) | |||
| 2028 | 2022 | ||
| 2029 | 2023 | internal static int PyObject_GenericSetAttr(IntPtr obj, IntPtr name, IntPtr value) => Delegates.PyObject_GenericSetAttr(obj, name, value); | |
| 2030 | 2024 | ||
| 2031 | - | ||
| 2032 | - internal static BorrowedReference* _PyObject_GetDictPtr(BorrowedReference obj) => Delegates._PyObject_GetDictPtr(obj); | ||
| 2033 | - | ||
| 2025 | + internal static NewReference PyObject_GenericGetDict(BorrowedReference o) => PyObject_GenericGetDict(o, IntPtr.Zero); | ||
| 2026 | + internal static NewReference PyObject_GenericGetDict(BorrowedReference o, IntPtr context) => Delegates.PyObject_GenericGetDict(o, context); | ||
| 2034 | 2027 | ||
| 2035 | 2028 | internal static void PyObject_GC_Del(IntPtr tp) => Delegates.PyObject_GC_Del(tp); | |
| 2036 | 2029 | ||
@@ -2323,7 +2316,7 @@ static Delegates() | |||
| 2323 | 2316 | PyCallable_Check = (delegate* unmanaged[Cdecl]<IntPtr, int>)GetFunctionByName(nameof(PyCallable_Check), GetUnmanagedDll(_PythonDll)); | |
| 2324 | 2317 | PyObject_IsTrue = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsTrue), GetUnmanagedDll(_PythonDll)); | |
| 2325 | 2318 | PyObject_Not = (delegate* unmanaged[Cdecl]<IntPtr, int>)GetFunctionByName(nameof(PyObject_Not), GetUnmanagedDll(_PythonDll)); | |
| 2326 | - _PyObject_Size = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName("PyObject_Size", GetUnmanagedDll(_PythonDll)); | ||
| 2319 | + PyObject_Size = (delegate* unmanaged[Cdecl]<BorrowedReference, nint>)GetFunctionByName("PyObject_Size", GetUnmanagedDll(_PythonDll)); | ||
| 2327 | 2320 | PyObject_Hash = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Hash), GetUnmanagedDll(_PythonDll)); | |
| 2328 | 2321 | PyObject_Repr = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Repr), GetUnmanagedDll(_PythonDll)); | |
| 2329 | 2322 | PyObject_Str = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_Str), GetUnmanagedDll(_PythonDll)); | |
@@ -2478,8 +2471,8 @@ static Delegates() | |||
| 2478 | 2471 | PyType_Ready = (delegate* unmanaged[Cdecl]<IntPtr, int>)GetFunctionByName(nameof(PyType_Ready), GetUnmanagedDll(_PythonDll)); | |
| 2479 | 2472 | _PyType_Lookup = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr>)GetFunctionByName(nameof(_PyType_Lookup), GetUnmanagedDll(_PythonDll)); | |
| 2480 | 2473 | PyObject_GenericGetAttr = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr>)GetFunctionByName(nameof(PyObject_GenericGetAttr), GetUnmanagedDll(_PythonDll)); | |
| 2474 | + PyObject_GenericGetDict = (delegate* unmanaged[Cdecl]<BorrowedReference, IntPtr, NewReference>)GetFunctionByName(nameof(PyObject_GenericGetDict), GetUnmanagedDll(PythonDLL)); | ||
| 2481 | 2475 | PyObject_GenericSetAttr = (delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, int>)GetFunctionByName(nameof(PyObject_GenericSetAttr), GetUnmanagedDll(_PythonDll)); | |
| 2482 | - _PyObject_GetDictPtr = (delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference*>)GetFunctionByName(nameof(_PyObject_GetDictPtr), GetUnmanagedDll(_PythonDll)); | ||
| 2483 | 2476 | PyObject_GC_Del = (delegate* unmanaged[Cdecl]<IntPtr, void>)GetFunctionByName(nameof(PyObject_GC_Del), GetUnmanagedDll(_PythonDll)); | |
| 2484 | 2477 | PyObject_GC_Track = (delegate* unmanaged[Cdecl]<IntPtr, void>)GetFunctionByName(nameof(PyObject_GC_Track), GetUnmanagedDll(_PythonDll)); | |
| 2485 | 2478 | PyObject_GC_UnTrack = (delegate* unmanaged[Cdecl]<IntPtr, void>)GetFunctionByName(nameof(PyObject_GC_UnTrack), GetUnmanagedDll(_PythonDll)); | |
@@ -2595,7 +2588,7 @@ static Delegates() | |||
| 2595 | 2588 | internal static delegate* unmanaged[Cdecl]<IntPtr, int> PyCallable_Check { get; } | |
| 2596 | 2589 | internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyObject_IsTrue { get; } | |
| 2597 | 2590 | internal static delegate* unmanaged[Cdecl]<IntPtr, int> PyObject_Not { get; } | |
| 2598 | - internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> _PyObject_Size { get; } | ||
| 2591 | + internal static delegate* unmanaged[Cdecl]<BorrowedReference, nint> PyObject_Size { get; } | ||
| 2599 | 2592 | internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Hash { get; } | |
| 2600 | 2593 | internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Repr { get; } | |
| 2601 | 2594 | internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr> PyObject_Str { get; } | |
@@ -2744,7 +2737,6 @@ static Delegates() | |||
| 2744 | 2737 | internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr> _PyType_Lookup { get; } | |
| 2745 | 2738 | internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr> PyObject_GenericGetAttr { get; } | |
| 2746 | 2739 | internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, int> PyObject_GenericSetAttr { get; } | |
| 2747 | - internal static delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference*> _PyObject_GetDictPtr { get; } | ||
| 2748 | 2740 | internal static delegate* unmanaged[Cdecl]<IntPtr, void> PyObject_GC_Del { get; } | |
| 2749 | 2741 | internal static delegate* unmanaged[Cdecl]<IntPtr, void> PyObject_GC_Track { get; } | |
| 2750 | 2742 | internal static delegate* unmanaged[Cdecl]<IntPtr, void> PyObject_GC_UnTrack { get; } | |
@@ -2781,6 +2773,7 @@ static Delegates() | |||
| 2781 | 2773 | internal static delegate* unmanaged[Cdecl]<IntPtr, IntPtr, void> PyException_SetCause { get; } | |
| 2782 | 2774 | internal static delegate* unmanaged[Cdecl]<uint, IntPtr, int> PyThreadState_SetAsyncExcLLP64 { get; } | |
| 2783 | 2775 | internal static delegate* unmanaged[Cdecl]<ulong, IntPtr, int> PyThreadState_SetAsyncExcLP64 { get; } | |
| 2776 | + internal static delegate* unmanaged[Cdecl]<BorrowedReference, IntPtr, NewReference> PyObject_GenericGetDict { get; } | ||
| 2784 | 2777 | } | |
| 2785 | 2778 | } | |
| 2786 | 2779 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments