diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cc571ad4..b7e1d7ebc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - When calling C# from Python, enable passing argument of any type to a parameter of C# type `object` by wrapping it into `PyObject` instance. ([#881][i881]) - Added support for kwarg parameters when calling .NET methods from Python - Changed method for finding MSBuild using vswhere +- Removed .NET remoting support ### Fixed diff --git a/src/embed_tests/TestCallbacks.cs b/src/embed_tests/TestCallbacks.cs index 220b0a86a..3d1946c78 100644 --- a/src/embed_tests/TestCallbacks.cs +++ b/src/embed_tests/TestCallbacks.cs @@ -25,9 +25,12 @@ public void TestNoOverloadException() { dynamic callWith42 = PythonEngine.Eval("lambda f: f([42])"); var error = Assert.Throws(() => callWith42(aFunctionThatCallsIntoPython.ToPython())); Assert.AreEqual("TypeError", error.PythonTypeName); - string expectedArgTypes = Runtime.IsPython2 - ? "()" - : "()"; + string expectedArgTypes = +#if PYTHON2 + "()"; +#else + "()"; +#endif StringAssert.EndsWith(expectedArgTypes, error.Message); } } diff --git a/src/runtime/CustomMarshaler.cs b/src/runtime/CustomMarshaler.cs index b51911816..934ef52f7 100644 --- a/src/runtime/CustomMarshaler.cs +++ b/src/runtime/CustomMarshaler.cs @@ -120,9 +120,11 @@ public static int GetUnicodeByteLength(IntPtr p) /// public static IntPtr Py3UnicodePy2StringtoPtr(string s) { - return Runtime.IsPython3 - ? Instance.MarshalManagedToNative(s) - : Marshal.StringToHGlobalAnsi(s); +#if PYTHON2 + return Marshal.StringToHGlobalAnsi(s); +#else + return Instance.MarshalManagedToNative(s); +#endif } /// @@ -137,9 +139,11 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s) /// public static string PtrToPy3UnicodePy2String(IntPtr p) { - return Runtime.IsPython3 - ? PtrToStringUni(p) - : Marshal.PtrToStringAnsi(p); +#if PYTHON2 + return Marshal.PtrToStringAnsi(p); +#else + return PtrToStringUni(p); +#endif } } diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index a7b7b5c48..736183c2c 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -86,8 +86,10 @@ internal static IntPtr GetPythonTypeByAlias(Type op) if (op == int32Type) return Runtime.PyIntType; - if (op == int64Type && Runtime.IsPython2) +#if PYTHON2 + if (op == int64Type) return Runtime.PyLongType; +#endif if (op == int64Type) return Runtime.PyIntType; @@ -165,15 +167,7 @@ internal static IntPtr ToPython(object value, Type type) var pyderived = value as IPythonDerivedType; if (null != pyderived) { - #if NETSTANDARD return ClassDerivedObject.ToPython(pyderived); - #else - // if object is remote don't do this - if (!System.Runtime.Remoting.RemotingServices.IsTransparentProxy(pyderived)) - { - return ClassDerivedObject.ToPython(pyderived); - } - #endif } // hmm - from Python, we almost never care what the declared @@ -488,8 +482,9 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo return true; case TypeCode.Int32: +#if PYTHON2 // Trickery to support 64-bit platforms. - if (Runtime.IsPython2 && Runtime.Is32Bit) + if (Runtime.Is32Bit) { op = Runtime.PyNumber_Int(value); @@ -513,7 +508,8 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo result = ival; return true; } - else // Python3 always use PyLong API +#else + // Python3 always use PyLong API { op = Runtime.PyNumber_Long(value); if (op == IntPtr.Zero) @@ -538,13 +534,14 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo result = (int)ll; return true; } +#endif case TypeCode.Boolean: result = Runtime.PyObject_IsTrue(value) != 0; return true; case TypeCode.Byte: -#if PYTHON3 +#if !PYTHON2 if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) @@ -555,7 +552,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo } goto type_error; } -#elif PYTHON2 +#else if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType)) { if (Runtime.PyString_Size(value) == 1) @@ -589,7 +586,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo return true; case TypeCode.SByte: -#if PYTHON3 +#if !PYTHON2 if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) @@ -600,7 +597,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo } goto type_error; } -#elif PYTHON2 +#else if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType)) { if (Runtime.PyString_Size(value) == 1) @@ -634,7 +631,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo return true; case TypeCode.Char: -#if PYTHON3 +#if !PYTHON2 if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) @@ -645,7 +642,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo } goto type_error; } -#elif PYTHON2 +#else if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType)) { if (Runtime.PyString_Size(value) == 1) @@ -753,20 +750,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo } goto type_error; } - + uint ui; - try + try { ui = Convert.ToUInt32(Runtime.PyLong_AsUnsignedLong(op)); } catch (OverflowException) { // Probably wasn't an overflow in python but was in C# (e.g. if cpython - // longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in + // longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in // PyLong_AsUnsignedLong) Runtime.XDecref(op); goto overflow; } - + if (Exceptions.ErrorOccurred()) { @@ -900,7 +897,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s var listType = typeof(List<>); var constructedListType = listType.MakeGenericType(elementType); - IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) : + IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) : (IList) Activator.CreateInstance(constructedListType); IntPtr item; @@ -921,7 +918,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s items = Array.CreateInstance(elementType, list.Count); list.CopyTo(items, 0); - + result = items; return true; } diff --git a/src/runtime/delegateobject.cs b/src/runtime/delegateobject.cs index e1103cbc7..c9aad9898 100644 --- a/src/runtime/delegateobject.cs +++ b/src/runtime/delegateobject.cs @@ -96,7 +96,6 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) /// /// Implements __cmp__ for reflected delegate types. /// -#if PYTHON3 // TODO: Doesn't PY2 implement tp_richcompare too? public new static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op) { if (op != Runtime.Py_EQ && op != Runtime.Py_NE) @@ -126,13 +125,5 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) Runtime.XIncref(pyfalse); return pyfalse; } -#elif PYTHON2 - public static int tp_compare(IntPtr ob, IntPtr other) - { - Delegate d1 = GetTrueDelegate(ob); - Delegate d2 = GetTrueDelegate(other); - return d1 == d2 ? 0 : -1; - } -#endif } } diff --git a/src/runtime/exceptions.cs b/src/runtime/exceptions.cs index 31c367eb2..e4e823a9a 100644 --- a/src/runtime/exceptions.cs +++ b/src/runtime/exceptions.cs @@ -103,7 +103,12 @@ private Exceptions() /// internal static void Initialize() { - string exceptionsModuleName = Runtime.IsPython3 ? "builtins" : "exceptions"; + string exceptionsModuleName = +#if PYTHON2 + "exceptions"; +#else + "builtins"; +#endif exceptions_module = Runtime.PyImport_ImportModule(exceptionsModuleName); Exceptions.ErrorCheck(exceptions_module); @@ -180,7 +185,7 @@ internal static void SetArgsAndCause(IntPtr ob) Marshal.WriteIntPtr(ob, ExceptionOffset.args, args); -#if PYTHON3 +#if !PYTHON2 if (e.InnerException != null) { IntPtr cause = CLRObject.GetInstHandle(e.InnerException); diff --git a/src/runtime/importhook.cs b/src/runtime/importhook.cs index aa3bbab6d..58069eb24 100644 --- a/src/runtime/importhook.cs +++ b/src/runtime/importhook.cs @@ -13,7 +13,6 @@ internal class ImportHook private static MethodWrapper hook; private static IntPtr py_clr_module; -#if PYTHON3 private static IntPtr module_def = IntPtr.Zero; internal static void InitializeModuleDef() @@ -33,7 +32,6 @@ internal static void ReleaseModuleDef() ModuleDefOffset.FreeModuleDef(module_def); module_def = IntPtr.Zero; } -#endif /// /// Initialize just the __import__ hook itself. @@ -82,7 +80,7 @@ internal static void Initialize() // Initialize the clr module and tell Python about it. root = new CLRModule(); -#if PYTHON3 +#if !PYTHON2 // create a python module with the same methods as the clr module-like object InitializeModuleDef(); py_clr_module = Runtime.PyModule_Create2(module_def, 3); @@ -93,7 +91,7 @@ internal static void Initialize() clr_dict = (IntPtr)Marshal.PtrToStructure(clr_dict, typeof(IntPtr)); Runtime.PyDict_Update(mod_dict, clr_dict); -#elif PYTHON2 +#else Runtime.XIncref(root.pyHandle); // we are using the module two times py_clr_module = root.pyHandle; // Alias handle for PY2/PY3 #endif @@ -118,7 +116,7 @@ internal static void Shutdown() bool shouldFreeDef = Runtime.Refcount(py_clr_module) == 1; Runtime.XDecref(py_clr_module); py_clr_module = IntPtr.Zero; -#if PYTHON3 +#if !PYTHON2 if (shouldFreeDef) { ReleaseModuleDef(); @@ -137,11 +135,10 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null) { root.InitializePreload(); - if (Runtime.IsPython2) - { - Runtime.XIncref(py_clr_module); - return py_clr_module; - } +#if PYTHON2 + Runtime.XIncref(py_clr_module); + return py_clr_module; +#endif // Python 3 // update the module dictionary with the contents of the root dictionary diff --git a/src/runtime/interop.cs b/src/runtime/interop.cs index ca3c35bfd..135b35ba0 100644 --- a/src/runtime/interop.cs +++ b/src/runtime/interop.cs @@ -156,7 +156,7 @@ public static int Size() public static int args = 0; #if PYTHON2 public static int message = 0; -#elif PYTHON3 +#else public static int traceback = 0; public static int context = 0; public static int cause = 0; @@ -169,7 +169,6 @@ public static int Size() } -#if PYTHON3 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] internal class BytesOffset { @@ -260,7 +259,6 @@ public static void FreeModuleDef(IntPtr ptr) public static int name = 0; } -#endif // PYTHON3 /// /// TypeFlags(): The actual bit values for the Type Flags stored @@ -270,17 +268,6 @@ public static void FreeModuleDef(IntPtr ptr) /// internal class TypeFlags { -#if PYTHON2 // these flags were removed in Python 3 - public static int HaveGetCharBuffer = (1 << 0); - public static int HaveSequenceIn = (1 << 1); - public static int GC = 0; - public static int HaveInPlaceOps = (1 << 3); - public static int CheckTypes = (1 << 4); - public static int HaveRichCompare = (1 << 5); - public static int HaveWeakRefs = (1 << 6); - public static int HaveIter = (1 << 7); - public static int HaveClass = (1 << 8); -#endif public static int HeapType = (1 << 9); public static int BaseType = (1 << 10); public static int Ready = (1 << 12); @@ -309,6 +296,15 @@ internal class TypeFlags public static int TypeSubclass = (1 << 31); #if PYTHON2 // Default flags for Python 2 + public static int HaveGetCharBuffer = (1 << 0); + public static int HaveSequenceIn = (1 << 1); + public static int GC = 0; + public static int HaveInPlaceOps = (1 << 3); + public static int CheckTypes = (1 << 4); + public static int HaveRichCompare = (1 << 5); + public static int HaveWeakRefs = (1 << 6); + public static int HaveIter = (1 << 7); + public static int HaveClass = (1 << 8); public static int Default = ( HaveGetCharBuffer | HaveSequenceIn | @@ -320,7 +316,7 @@ internal class TypeFlags HaveStacklessExtension | HaveIndex | 0); -#elif PYTHON3 // Default flags for Python 3 +#else // Default flags for Python 3 public static int Default = ( HaveStacklessExtension | HaveVersionTag); diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index df2d98641..47e65b5d8 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -95,13 +95,13 @@ public static string PythonPath } set { - if (Runtime.IsPython2) - { - throw new NotSupportedException("Set PythonPath not supported on Python 2"); - } +#if PYTHON2 + throw new NotSupportedException("Set PythonPath not supported on Python 2"); +#else Marshal.FreeHGlobal(_pythonPath); _pythonPath = UcsMarshaler.Py3UnicodePy2StringtoPtr(value); Runtime.Py_SetPath(_pythonPath); +#endif } } @@ -254,11 +254,7 @@ static void OnDomainUnload(object _, EventArgs __) /// CPython interpreter process - this bootstraps the managed runtime /// when it is imported by the CLR extension module. /// -#if PYTHON3 - public static IntPtr InitExt() -#elif PYTHON2 - public static void InitExt() -#endif + public static int InternalInitialize(int size, IntPtr data) { try { @@ -294,18 +290,13 @@ public static void InitExt() " break\n"; PythonEngine.Exec(code); + return 0; } catch (PythonException e) { e.Restore(); -#if PYTHON3 - return IntPtr.Zero; -#endif + return -1; } - -#if PYTHON3 - return Python.Runtime.ImportHook.GetCLRModule(); -#endif } /// @@ -321,7 +312,7 @@ public static void Shutdown() if (initialized) { PyScopeManager.Global.Clear(); - + // If the shutdown handlers trigger a domain unload, // don't call shutdown again. AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload; @@ -587,7 +578,7 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals, borrowedGlobals = false; } } - + if (locals == null) { locals = globals; @@ -650,7 +641,7 @@ public static PyScope CreateScope(string name) var scope = PyScopeManager.Global.Create(name); return scope; } - + public class GILState : IDisposable { private readonly IntPtr state; @@ -751,7 +742,7 @@ public static void SetArgv(IEnumerable argv) public static void With(PyObject obj, Action Body) { - // Behavior described here: + // Behavior described here: // https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers IntPtr type = Runtime.PyNone; diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index bae3daa15..811d3fdb4 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -22,7 +22,7 @@ public class Runtime public static int UCS => _UCS; -#if UCS4 +#if !UCS2 internal const int _UCS = 4; /// @@ -30,7 +30,7 @@ public class Runtime /// methods prior to PEP393. Only used for PY27. /// private const string PyUnicodeEntryPoint = "PyUnicodeUCS4_"; -#elif UCS2 +#else internal const int _UCS = 2; /// @@ -38,69 +38,9 @@ public class Runtime /// methods prior to PEP393. Only used for PY27. /// private const string PyUnicodeEntryPoint = "PyUnicodeUCS2_"; -#else -#error You must define either UCS2 or UCS4! -#endif - - // C# compiler copies constants to the assemblies that references this library. - // We needs to replace all public constants to static readonly fields to allow - // binary substitution of different Python.Runtime.dll builds in a target application. - - public static string pyversion => _pyversion; - public static string pyver => _pyver; - -#if PYTHON27 - internal const string _pyversion = "2.7"; - internal const string _pyver = "27"; -#elif PYTHON34 - internal const string _pyversion = "3.4"; - internal const string _pyver = "34"; -#elif PYTHON35 - internal const string _pyversion = "3.5"; - internal const string _pyver = "35"; -#elif PYTHON36 - internal const string _pyversion = "3.6"; - internal const string _pyver = "36"; -#elif PYTHON37 - internal const string _pyversion = "3.7"; - internal const string _pyver = "37"; -#elif PYTHON38 - internal const string _pyversion = "3.8"; - internal const string _pyver = "38"; -#else -#error You must define one of PYTHON34 to PYTHON38 or PYTHON27 -#endif - -#if MONO_LINUX || MONO_OSX // Linux/macOS use dotted version string - internal const string dllBase = "python" + _pyversion; -#else // Windows - internal const string dllBase = "python" + _pyver; #endif -#if PYTHON_WITH_PYDEBUG - internal const string dllWithPyDebug = "d"; -#else - internal const string dllWithPyDebug = ""; -#endif -#if PYTHON_WITH_PYMALLOC - internal const string dllWithPyMalloc = "m"; -#else - internal const string dllWithPyMalloc = ""; -#endif - - // C# compiler copies constants to the assemblies that references this library. - // We needs to replace all public constants to static readonly fields to allow - // binary substitution of different Python.Runtime.dll builds in a target application. - - public static readonly string PythonDLL = _PythonDll; - -#if PYTHON_WITHOUT_ENABLE_SHARED && !NETSTANDARD internal const string _PythonDll = "__Internal"; -#else - internal const string _PythonDll = dllBase + dllWithPyDebug + dllWithPyMalloc; -#endif - - public static readonly int pyversionnumber = Convert.ToInt32(_pyver); // set to true when python is finalizing internal static object IsFinalizingLock = new object(); @@ -159,8 +99,13 @@ public class Runtime /// public static string MachineName { get; private set; } - internal static bool IsPython2 = pyversionnumber < 30; - internal static bool IsPython3 = pyversionnumber >= 30; +#if PYTHON2 + internal static bool IsPython2 = true; +#else + internal static bool IsPython2 = false; +#endif + + internal static bool IsPython3 = !IsPython2; public static int MainManagedThreadId { get; private set; } @@ -248,7 +193,7 @@ internal static void Initialize(bool initSigs = false) () => PyUnicodeType = IntPtr.Zero); XDecref(op); -#if PYTHON3 +#if !PYTHON2 op = PyBytes_FromString("bytes"); SetPyMember(ref PyBytesType, PyObject_Type(op), () => PyBytesType = IntPtr.Zero); @@ -455,9 +400,7 @@ private static void ResetPyMembers() internal static IntPtr Py_NoSiteFlag; -#if PYTHON3 internal static IntPtr PyBytesType; -#endif internal static IntPtr _PyObject_NextNotImplemented; internal static IntPtr PyNotImplemented; @@ -721,13 +664,13 @@ internal static unsafe long Refcount(IntPtr op) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyGILState_GetThisThreadState(); -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] public static extern int Py_Main( int argc, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrArrayMarshaler))] string[] argv ); -#elif PYTHON2 +#else [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] public static extern int Py_Main(int argc, string[] argv); #endif @@ -958,7 +901,6 @@ internal static bool PyObject_IsIterable(IntPtr pointer) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_CallObject(IntPtr pointer, IntPtr args); -#if PYTHON3 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_RichCompareBool(IntPtr value1, IntPtr value2, int opid); @@ -986,10 +928,6 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) Exceptions.SetError(Exceptions.SystemError, "Error comparing objects"); return -1; } -#elif PYTHON2 - [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] - internal static extern int PyObject_Compare(IntPtr value1, IntPtr value2); -#endif [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_IsInstance(IntPtr ob, IntPtr type); @@ -1023,11 +961,11 @@ internal static long PyObject_Size(IntPtr pointer) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Str(IntPtr pointer); -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyObject_Str")] internal static extern IntPtr PyObject_Unicode(IntPtr pointer); -#elif PYTHON2 +#else [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Unicode(IntPtr pointer); #endif @@ -1040,11 +978,11 @@ internal static long PyObject_Size(IntPtr pointer) // Python number API //==================================================================== -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyNumber_Long")] internal static extern IntPtr PyNumber_Int(IntPtr ob); -#elif PYTHON2 +#else [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Int(IntPtr ob); #endif @@ -1080,7 +1018,7 @@ internal static IntPtr PyInt_FromInt64(long value) return PyInt_FromLong(v); } -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyLong_FromLong")] private static extern IntPtr PyInt_FromLong(IntPtr value); @@ -1092,7 +1030,7 @@ internal static IntPtr PyInt_FromInt64(long value) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyLong_FromString")] internal static extern IntPtr PyInt_FromString(string value, IntPtr end, int radix); -#elif PYTHON2 +#else [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr PyInt_FromLong(IntPtr value); @@ -1369,14 +1307,14 @@ internal static bool PyString_Check(IntPtr ob) internal static IntPtr PyString_FromString(string value) { -#if PYTHON3 +#if !PYTHON2 return PyUnicode_FromKindAndData(_UCS, value, value.Length); -#elif PYTHON2 +#else return PyString_FromStringAndSize(value, value.Length); #endif } -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyBytes_FromString(string op); @@ -1416,7 +1354,7 @@ internal static IntPtr PyUnicode_FromStringAndSize(IntPtr value, long size) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_AsUTF8(IntPtr unicode); -#elif PYTHON2 +#else internal static IntPtr PyString_FromStringAndSize(string value, long size) { return PyString_FromStringAndSize(value, new IntPtr(size)); @@ -1437,7 +1375,7 @@ internal static bool PyUnicode_Check(IntPtr ob) return PyObject_TYPE(ob) == PyUnicodeType; } -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromObject(IntPtr ob); @@ -1474,7 +1412,7 @@ internal static long PyUnicode_GetSize(IntPtr ob) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromOrdinal(int c); -#elif PYTHON2 +#else [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = PyUnicodeEntryPoint + "FromObject")] internal static extern IntPtr PyUnicode_FromObject(IntPtr ob); @@ -1785,7 +1723,7 @@ internal static bool PyIter_Check(IntPtr pointer) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern string PyModule_GetFilename(IntPtr module); -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyModule_Create2(IntPtr module, int apiver); #endif @@ -1808,14 +1746,14 @@ internal static bool PyIter_Check(IntPtr pointer) [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyImport_GetModuleDict(); -#if PYTHON3 +#if !PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PySys_SetArgvEx( int argc, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrArrayMarshaler))] string[] argv, int updatepath ); -#elif PYTHON2 +#else [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PySys_SetArgvEx( int argc, @@ -1976,24 +1914,8 @@ internal static void SetNoSiteFlag() { var loader = LibraryLoader.Get(OperatingSystem); - IntPtr dllLocal; - if (_PythonDll != "__Internal") - { - dllLocal = loader.Load(_PythonDll); - } - - try - { - Py_NoSiteFlag = loader.GetFunction(dllLocal, "Py_NoSiteFlag"); - Marshal.WriteInt32(Py_NoSiteFlag, 1); - } - finally - { - if (dllLocal != IntPtr.Zero) - { - loader.Free(dllLocal); - } - } + Py_NoSiteFlag = loader.GetFunction(IntPtr.Zero, "Py_NoSiteFlag"); + Marshal.WriteInt32(Py_NoSiteFlag, 1); } /// diff --git a/src/runtime/typemanager.cs b/src/runtime/typemanager.cs index bb920b74f..bd9a4568e 100644 --- a/src/runtime/typemanager.cs +++ b/src/runtime/typemanager.cs @@ -425,20 +425,16 @@ internal static IntPtr AllocateTypeObject(string name) // Cheat a little: we'll set tp_name to the internal char * of // the Python version of the type name - otherwise we'd have to // allocate the tp_name and would have no way to free it. -#if PYTHON3 +#if !PYTHON2 IntPtr temp = Runtime.PyUnicode_FromString(name); IntPtr raw = Runtime.PyUnicode_AsUTF8(temp); -#elif PYTHON2 +#else IntPtr temp = Runtime.PyString_FromString(name); IntPtr raw = Runtime.PyString_AsString(temp); #endif Marshal.WriteIntPtr(type, TypeOffset.tp_name, raw); Marshal.WriteIntPtr(type, TypeOffset.name, temp); -#if PYTHON3 - Marshal.WriteIntPtr(type, TypeOffset.qualname, temp); -#endif - long ptr = type.ToInt64(); // 64-bit safe temp = new IntPtr(ptr + TypeOffset.nb_add); @@ -450,9 +446,9 @@ internal static IntPtr AllocateTypeObject(string name) temp = new IntPtr(ptr + TypeOffset.mp_length); Marshal.WriteIntPtr(type, TypeOffset.tp_as_mapping, temp); -#if PYTHON3 +#if !PYTHON2 temp = new IntPtr(ptr + TypeOffset.bf_getbuffer); -#elif PYTHON2 +#else temp = new IntPtr(ptr + TypeOffset.bf_getreadbuffer); #endif Marshal.WriteIntPtr(type, TypeOffset.tp_as_buffer, temp);