From cdb9a42d6168811885d87f2f07f00e729f93af6e Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Thu, 12 Jan 2017 13:58:27 -0700 Subject: [PATCH 1/5] Remove unreachable code from PY32 That entire section is only for PY32 compatiblity --- src/runtime/runtime.cs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 80f62df49..b7451ecd1 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1563,27 +1563,11 @@ internal static IntPtr PyString_FromStringAndSize(string value, int length) } } -#if (PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Unicode)] internal unsafe static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size); -#elif (UCS2) - [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, - EntryPoint = "PyUnicodeUCS2_FromStringAndSize", - ExactSpelling = true, CharSet = CharSet.Unicode)] - internal unsafe static extern IntPtr - PyUnicode_FromStringAndSize(IntPtr value, int size); -#else - [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, - EntryPoint = "PyUnicodeUCS4_FromStringAndSize", - ExactSpelling = true, CharSet = CharSet.Ansi)] - internal unsafe static extern IntPtr - PyUnicode_FromStringAndSize(IntPtr value, int size); -#endif - -#else // Python2x - +#elif PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, CharSet = CharSet.Ansi)] internal unsafe static extern IntPtr From 5357401a4158d222582fbb18bd715e2f188cdbb6 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Thu, 12 Jan 2017 14:05:50 -0700 Subject: [PATCH 2/5] Refactor repeated section from UCS4/UCS2 --- src/runtime/runtime.cs | 42 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index b7451ecd1..46a0d2d91 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1677,37 +1677,6 @@ internal unsafe static extern IntPtr internal unsafe static extern IntPtr PyUnicode_FromOrdinal(int c); #endif - - internal static IntPtr PyUnicode_FromString(string s) - { - return PyUnicode_FromUnicode(s, (s.Length)); - } - - internal unsafe static string GetManagedString(IntPtr op) - { - IntPtr type = PyObject_TYPE(op); - -// Python 3 strings are all unicode -#if PYTHON2 - if (type == Runtime.PyStringType) - { - return Marshal.PtrToStringAnsi( - PyString_AS_STRING(op), - Runtime.PyString_Size(op) - ); - } -#endif - - if (type == Runtime.PyUnicodeType) - { - char* p = Runtime.PyUnicode_AsUnicode(op); - int size = Runtime.PyUnicode_GetSize(op); - return new String(p, 0, size); - } - - return null; - } - #endif #if (UCS4) #if (PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) @@ -1800,6 +1769,7 @@ internal unsafe static extern IntPtr internal unsafe static extern IntPtr PyUnicode_FromOrdinal(int c); +#endif #endif internal static IntPtr PyUnicode_FromString(string s) @@ -1811,8 +1781,7 @@ internal unsafe static string GetManagedString(IntPtr op) { IntPtr type = PyObject_TYPE(op); -// Python 3 strings are all unicode -#if PYTHON2 +#if PYTHON2 // Python 3 strings are all Unicode if (type == Runtime.PyStringType) { return Marshal.PtrToStringAnsi( @@ -1824,17 +1793,22 @@ internal unsafe static string GetManagedString(IntPtr op) if (type == Runtime.PyUnicodeType) { +#if UCS4 IntPtr p = Runtime.PyUnicode_AsUnicode(op); int length = Runtime.PyUnicode_GetSize(op); int size = length*4; byte[] buffer = new byte[size]; Marshal.Copy(p, buffer, 0, size); return Encoding.UTF32.GetString(buffer, 0, size); +#elif UCS2 + char* p = Runtime.PyUnicode_AsUnicode(op); + int size = Runtime.PyUnicode_GetSize(op); + return new String(p, 0, size); +#endif } return null; } -#endif //==================================================================== // Python dictionary API From 4f298019722c3b71a3dccfa4ea0ceff9ff1a8b1d Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Sun, 29 Jan 2017 13:24:47 -0700 Subject: [PATCH 3/5] Clarify nested UCS2/4 PY2/3 directives Clearly shows what each section does --- src/runtime/runtime.cs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 46a0d2d91..84f237f30 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1590,8 +1590,7 @@ internal static bool PyUnicode_Check(IntPtr ob) return PyObject_TYPE(ob) == Runtime.PyUnicodeType; } -#if (UCS2) -#if (PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#if UCS2 && PYTHON3 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Unicode)] internal unsafe static extern IntPtr @@ -1633,8 +1632,7 @@ internal unsafe static extern IntPtr ExactSpelling = true, CharSet = CharSet.Unicode)] internal unsafe static extern IntPtr PyUnicode_FromOrdinal(int c); - -#else +#elif UCS2 && PYTHON2 [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, EntryPoint="PyUnicodeUCS2_FromObject", ExactSpelling=true, CharSet=CharSet.Unicode)] @@ -1676,10 +1674,7 @@ internal unsafe static extern IntPtr ExactSpelling=true, CharSet=CharSet.Unicode)] internal unsafe static extern IntPtr PyUnicode_FromOrdinal(int c); -#endif -#endif -#if (UCS4) -#if (PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) +#elif UCS4 && PYTHON3 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, ExactSpelling=true, CharSet=CharSet.Unicode)] internal unsafe static extern IntPtr @@ -1723,8 +1718,7 @@ internal unsafe static extern IntPtr ExactSpelling = true, CharSet = CharSet.Unicode)] internal unsafe static extern IntPtr PyUnicode_FromOrdinal(int c); - -#else +#elif UCS4 && PYTHON2 [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyUnicodeUCS4_FromObject", ExactSpelling = true, CharSet = CharSet.Unicode)] @@ -1768,8 +1762,6 @@ internal unsafe static extern IntPtr ExactSpelling = true, CharSet = CharSet.Unicode)] internal unsafe static extern IntPtr PyUnicode_FromOrdinal(int c); - -#endif #endif internal static IntPtr PyUnicode_FromString(string s) From 94b9e257eda8a6ce9af08091f3eea8f8ec9201d4 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Sun, 29 Jan 2017 13:40:28 -0700 Subject: [PATCH 4/5] Refactor Python version directives Added Python37 definition, won't add interop until final release --- src/runtime/assemblyinfo.cs | 15 +++--- src/runtime/runtime.cs | 96 +++++++++++-------------------------- 2 files changed, 35 insertions(+), 76 deletions(-) diff --git a/src/runtime/assemblyinfo.cs b/src/runtime/assemblyinfo.cs index 49433f0dc..1defa851f 100644 --- a/src/runtime/assemblyinfo.cs +++ b/src/runtime/assemblyinfo.cs @@ -15,20 +15,19 @@ #if PYTHON27 [assembly: AssemblyTitle("Python.Runtime for Python 2.7")] [assembly: AssemblyDescription("Python Runtime for Python 2.7")] -#endif -#if PYTHON33 +#elif PYTHON33 [assembly: AssemblyTitle("Python.Runtime for Python 3.3")] [assembly: AssemblyDescription("Python Runtime for Python 3.3")] -#endif -#if PYTHON34 +#elif PYTHON34 [assembly: AssemblyTitle("Python.Runtime for Python 3.4")] [assembly: AssemblyDescription("Python Runtime for Python 3.4")] -#endif -#if PYTHON35 +#elif PYTHON35 [assembly: AssemblyTitle("Python.Runtime for Python 3.5")] [assembly: AssemblyDescription("Python Runtime for Python 3.5")] -#endif -#if PYTHON36 +#elif PYTHON36 [assembly: AssemblyTitle("Python.Runtime for Python 3.6")] [assembly: AssemblyDescription("Python Runtime for Python 3.6")] +#elif PYTHON37 +[assembly: AssemblyTitle("Python.Runtime for Python 3.7")] +[assembly: AssemblyDescription("Python Runtime for Python 3.7")] #endif diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 84f237f30..4a67e8c65 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -103,96 +103,56 @@ public class Runtime #error You must define either UCS2 or UCS4! #endif -#if (PYTHON23) - public const string pyversion = "2.3"; - public const int pyversionnumber = 23; -#endif -#if (PYTHON24) - public const string pyversion = "2.4"; - public const int pyversionnumber = 24; -#endif -#if (PYTHON25) - public const string pyversion = "2.5"; - public const int pyversionnumber = 25; -#endif -#if (PYTHON26) - public const string pyversion = "2.6"; - public const int pyversionnumber = 26; -#endif -#if (PYTHON27) +#if PYTHON27 public const string pyversion = "2.7"; public const int pyversionnumber = 27; -#endif -#if (PYTHON32) - public const string pyversion = "3.2"; - public const int pyversionnumber = 32; -#endif -#if (PYTHON33) +#elif PYTHON33 public const string pyversion = "3.3"; public const int pyversionnumber = 33; -#endif -#if (PYTHON34) +#elif PYTHON34 public const string pyversion = "3.4"; public const int pyversionnumber = 34; -#endif -#if (PYTHON35) +#elif PYTHON35 public const string pyversion = "3.5"; public const int pyversionnumber = 35; -#endif -#if (PYTHON36) +#elif PYTHON36 public const string pyversion = "3.6"; public const int pyversionnumber = 36; -#endif -#if ! (PYTHON23 || PYTHON24 || PYTHON25 || PYTHON26 || PYTHON27 || PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36) -#error You must define one of PYTHON23 to PYTHON36 +#elif PYTHON37 + // TODO: Add interop37 after Python3.7 is released + public const string pyversion = "3.7"; + public const int pyversionnumber = 37; +#else +#error You must define one of PYTHON33 to PYTHON37 or PYTHON27 #endif -#if (PYTHON23) - internal const string dllBase = "python23"; -#endif -#if (PYTHON24) - internal const string dllBase = "python24"; -#endif -#if (PYTHON25) - internal const string dllBase = "python25"; -#endif -#if (PYTHON26) - internal const string dllBase = "python26"; -#endif -#if (PYTHON27) +#if MONO_LINUX || MONO_OSX +#if PYTHON27 internal const string dllBase = "python27"; -#endif -#if (MONO_LINUX || MONO_OSX) -#if (PYTHON32) - internal const string dllBase = "python3.2"; -#endif -#if (PYTHON33) +#elif PYTHON33 internal const string dllBase = "python3.3"; -#endif -#if (PYTHON34) +#elif PYTHON34 internal const string dllBase = "python3.4"; -#endif -#if (PYTHON35) +#elif PYTHON35 internal const string dllBase = "python3.5"; -#endif -#if (PYTHON36) +#elif PYTHON36 internal const string dllBase = "python3.6"; +#elif PYTHON37 + internal const string dllBase = "python3.7"; #endif -#else -#if (PYTHON32) - internal const string dllBase = "python32"; -#endif -#if (PYTHON33) +#else // Windows +#if PYTHON27 + internal const string dllBase = "python27"; +#elif PYTHON33 internal const string dllBase = "python33"; -#endif -#if (PYTHON34) +#elif PYTHON34 internal const string dllBase = "python34"; -#endif -#if (PYTHON35) +#elif PYTHON35 internal const string dllBase = "python35"; -#endif -#if (PYTHON36) +#elif PYTHON36 internal const string dllBase = "python36"; +#elif PYTHON37 + internal const string dllBase = "python37"; #endif #endif From dde711495055c2f77d705ef253bf9c2bd1cbb664 Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Sun, 29 Jan 2017 13:41:27 -0700 Subject: [PATCH 5/5] Misc directives in runtime --- src/runtime/runtime.cs | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 4a67e8c65..5f0ecfbd2 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1,13 +1,10 @@ using System; using System.Runtime.InteropServices; using System.Security; -#if (UCS4) +#if UCS4 using System.Text; using Mono.Unix; - -#endif - -#if (UCS2 && PYTHON3) +#elif UCS2 && PYTHON3 using System.Text; #endif @@ -16,7 +13,7 @@ namespace Python.Runtime [SuppressUnmanagedCodeSecurityAttribute()] static class NativeMethods { -#if (MONO_LINUX || MONO_OSX) +#if MONO_LINUX || MONO_OSX static public IntPtr LoadLibrary(string fileName) { return dlopen(fileName, RTLD_NOW | RTLD_SHARED); } @@ -40,7 +37,7 @@ static public IntPtr GetProcAddress(IntPtr dllHandle, string name) { return res; } -#if (MONO_OSX) +#if MONO_OSX static int RTLD_NOW = 0x2; static int RTLD_SHARED = 0x20; static IntPtr RTLD_DEFAULT = new IntPtr(-2); @@ -56,7 +53,7 @@ static public IntPtr GetProcAddress(IntPtr dllHandle, string name) { [DllImport("__Internal")] private static extern IntPtr dlerror(); -#else +#elif MONO_LINUX static int RTLD_NOW = 0x2; static int RTLD_SHARED = 0x20; static IntPtr RTLD_DEFAULT = IntPtr.Zero; @@ -74,7 +71,7 @@ static public IntPtr GetProcAddress(IntPtr dllHandle, string name) { private static extern IntPtr dlerror(); #endif -#else +#else // Windows [DllImport("kernel32.dll")] public static extern IntPtr LoadLibrary(string dllToLoad); @@ -93,13 +90,11 @@ public class Runtime /// the responsibility of the caller to have acquired the GIL /// before calling any of these methods. /// -#if (UCS4) +#if UCS4 public const int UCS = 4; -#endif -#if (UCS2) +#elif UCS2 public const int UCS = 2; -#endif -#if ! (UCS2 || UCS4) +#else #error You must define either UCS2 or UCS4! #endif @@ -156,23 +151,23 @@ public class Runtime #endif #endif -#if (PYTHON_WITH_PYDEBUG) +#if PYTHON_WITH_PYDEBUG internal const string dllWithPyDebug = "d"; #else internal const string dllWithPyDebug = ""; #endif -#if (PYTHON_WITH_PYMALLOC) +#if PYTHON_WITH_PYMALLOC internal const string dllWithPyMalloc = "m"; #else internal const string dllWithPyMalloc = ""; #endif -#if (PYTHON_WITH_WIDE_UNICODE) +#if PYTHON_WITH_WIDE_UNICODE internal const string dllWithWideUnicode = "u"; #else internal const string dllWithWideUnicode = ""; #endif -#if (PYTHON_WITHOUT_ENABLE_SHARED) +#if PYTHON_WITHOUT_ENABLE_SHARED public const string dll = "__Internal"; #else public const string dll = dllBase + dllWithPyDebug + dllWithPyMalloc + dllWithWideUnicode; @@ -504,7 +499,7 @@ internal static Type[] PythonArgsToTypeArray(IntPtr arg, bool mangleObjects) internal unsafe static void XIncref(IntPtr op) { -#if (Py_DEBUG) +#if Py_DEBUG // according to Python doc, Py_IncRef() is Py_XINCREF() Py_IncRef(op); return; @@ -526,7 +521,7 @@ internal unsafe static void XIncref(IntPtr op) internal static unsafe void XDecref(IntPtr op) { -#if (Py_DEBUG) +#if Py_DEBUG // Py_DecRef calls Python's Py_DECREF // according to Python doc, Py_DecRef() is Py_XDECREF() Py_DecRef(op); @@ -581,7 +576,7 @@ internal unsafe static long Refcount(IntPtr op) return 0; } -#if (Py_DEBUG) +#if Py_DEBUG // Py_IncRef and Py_DecRef are taking care of the extra payload // in Py_DEBUG builds of Python like _Py_RefTotal [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, @@ -892,7 +887,7 @@ internal unsafe static IntPtr { return IntPtr.Zero; } -#if (Py_DEBUG) +#if Py_DEBUG int n = 3; #else int n = 1;