From 83ae857b4ff4affa12c13aa01f2e6519bdf61f13 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 11 Jan 2017 19:40:11 -0700 Subject: [PATCH 1/4] Define PYTHON2 & PYTHON3 directives Makes code more future proof as new python versions role out. We could otherwise use !PYTHON27 but it could be confused as a patch specific to that version of python instead of PYTHON3 compat. --- setup.py | 3 ++- src/clrmodule/ClrModule.cs | 12 ++++++------ src/clrmodule/clrmodule.csproj | 20 ++++++++------------ src/runtime/Python.Runtime.csproj | 16 ++++++++-------- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/setup.py b/setup.py index 3a59a6d8a..9353a581f 100644 --- a/setup.py +++ b/setup.py @@ -138,7 +138,8 @@ def build_extension(self, ext): unicode_width = ctypes.sizeof(ctypes.c_wchar) defines = [ - "PYTHON%d%s" % (sys.version_info[:2]), + "PYTHON%d%d" % (sys.version_info[:2]), + "PYTHON%d" % (sys.version_info[:1]), # Python Major Version "UCS%d" % unicode_width, ] diff --git a/src/clrmodule/ClrModule.cs b/src/clrmodule/ClrModule.cs index 6944f4b44..5fa28e15a 100644 --- a/src/clrmodule/ClrModule.cs +++ b/src/clrmodule/ClrModule.cs @@ -29,10 +29,10 @@ public class clrModule { -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllExport("PyInit_clr", CallingConvention.StdCall)] public static IntPtr PyInit_clr() -#else +#elif PYTHON2 [DllExport("initclr", CallingConvention.StdCall)] public static void initclr() #endif @@ -99,9 +99,9 @@ public static void initclr() #if DEBUG_PRINT Console.WriteLine("Could not load Python.Runtime"); #endif -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 return IntPtr.Zero; -#else +#elif PYTHON2 return; #endif } @@ -111,9 +111,9 @@ public static void initclr() // So now we get the PythonEngine and execute the InitExt method on it. Type pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine"); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 return (IntPtr)pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null); -#else +#elif PYTHON2 pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null); #endif } diff --git a/src/clrmodule/clrmodule.csproj b/src/clrmodule/clrmodule.csproj index 134f5997e..d19646778 100644 --- a/src/clrmodule/clrmodule.csproj +++ b/src/clrmodule/clrmodule.csproj @@ -18,7 +18,7 @@ true bin\x86\DebugMono\ - DEBUG;TRACE + TRACE;DEBUG;PYTHON2 full x86 prompt @@ -29,7 +29,7 @@ true bin\x64\DebugMono\ - DEBUG;TRACE + TRACE;DEBUG;PYTHON2 full x64 prompt @@ -39,8 +39,7 @@ bin\x86\ReleaseMono\ - - + PYTHON2 true pdbonly x86 @@ -51,8 +50,7 @@ bin\x64\ReleaseMono\ - - + PYTHON2 true pdbonly x64 @@ -64,7 +62,7 @@ true bin\x86\DebugWin\ - TRACE;DEBUG;DEBUG_PRINT + TRACE;DEBUG;PYTHON2 full x86 prompt @@ -75,7 +73,7 @@ true bin\x64\DebugWin\ - DEBUG;TRACE + TRACE;DEBUG;PYTHON2 full x64 prompt @@ -85,8 +83,7 @@ bin\x86\ReleaseWin\ - - + PYTHON2 true pdbonly x86 @@ -97,8 +94,7 @@ bin\x64\ReleaseWin\ - - + PYTHON2 true pdbonly x64 diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index f12aabce4..81616448c 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -13,7 +13,7 @@ bin\x86\ReleaseMono\ - PYTHON27, UCS4 + PYTHON2;PYTHON27;UCS4 true true pdbonly @@ -23,7 +23,7 @@ bin\x64\ReleaseMono\ - PYTHON27, UCS4 + PYTHON2;PYTHON27;UCS4 true true pdbonly @@ -33,7 +33,7 @@ bin\x86\ReleaseWin\ - PYTHON27, UCS2 + PYTHON2;PYTHON27;UCS2 true true pdbonly @@ -43,7 +43,7 @@ bin\x64\ReleaseWin\ - PYTHON27, UCS2 + PYTHON2;PYTHON27;UCS2 true true pdbonly @@ -54,7 +54,7 @@ true bin\x86\DebugMono\ - TRACE;DEBUG;PYTHON27,UCS4 + TRACE;DEBUG;PYTHON2;PYTHON27;UCS4 true false full @@ -66,7 +66,7 @@ true bin\x64\DebugMono\ - TRACE;DEBUG;PYTHON27,UCS4 + TRACE;DEBUG;PYTHON2;PYTHON27;UCS4 true false full @@ -75,7 +75,7 @@ true bin\x86\DebugWin\ - TRACE;DEBUG;PYTHON27,UCS2 + TRACE;DEBUG;PYTHON2;PYTHON27;UCS2 true false full @@ -87,7 +87,7 @@ true bin\x64\DebugWin\ - TRACE;DEBUG;PYTHON27,UCS2 + TRACE;DEBUG;PYTHON2;PYTHON27;UCS2 true false full From 51d29118683535f01fba5025599496ebf4e7ffc1 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Wed, 11 Jan 2017 21:56:09 -0700 Subject: [PATCH 2/4] Use DEBUG instead of DEBUG_PRINT Go with general convention --- src/clrmodule/ClrModule.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clrmodule/ClrModule.cs b/src/clrmodule/ClrModule.cs index 5fa28e15a..eca411b30 100644 --- a/src/clrmodule/ClrModule.cs +++ b/src/clrmodule/ClrModule.cs @@ -17,7 +17,7 @@ // set to Python.Runtime's public key token. (sn -T Python.Runtin.dll) #define USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN -// If DEBUG_PRINT is defined in the Build Properties, a few Console.WriteLine +// If DEBUG is defined in the Build Properties, a few Console.WriteLine // calls are made to indicate what's going on during the load... //============================================================================ using System; @@ -37,7 +37,7 @@ public static IntPtr PyInit_clr() public static void initclr() #endif { -#if DEBUG_PRINT +#if DEBUG Console.WriteLine("Attempting to load Python.Runtime using standard binding rules... "); #endif #if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN @@ -65,7 +65,7 @@ public static void initclr() try { pythonRuntime = Assembly.Load(pythonRuntimeName); -#if DEBUG_PRINT +#if DEBUG Console.WriteLine("Success!"); #endif } @@ -89,14 +89,14 @@ public static void initclr() throw new InvalidOperationException(executingAssembly.Location); } string pythonRuntimeDllPath = Path.Combine(assemblyDirectory, "Python.Runtime.dll"); -#if DEBUG_PRINT +#if DEBUG Console.WriteLine("Attempting to load Python.Runtime from: '{0}'...", pythonRuntimeDllPath); #endif pythonRuntime = Assembly.LoadFrom(pythonRuntimeDllPath); } catch (InvalidOperationException) { -#if DEBUG_PRINT +#if DEBUG Console.WriteLine("Could not load Python.Runtime"); #endif #if PYTHON3 From 935c6538003e5a96d3608269a704964ec6d1242d Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Thu, 12 Jan 2017 00:59:45 -0700 Subject: [PATCH 3/4] Remove redundant #if directives Not relevant after dropping Python 24, Python 25 --- src/runtime/exceptions.cs | 6 ------ src/runtime/interop.cs | 8 +------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/runtime/exceptions.cs b/src/runtime/exceptions.cs index 30c8c7e27..c15f634d5 100644 --- a/src/runtime/exceptions.cs +++ b/src/runtime/exceptions.cs @@ -371,14 +371,10 @@ internal static IntPtr RaiseTypeError(string message) puplic static variables on the Exceptions class filled in from the python class using reflection in Initialize() looked up by name, not posistion. */ -#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) public static IntPtr BaseException; -#endif public static IntPtr Exception; public static IntPtr StopIteration; -#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) public static IntPtr GeneratorExit; -#endif #if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) public static IntPtr StandardError; #endif @@ -436,10 +432,8 @@ puplic static variables on the Exceptions class filled in from public static IntPtr SyntaxWarning; public static IntPtr RuntimeWarning; public static IntPtr FutureWarning; -#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) public static IntPtr ImportWarning; public static IntPtr UnicodeWarning; //PyAPI_DATA(PyObject *) PyExc_BytesWarning; -#endif } } diff --git a/src/runtime/interop.cs b/src/runtime/interop.cs index fa5c258f9..ca2955e48 100644 --- a/src/runtime/interop.cs +++ b/src/runtime/interop.cs @@ -292,10 +292,7 @@ internal class TypeFlags /* XXX Reusing reserved constants */ public static int Managed = (1 << 15); // PythonNet specific public static int Subclass = (1 << 16); // PythonNet specific -#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) public static int HaveIndex = (1 << 17); -#endif -#if (PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) /* Objects support nb_index in PyNumberMethods */ public static int HaveVersionTag = (1 << 18); public static int ValidVersionTag = (1 << 19); @@ -311,7 +308,6 @@ internal class TypeFlags public static int DictSubclass = (1 << 29); public static int BaseExceptionSubclass = (1 << 30); public static int TypeSubclass = (1 << 31); -#endif // Default flags for Python 2 #if (PYTHON23 || PYTHON24 || PYTHON25 || PYTHON26 || PYTHON27) @@ -433,9 +429,7 @@ static Interop() pmap["nb_true_divide"] = p["BinaryFunc"]; pmap["nb_inplace_floor_divide"] = p["BinaryFunc"]; pmap["nb_inplace_true_divide"] = p["BinaryFunc"]; -#if (PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) pmap["nb_index"] = p["UnaryFunc"]; -#endif pmap["sq_length"] = p["InquiryFunc"]; pmap["sq_concat"] = p["BinaryFunc"]; @@ -536,4 +530,4 @@ public Thunk(Delegate d) fn = d; } } -} \ No newline at end of file +} From c16bb82c50719f3f0e70a14474c0f1a33dfc076e Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Thu, 12 Jan 2017 01:05:13 -0700 Subject: [PATCH 4/4] Refactor #if by using PYTHON2 and PYTHON3 --- src/runtime/converter.cs | 20 ++++++------ src/runtime/delegateobject.cs | 6 ++-- src/runtime/exceptions.cs | 8 ++--- src/runtime/importhook.cs | 20 ++++++------ src/runtime/interop.cs | 26 ++++++---------- src/runtime/pythonengine.cs | 10 +++--- src/runtime/runtime.cs | 58 +++++++++++++++++------------------ src/runtime/typemanager.cs | 12 ++++---- 8 files changed, 77 insertions(+), 83 deletions(-) diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index de0003c51..8cbac7fd7 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -84,7 +84,7 @@ internal static IntPtr GetPythonTypeByAlias(Type op) { return Runtime.PyUnicodeType; } -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 else if ((op == int16Type) || (op == int32Type) || (op == int64Type)) { @@ -450,7 +450,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, return true; case TypeCode.Int32: -#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON2 // Trickery to support 64-bit platforms. if (IntPtr.Size == 4) { @@ -478,7 +478,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, } else { -#else +#elif PYTHON3 // When using Python3 always use the PyLong API { #endif @@ -511,7 +511,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, return true; case TypeCode.Byte: -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) @@ -522,7 +522,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, } goto type_error; } -#else +#elif PYTHON2 if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType)) { if (Runtime.PyString_Size(value) == 1) @@ -556,7 +556,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, return true; case TypeCode.SByte: -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) { op = Runtime.PyBytes_AS_STRING(value); @@ -565,7 +565,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, } goto type_error; } -#else +#elif PYTHON2 if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType)) { if (Runtime.PyString_Size(value) == 1) @@ -599,7 +599,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, return true; case TypeCode.Char: -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType)) { if (Runtime.PyBytes_Size(value) == 1) { op = Runtime.PyBytes_AS_STRING(value); @@ -608,7 +608,7 @@ static bool ToPrimitive(IntPtr value, Type obType, out Object result, } goto type_error; } -#else +#elif PYTHON2 if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType)) { if (Runtime.PyString_Size(value) == 1) @@ -935,4 +935,4 @@ public static PyObject ToPython(this object o) return new PyObject(Converter.ToPython(o, o?.GetType())); } } -} \ No newline at end of file +} diff --git a/src/runtime/delegateobject.cs b/src/runtime/delegateobject.cs index 19bbbe0e8..61e91942b 100644 --- a/src/runtime/delegateobject.cs +++ b/src/runtime/delegateobject.cs @@ -102,7 +102,7 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) //==================================================================== // Implements __cmp__ for reflected delegate types. //==================================================================== -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 public static new IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op) { if (op != Runtime.Py_EQ && op != Runtime.Py_NE) { @@ -131,7 +131,7 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) Runtime.XIncref(pyfalse); return pyfalse; } -#else +#elif PYTHON2 public static new int tp_compare(IntPtr ob, IntPtr other) { Delegate d1 = GetTrueDelegate(ob); @@ -144,4 +144,4 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw) } #endif } -} \ No newline at end of file +} diff --git a/src/runtime/exceptions.cs b/src/runtime/exceptions.cs index c15f634d5..429c09212 100644 --- a/src/runtime/exceptions.cs +++ b/src/runtime/exceptions.cs @@ -84,9 +84,9 @@ private Exceptions() internal static void Initialize() { -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 exceptions_module = Runtime.PyImport_ImportModule("builtins"); -#else +#elif PYTHON2 exceptions_module = Runtime.PyImport_ImportModule("exceptions"); #endif Exceptions.ErrorCheck(exceptions_module); @@ -164,7 +164,7 @@ internal static void SetArgsAndCause(IntPtr ob) Marshal.WriteIntPtr(ob, ExceptionOffset.args, args); -#if !(PYTHON25 || PYTHON26 || PYTHON27) +#if PYTHON3 if (e.InnerException != null) { IntPtr cause = CLRObject.GetInstHandle(e.InnerException); @@ -375,7 +375,7 @@ puplic static variables on the Exceptions class filled in from public static IntPtr Exception; public static IntPtr StopIteration; public static IntPtr GeneratorExit; -#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON2 public static IntPtr StandardError; #endif public static IntPtr ArithmeticError; diff --git a/src/runtime/importhook.cs b/src/runtime/importhook.cs index 7ebb13be0..bdef98c27 100644 --- a/src/runtime/importhook.cs +++ b/src/runtime/importhook.cs @@ -14,7 +14,7 @@ internal class ImportHook static CLRModule root; static MethodWrapper hook; -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 static IntPtr py_clr_module; static IntPtr module_def; #endif @@ -30,10 +30,10 @@ internal static void Initialize() // but it provides the most "Pythonic" way of dealing with CLR // modules (Python doesn't provide a way to emulate packages). IntPtr dict = Runtime.PyImport_GetModuleDict(); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 IntPtr mod = Runtime.PyImport_ImportModule("builtins"); py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); -#else +#elif PYTHON2 IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__"); py_import = Runtime.PyObject_GetAttrString(mod, "__import__"); #endif @@ -43,7 +43,7 @@ internal static void Initialize() root = new CLRModule(); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 // create a python module with the same methods as the clr module-like object module_def = ModuleDefOffset.AllocModuleDef("clr"); py_clr_module = Runtime.PyModule_Create2(module_def, 3); @@ -56,7 +56,7 @@ internal static void Initialize() Runtime.PyDict_Update(mod_dict, clr_dict); Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module); Runtime.PyDict_SetItemString(dict, "clr", py_clr_module); -#else +#elif PYTHON2 Runtime.XIncref(root.pyHandle); // we are using the module two times Runtime.PyDict_SetItemString(dict, "CLR", root.pyHandle); Runtime.PyDict_SetItemString(dict, "clr", root.pyHandle); @@ -70,13 +70,13 @@ internal static void Initialize() internal static void Shutdown() { -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 if (0 != Runtime.Py_IsInitialized()) { Runtime.XDecref(py_clr_module); Runtime.XDecref(root.pyHandle); } ModuleDefOffset.FreeModuleDef(module_def); -#else +#elif PYTHON2 if (0 != Runtime.Py_IsInitialized()) { Runtime.XDecref(root.pyHandle); @@ -95,7 +95,7 @@ internal static void Shutdown() public static IntPtr GetCLRModule(IntPtr? fromList = null) { root.InitializePreload(); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 // update the module dictionary with the contents of the root dictionary root.LoadNames(); IntPtr py_mod_dict = Runtime.PyModule_GetDict(py_clr_module); @@ -136,7 +136,7 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null) Runtime.XIncref(py_clr_module); return py_clr_module; -#else +#elif PYTHON2 Runtime.XIncref(root.pyHandle); return root.pyHandle; #endif @@ -368,4 +368,4 @@ public static IntPtr __import__(IntPtr self, IntPtr args, IntPtr kw) return mod.pyHandle; } } -} \ No newline at end of file +} diff --git a/src/runtime/interop.cs b/src/runtime/interop.cs index ca2955e48..336252ded 100644 --- a/src/runtime/interop.cs +++ b/src/runtime/interop.cs @@ -154,15 +154,13 @@ public static int Size() // (start after PyObject_HEAD) public static int dict = 0; public static int args = 0; -#if (PYTHON25 || PYTHON26 || PYTHON27) +#if PYTHON2 public static int message = 0; -#elif (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#elif PYTHON3 public static int traceback = 0; public static int context = 0; public static int cause = 0; -#if !PYTHON32 public static int suppress_context = 0; -#endif #endif // extra c# data @@ -171,7 +169,7 @@ public static int Size() } -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] internal class BytesOffset { @@ -270,7 +268,7 @@ public static void FreeModuleDef(IntPtr ptr) { /// internal class TypeFlags { -#if (PYTHON23 || PYTHON24 || PYTHON25 || PYTHON26 || PYTHON27) +#if PYTHON2 // these flags were removed in Python 3 public static int HaveGetCharBuffer = (1 << 0); public static int HaveSequenceIn = (1 << 1); @@ -309,8 +307,8 @@ internal class TypeFlags public static int BaseExceptionSubclass = (1 << 30); public static int TypeSubclass = (1 << 31); -// Default flags for Python 2 -#if (PYTHON23 || PYTHON24 || PYTHON25 || PYTHON26 || PYTHON27) +#if PYTHON2 + // Default flags for Python 2 public static int Default = ( HaveGetCharBuffer | HaveSequenceIn | @@ -320,14 +318,10 @@ internal class TypeFlags HaveIter | HaveClass | HaveStacklessExtension | -#if (PYTHON25 || PYTHON26 || PYTHON27) HaveIndex | -#endif 0); -#endif - -// Default flags for Python 3 -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#elif PYTHON3 + // Default flags for Python 3 public static int Default = ( HaveStacklessExtension | HaveVersionTag); @@ -390,7 +384,7 @@ static Interop() pmap["nb_add"] = p["BinaryFunc"]; pmap["nb_subtract"] = p["BinaryFunc"]; pmap["nb_multiply"] = p["BinaryFunc"]; -#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON2 pmap["nb_divide"] = p["BinaryFunc"]; #endif pmap["nb_remainder"] = p["BinaryFunc"]; @@ -415,7 +409,7 @@ static Interop() pmap["nb_inplace_add"] = p["BinaryFunc"]; pmap["nb_inplace_subtract"] = p["BinaryFunc"]; pmap["nb_inplace_multiply"] = p["BinaryFunc"]; -#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON2 pmap["nb_inplace_divide"] = p["BinaryFunc"]; #endif pmap["nb_inplace_remainder"] = p["BinaryFunc"]; diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index 79e132c70..4c676d375 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -186,9 +186,9 @@ public static void Initialize() // CPython interpreter process - this bootstraps the managed runtime // when it is imported by the CLR extension module. //==================================================================== -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 public static IntPtr InitExt() { -#else +#elif PYTHON2 public static void InitExt() { #endif @@ -234,12 +234,12 @@ public static void InitExt() catch (PythonException e) { e.Restore(); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 return IntPtr.Zero; #endif } -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 return Python.Runtime.ImportHook.GetCLRModule(); #endif } @@ -501,4 +501,4 @@ public static PyObject Import(string name) return PythonEngine.ImportModule(name); } } -} \ No newline at end of file +} diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index d88c30f0f..80f62df49 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -7,7 +7,7 @@ #endif -#if (UCS2 && (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)) +#if (UCS2 && PYTHON3) using System.Text; #endif @@ -241,10 +241,10 @@ internal static void Initialize() Runtime.PyEval_InitThreads(); } -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 IntPtr op = Runtime.PyImport_ImportModule("builtins"); IntPtr dict = Runtime.PyObject_GetAttrString(op, "__dict__"); -#else +#elif PYTHON2 IntPtr dict = Runtime.PyImport_GetModuleDict(); IntPtr op = Runtime.PyDict_GetItemString(dict, "__builtin__"); #endif @@ -272,7 +272,7 @@ internal static void Initialize() PyWrapperDescriptorType = Runtime.PyObject_Type(op); Runtime.XDecref(op); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 Runtime.XDecref(dict); #endif @@ -284,7 +284,7 @@ internal static void Initialize() PyUnicodeType = Runtime.PyObject_Type(op); Runtime.XDecref(op); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 op = Runtime.PyBytes_FromString("bytes"); PyBytesType = Runtime.PyObject_Type(op); Runtime.XDecref(op); @@ -314,10 +314,10 @@ internal static void Initialize() PyFloatType = Runtime.PyObject_Type(op); Runtime.XDecref(op); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 PyClassType = IntPtr.Zero; PyInstanceType = IntPtr.Zero; -#else +#elif PYTHON2 IntPtr s = Runtime.PyString_FromString("_temp"); IntPtr d = Runtime.PyDict_New(); @@ -335,7 +335,7 @@ internal static void Initialize() Error = new IntPtr(-1); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 IntPtr dll = IntPtr.Zero; if ("__Internal" != Runtime.dll) { NativeMethods.LoadLibrary(Runtime.dll); @@ -406,7 +406,7 @@ internal static int AtExit() internal static IntPtr PyNoneType; internal static IntPtr PyTypeType; -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 internal static IntPtr PyBytesType; internal static IntPtr _PyObject_NextNotImplemented; #endif @@ -545,7 +545,7 @@ internal static Type[] PythonArgsToTypeArray(IntPtr arg, bool mangleObjects) internal unsafe static void XIncref(IntPtr op) { #if (Py_DEBUG) - // according to Python doc, Py_IncRef() is Py_XINCREF() + // according to Python doc, Py_IncRef() is Py_XINCREF() Py_IncRef(op); return; #else @@ -707,12 +707,12 @@ internal unsafe static extern void internal unsafe static extern IntPtr PyGILState_GetThisThreadState(); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] public unsafe static extern int Py_Main(int argc, [MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)] string[] argv); -#else +#elif PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] public unsafe static extern int @@ -775,7 +775,7 @@ internal unsafe static extern IntPtr PyEval_GetLocals(); -#if PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36 +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] [return: MarshalAs(UnmanagedType.LPWStr)] @@ -808,7 +808,7 @@ internal unsafe static extern string ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern void Py_SetPath([MarshalAsAttribute(UnmanagedType.LPWStr)]string home); -#else +#elif PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern string @@ -1026,7 +1026,7 @@ internal unsafe static extern IntPtr internal unsafe static extern IntPtr PyObject_CallObject(IntPtr pointer, IntPtr args); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern int @@ -1055,7 +1055,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) { Exceptions.SetError(Exceptions.SystemError, "Error comparing objects"); return -1; } -#else +#elif PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern int @@ -1108,13 +1108,13 @@ internal unsafe static extern IntPtr internal unsafe static extern IntPtr PyObject_Str(IntPtr pointer); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, EntryPoint="PyObject_Str", ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern IntPtr PyObject_Unicode(IntPtr pointer); -#else +#elif PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern IntPtr @@ -1131,13 +1131,13 @@ internal unsafe static extern IntPtr // Python number API //==================================================================== -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, EntryPoint = "PyNumber_Long", ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern IntPtr PyNumber_Int(IntPtr ob); -#else // Python 2 +#elif PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] @@ -1184,7 +1184,7 @@ internal static IntPtr PyInt_FromInt64(long value) return PyInt_FromLong(v); } -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyLong_FromLong", ExactSpelling = true, CharSet = CharSet.Ansi)] @@ -1208,7 +1208,7 @@ internal unsafe static extern IntPtr ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern int PyInt_GetMax(); -#else // Python 2 +#elif PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] @@ -1532,7 +1532,7 @@ internal static IntPtr PyString_FromString(string value) return PyString_FromStringAndSize(value, value.Length); } -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern IntPtr @@ -1704,7 +1704,7 @@ internal unsafe static string GetManagedString(IntPtr op) IntPtr type = PyObject_TYPE(op); // Python 3 strings are all unicode -#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON2 if (type == Runtime.PyStringType) { return Marshal.PtrToStringAnsi( @@ -1828,7 +1828,7 @@ internal unsafe static string GetManagedString(IntPtr op) IntPtr type = PyObject_TYPE(op); // Python 3 strings are all unicode -#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON2 if (type == Runtime.PyStringType) { return Marshal.PtrToStringAnsi( @@ -2046,12 +2046,12 @@ internal unsafe static extern int // Python iterator API //==================================================================== -#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern bool PyIter_Check(IntPtr pointer); -#else +#elif PYTHON3 internal static bool PyIter_Check(IntPtr pointer) { @@ -2090,7 +2090,7 @@ internal unsafe static extern IntPtr internal unsafe static extern string PyModule_GetFilename(IntPtr module); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Ansi)] internal unsafe static extern IntPtr @@ -2319,4 +2319,4 @@ internal unsafe static extern IntPtr internal unsafe static extern IntPtr PyMethod_Function(IntPtr ob); } -} \ No newline at end of file +} diff --git a/src/runtime/typemanager.cs b/src/runtime/typemanager.cs index 6f1eb604f..40e60336d 100644 --- a/src/runtime/typemanager.cs +++ b/src/runtime/typemanager.cs @@ -407,21 +407,21 @@ 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 (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 // For python3 we leak two objects. One for the ascii representation // required for tp_name, and another for the unicode representation // for ht_name. IntPtr temp = Runtime.PyBytes_FromString(name); IntPtr raw = Runtime.PyBytes_AS_STRING(temp); temp = Runtime.PyUnicode_FromString(name); -#else +#elif PYTHON2 IntPtr temp = Runtime.PyString_FromString(name); IntPtr raw = Runtime.PyString_AS_STRING(temp); #endif Marshal.WriteIntPtr(type, TypeOffset.tp_name, raw); Marshal.WriteIntPtr(type, TypeOffset.name, temp); -#if (PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 Marshal.WriteIntPtr(type, TypeOffset.qualname, temp); #endif @@ -436,10 +436,10 @@ internal static IntPtr AllocateTypeObject(string name) temp = new IntPtr(ptr + TypeOffset.mp_length); Marshal.WriteIntPtr(type, TypeOffset.tp_as_mapping, temp); -#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if PYTHON3 temp = new IntPtr(ptr + TypeOffset.bf_getbuffer); Marshal.WriteIntPtr(type, TypeOffset.tp_as_buffer, temp); -#else +#elif PYTHON2 temp = new IntPtr(ptr + TypeOffset.bf_getreadbuffer); Marshal.WriteIntPtr(type, TypeOffset.tp_as_buffer, temp); #endif @@ -544,4 +544,4 @@ internal static void CopySlot(IntPtr from, IntPtr to, int offset) Marshal.WriteIntPtr(to, offset, fp); } } -} \ No newline at end of file +}