From 8bbf07cf5c0b015cf21e80db767349a37fde8565 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 15:11:27 +0400 Subject: [PATCH 01/14] DotNet core migration first files. --- pythonnet.15.sln | 28 +++++++++++++++++++++++ src/runtime/Python.Runtime.15.csproj | 33 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 pythonnet.15.sln create mode 100644 src/runtime/Python.Runtime.15.csproj diff --git a/pythonnet.15.sln b/pythonnet.15.sln new file mode 100644 index 000000000..546628591 --- /dev/null +++ b/pythonnet.15.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26228.9 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Runtime.15", "src\runtime\Python.Runtime.15.csproj", "{4D2089E1-5B6A-4799-984B-178AE467902D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug-Linux-x64|x64 = Debug-Linux-x64|x64 + Debug-Win-x64|x64 = Debug-Win-x64|x64 + Release-Linux-x64|x64 = Release-Linux-x64|x64 + Release-Win-x64|x64 = Release-Win-x64|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.ActiveCfg = Release|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.Build.0 = Debug|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.ActiveCfg = Release-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.Build.0 = Release-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.ActiveCfg = Release|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.Build.0 = Release|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.ActiveCfg = Release|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/runtime/Python.Runtime.15.csproj b/src/runtime/Python.Runtime.15.csproj new file mode 100644 index 000000000..902fc665d --- /dev/null +++ b/src/runtime/Python.Runtime.15.csproj @@ -0,0 +1,33 @@ + + + + netstandard1.4;net40 + Python.Runtime + Python.Runtime + Python.Runtime + 3.0.0-* + + + x64 + TRACE;DEBUG;X64;LINUX;UNIX;NET40;UCS2; + + + + x64 + TRACE;DEBUG;X64;LINUX;UNIX;NET40;UCS2;UCS2; + + + + x64 + TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_4;UCS2; + + + + x64 + TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_4;UCS2; + + + $(DefineConstants);PYTHON3; PYTHON35 + + + \ No newline at end of file From d279a59f72456d0f990fabeccc4c11d34e7cd6d5 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 16:56:53 +0400 Subject: [PATCH 02/14] Net 4.0 compilation fixed. NetStandard 1.5 dependencies added. --- pythonnet.15.sln | 4 +- src/runtime/Python.Runtime.15.csproj | 55 +++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/pythonnet.15.sln b/pythonnet.15.sln index 546628591..2384f8e5a 100644 --- a/pythonnet.15.sln +++ b/pythonnet.15.sln @@ -15,8 +15,8 @@ Global GlobalSection(ProjectConfigurationPlatforms) = postSolution {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.ActiveCfg = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.Build.0 = Debug|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.ActiveCfg = Release-Win-x64|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.Build.0 = Release-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.ActiveCfg = Debug-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.Build.0 = Debug-Win-x64|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.ActiveCfg = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.Build.0 = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.ActiveCfg = Release|x64 diff --git a/src/runtime/Python.Runtime.15.csproj b/src/runtime/Python.Runtime.15.csproj index 902fc665d..03afca84d 100644 --- a/src/runtime/Python.Runtime.15.csproj +++ b/src/runtime/Python.Runtime.15.csproj @@ -1,33 +1,70 @@  - netstandard1.4;net40 + net40;netstandard1.5 Python.Runtime Python.Runtime Python.Runtime - 3.0.0-* + 3.0.0 + Alpha + false + false + True + + 1.6.0 - + x64 TRACE;DEBUG;X64;LINUX;UNIX;NET40;UCS2; x64 - TRACE;DEBUG;X64;LINUX;UNIX;NET40;UCS2;UCS2; + TRACE;X64;LINUX;UNIX;NET40;UCS2; - + x64 - TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_4;UCS2; + TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_5;UCS2; - + x64 - TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_4;UCS2; + TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_5;UCS2; + $(DefineConstants);PYTHON3; PYTHON35 - + + + + 2.0.11 + + + 4.0.1 + + + 4.1.0 + + + 4.0.1 + + + 4.0.11 + + + 4.0.1 + + + 4.1.0 + + + 4.0.0 + + + 4.0.0 + + + \ No newline at end of file From c65477122cdaf97d5c69e3a61a510504fc116e96 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 18:18:06 +0400 Subject: [PATCH 03/14] Adoption for .Net Standard 1.5 completed, except ICustomMarshaler workaround. --- src/runtime/Python.Runtime.15.csproj | 7 +++++ src/runtime/assemblymanager.cs | 32 +++++++++++++++++++--- src/runtime/converter.cs | 4 ++- src/runtime/polyfill/ICustomMarshaler.cs | 34 ++++++++++++++++++++++++ src/runtime/propertyobject.cs | 8 +++--- src/runtime/runtime.cs | 9 ++++++- 6 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 src/runtime/polyfill/ICustomMarshaler.cs diff --git a/src/runtime/Python.Runtime.15.csproj b/src/runtime/Python.Runtime.15.csproj index 03afca84d..e1d485046 100644 --- a/src/runtime/Python.Runtime.15.csproj +++ b/src/runtime/Python.Runtime.15.csproj @@ -62,9 +62,16 @@ 4.0.0 + + 4.0.0-rc3-24212-01 + 4.0.0 + + + + \ No newline at end of file diff --git a/src/runtime/assemblymanager.cs b/src/runtime/assemblymanager.cs index 06a4449a2..ad287eba8 100644 --- a/src/runtime/assemblymanager.cs +++ b/src/runtime/assemblymanager.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; @@ -6,6 +6,9 @@ using System.IO; using System.Reflection; using System.Threading; +#if NETSTANDARD1_5 +using System.Runtime.Loader; +#endif namespace Python.Runtime { @@ -19,7 +22,9 @@ internal class AssemblyManager // therefore this should be a ConcurrentDictionary private static ConcurrentDictionary> namespaces; //private static Dictionary> generics; +#if !NETSTANDARD1_5 private static AssemblyLoadEventHandler lhandler; +#endif private static ResolveEventHandler rhandler; // updated only under GIL? @@ -48,9 +53,10 @@ internal static void Initialize() AppDomain domain = AppDomain.CurrentDomain; +#if !NETSTANDARD1_5 lhandler = new AssemblyLoadEventHandler(AssemblyLoadHandler); domain.AssemblyLoad += lhandler; - +#endif rhandler = new ResolveEventHandler(ResolveHandler); domain.AssemblyResolve += rhandler; @@ -76,11 +82,13 @@ internal static void Initialize() internal static void Shutdown() { AppDomain domain = AppDomain.CurrentDomain; +#if !NETSTANDARD1_5 domain.AssemblyLoad -= lhandler; +#endif domain.AssemblyResolve -= rhandler; } - +#if !NETSTANDARD1_5 /// /// Event handler for assembly load events. At the time the Python /// runtime loads, we scan the app domain to map the assemblies that @@ -94,7 +102,7 @@ private static void AssemblyLoadHandler(object ob, AssemblyLoadEventArgs args) assemblies.Add(assembly); ScanAssembly(assembly); } - +#endif /// /// Event handler for assembly resolve events. This is needed because @@ -196,7 +204,11 @@ public static Assembly LoadAssembly(string name) Assembly assembly = null; try { +#if NETSTANDARD1_5 + assembly = Assembly.Load(new AssemblyName(name)); +#else assembly = Assembly.Load(name); +#endif } catch (Exception) { @@ -220,7 +232,11 @@ public static Assembly LoadAssemblyPath(string name) { try { +#if NETSTANDARD1_5 + assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(path); +#else assembly = Assembly.LoadFrom(path); +#endif } catch (Exception) { @@ -247,7 +263,11 @@ public static Assembly LoadAssemblyFullPath(string name) { try { +#if NETSTANDARD1_5 + assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(name); +#else assembly = Assembly.LoadFrom(name); +#endif } catch (Exception) { @@ -364,7 +384,11 @@ internal static void ScanAssembly(Assembly assembly) namespaces[ns].TryAdd(assembly, string.Empty); } +#if !NETSTANDARD1_5 if (ns != null && t.IsGenericTypeDefinition) +#else + if (ns != null && t.GetTypeInfo().IsGenericTypeDefinition) +#endif { GenericUtil.Register(t); } diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index e68767da9..e0566f5d2 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Globalization; @@ -11,7 +11,9 @@ namespace Python.Runtime /// /// Performs data conversions between managed types and Python types. /// +#if !NETSTANDARD1_5 [SuppressUnmanagedCodeSecurity] +#endif internal class Converter { private Converter() diff --git a/src/runtime/polyfill/ICustomMarshaler.cs b/src/runtime/polyfill/ICustomMarshaler.cs new file mode 100644 index 000000000..0b295a6de --- /dev/null +++ b/src/runtime/polyfill/ICustomMarshaler.cs @@ -0,0 +1,34 @@ +// This is copy from https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Runtime/InteropServices/ICustomMarshaler.cs + +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +/*============================================================================= +** +** +** +** Purpose: This the base interface that must be implemented by all custom +** marshalers. +** +** +=============================================================================*/ + +#if NETSTANDARD1_5 +using System; +namespace System.Runtime.InteropServices +{ + public interface ICustomMarshaler + { + Object MarshalNativeToManaged(IntPtr pNativeData); + + IntPtr MarshalManagedToNative(Object ManagedObj); + + void CleanUpNativeData(IntPtr pNativeData); + + void CleanUpManagedData(Object ManagedObj); + + int GetNativeDataSize(); + } +} +#endif \ No newline at end of file diff --git a/src/runtime/propertyobject.cs b/src/runtime/propertyobject.cs index f2c97f163..5c76964a7 100644 --- a/src/runtime/propertyobject.cs +++ b/src/runtime/propertyobject.cs @@ -1,7 +1,8 @@ -using System; +using System; using System.Reflection; +#if !NETSTANDARD1_5 using System.Security.Permissions; - +#endif namespace Python.Runtime { /// @@ -12,8 +13,9 @@ internal class PropertyObject : ExtensionType private PropertyInfo info; private MethodInfo getter; private MethodInfo setter; - +#if !NETSTANDARD1_5 [StrongNameIdentityPermission(SecurityAction.Assert)] +#endif public PropertyObject(PropertyInfo md) { getter = md.GetGetMethod(true); diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 346ea745f..f5af0c8c6 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -1,11 +1,14 @@ -using System; +using System; using System.Runtime.InteropServices; using System.Security; using System.Text; +using ReflectionBridge.Extensions; namespace Python.Runtime { +#if !NETSTANDARD1_5 [SuppressUnmanagedCodeSecurity] +#endif internal static class NativeMethods { #if MONO_LINUX || MONO_OSX @@ -297,7 +300,11 @@ internal static void Initialize() // Need to add the runtime directory to sys.path so that we // can find built-in assemblies like System.Data, et. al. +#if NETSTANDARD1_5 + string rtdir = typeof(string).GetAssembly().Location; +#else string rtdir = RuntimeEnvironment.GetRuntimeDirectory(); +#endif IntPtr path = PySys_GetObject("path"); IntPtr item = PyString_FromString(rtdir); PyList_Append(path, item); From 4e19934d2499f0ce2bc011775194f40630aa3564 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 19:06:48 +0400 Subject: [PATCH 04/14] Custom Marshaler workaround for NetStandard 1.5 implemented. --- src/runtime/runtime.cs | 114 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index f5af0c8c6..0a2bc4317 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -632,11 +632,33 @@ internal static unsafe long Refcount(IntPtr op) internal static extern IntPtr PyGILState_GetThisThreadState(); #if PYTHON3 +#if NETSTANDARD1_5 + [DllImport(PythonDll)] + private static extern int Py_Main( + int argc, + IntPtr argv + ); + + public static int Py_Main( + int argc, + string[] argv + ) + { + var marshaler = StrArrayMarshaler.GetInstance(string.Empty); + var unmanagedDataPtr = marshaler.MarshalManagedToNative(argv); + + int result = Py_Main(argc, unmanagedDataPtr); + + marshaler.CleanUpNativeData(unmanagedDataPtr); + return result; + } +#else [DllImport(PythonDll)] public static extern int Py_Main( int argc, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrArrayMarshaler))] string[] argv ); +#endif #elif PYTHON2 [DllImport(PythonDll)] public static extern int Py_Main(int argc, string[] argv); @@ -1168,11 +1190,33 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob) return ob + BytesOffset.ob_sval; } +#if NETSTANDARD1_5 + [DllImport(PythonDll, EntryPoint = "PyUnicode_FromStringAndSize")] + private static extern IntPtr PyString_FromStringAndSize( + IntPtr value, + int size + ); + + internal static IntPtr PyString_FromStringAndSize( + string value, + int size + ) + { + var marshaler = Utf8Marshaler.GetInstance(string.Empty); + var unmanagedDataPtr = marshaler.MarshalManagedToNative(value); + + IntPtr result = PyString_FromStringAndSize(unmanagedDataPtr, size); + + marshaler.CleanUpNativeData(unmanagedDataPtr); + return result; + } +#else [DllImport(PythonDll, EntryPoint = "PyUnicode_FromStringAndSize")] internal static extern IntPtr PyString_FromStringAndSize( [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value, int size ); +#endif [DllImport(PythonDll)] internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size); @@ -1199,12 +1243,36 @@ internal static bool PyUnicode_Check(IntPtr ob) [DllImport(PythonDll)] internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err); +#if NETSTANDARD1_5 + [DllImport(PythonDll)] + private static extern IntPtr PyUnicode_FromKindAndData( + int kind, + IntPtr s, + int size + ); + + internal static IntPtr PyUnicode_FromKindAndData( + int kind, + string s, + int size + ) + { + var marshaler = UcsMarshaler.GetInstance(string.Empty); + var unmanagedDataPtr = marshaler.MarshalManagedToNative(s); + + IntPtr result = PyUnicode_FromKindAndData(kind, unmanagedDataPtr, size); + + marshaler.CleanUpNativeData(unmanagedDataPtr); + return result; + } +#else [DllImport(PythonDll)] internal static extern IntPtr PyUnicode_FromKindAndData( int kind, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UcsMarshaler))] string s, int size ); +#endif internal static IntPtr PyUnicode_FromUnicode(string s, int size) { @@ -1226,12 +1294,33 @@ internal static IntPtr PyUnicode_FromUnicode(string s, int size) [DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "FromEncodedObject")] internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err); +#if NETSTANDARD1_5 + [DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "FromUnicode")] + private static extern IntPtr PyUnicode_FromUnicode( + IntPtr s, + int size + ); + + internal static IntPtr PyUnicode_FromUnicode( + string s, + int size + ) + { + var marshaler = UcsMarshaler.GetInstance(string.Empty); + var unmanagedDataPtr = marshaler.MarshalManagedToNative(s); + + IntPtr result = PyUnicode_FromUnicode(unmanagedDataPtr, size); + + marshaler.CleanUpNativeData(unmanagedDataPtr); + return result; + } +#else [DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "FromUnicode")] internal static extern IntPtr PyUnicode_FromUnicode( [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UcsMarshaler))] string s, int size ); - +#endif [DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "GetSize")] internal static extern int PyUnicode_GetSize(IntPtr ob); @@ -1469,12 +1558,35 @@ internal static bool PyIter_Check(IntPtr pointer) internal static extern IntPtr PyImport_GetModuleDict(); #if PYTHON3 +#if NETSTANDARD1_5 + [DllImport(PythonDll)] + private static extern void PySys_SetArgvEx( + int argc, + IntPtr argv, + int updatepath + ); + + internal static void PySys_SetArgvEx( + int argc, + string[] argv, + int updatepath + ) + { + var marshaler = StrArrayMarshaler.GetInstance(string.Empty); + var unmanagedDataPtr = marshaler.MarshalManagedToNative(argv); + + PySys_SetArgvEx(argc, unmanagedDataPtr, updatepath); + + marshaler.CleanUpNativeData(unmanagedDataPtr); + } +#else [DllImport(PythonDll)] internal static extern void PySys_SetArgvEx( int argc, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrArrayMarshaler))] string[] argv, int updatepath ); +#endif #elif PYTHON2 [DllImport(PythonDll)] internal static extern void PySys_SetArgvEx( From 96f14f0b83bc7b5d954a8a1c09b67a10d4b92d39 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 19:36:27 +0400 Subject: [PATCH 05/14] New .Net 4.5+ reflection API polyfill implemented. --- src/runtime/Python.Runtime.csproj | 9 +++++++-- src/runtime/arrayobject.cs | 6 +++--- src/runtime/classbase.cs | 5 +++-- src/runtime/classderived.cs | 11 ++++++----- src/runtime/classmanager.cs | 20 +++++++++++++++++--- src/runtime/classobject.cs | 9 +++++---- src/runtime/constructorbinder.cs | 7 ++++--- src/runtime/converter.cs | 7 ++++--- src/runtime/delegatemanager.cs | 7 ++++--- src/runtime/genericutil.cs | 3 ++- src/runtime/interop.cs | 6 +++++- src/runtime/metatype.cs | 2 +- src/runtime/moduleobject.cs | 9 +++++---- src/runtime/packages.config | 4 ++++ src/runtime/typemanager.cs | 14 ++++++++------ 15 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 src/runtime/packages.config diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index 8580b7f61..7afbd7c6e 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -1,4 +1,4 @@ - + Debug @@ -71,6 +71,10 @@ full + + ..\..\packages\ReflectionBridge.0.0.11\lib\net40\ReflectionBridge.dll + True + @@ -148,6 +152,7 @@ + @@ -163,4 +168,4 @@ - + \ No newline at end of file diff --git a/src/runtime/arrayobject.cs b/src/runtime/arrayobject.cs index a10688749..0c174135c 100644 --- a/src/runtime/arrayobject.cs +++ b/src/runtime/arrayobject.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; namespace Python.Runtime @@ -140,8 +140,8 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v) int rank = items.Rank; int index; object value; - - if (items.IsReadOnly) + + if (((IList)items).IsReadOnly) { Exceptions.RaiseTypeError("array is read-only"); return -1; diff --git a/src/runtime/classbase.cs b/src/runtime/classbase.cs index 4dd3b5364..edbb32061 100644 --- a/src/runtime/classbase.cs +++ b/src/runtime/classbase.cs @@ -1,6 +1,7 @@ -using System; +using System; using System.Collections; using System.Runtime.InteropServices; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -25,7 +26,7 @@ internal ClassBase(Type tp) internal virtual bool CanSubclass() { - return !type.IsEnum; + return !type.IsEnum(); } /// diff --git a/src/runtime/classderived.cs b/src/runtime/classderived.cs index c180f9acc..b90a52a3e 100644 --- a/src/runtime/classderived.cs +++ b/src/runtime/classderived.cs @@ -1,10 +1,11 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; using System.Threading.Tasks; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -133,7 +134,7 @@ internal static Type CreateDerivedType(string name, // if the base type is an interface then use System.Object as the base class // and add the base type to the list of interfaces this new class will implement. - if (baseType.IsInterface) + if (baseType.IsInterface()) { interfaces.Add(baseType); baseClass = typeof(object); @@ -309,7 +310,7 @@ private static void AddConstructor(ConstructorInfo ctor, Type baseType, TypeBuil il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldarg, i + 1); - if (parameterTypes[i].IsValueType) + if (parameterTypes[i].IsValueType()) { il.Emit(OpCodes.Box, parameterTypes[i]); } @@ -387,7 +388,7 @@ private static void AddVirtualMethod(MethodInfo method, Type baseType, TypeBuild il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldarg, i + 1); - if (parameterTypes[i].IsValueType) + if (parameterTypes[i].IsValueType()) { il.Emit(OpCodes.Box, parameterTypes[i]); } @@ -474,7 +475,7 @@ private static void AddPythonMethod(string methodName, PyObject func, TypeBuilde il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldarg, i + 1); - if (argTypes[i].IsValueType) + if (argTypes[i].IsValueType()) { il.Emit(OpCodes.Box, argTypes[i]); } diff --git a/src/runtime/classmanager.cs b/src/runtime/classmanager.cs index 6a9d40ebd..58e4037dd 100644 --- a/src/runtime/classmanager.cs +++ b/src/runtime/classmanager.cs @@ -1,9 +1,10 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using System.Security; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -73,7 +74,11 @@ private static ClassBase CreateClass(Type type) // lets us check once (vs. on every lookup) in case we need to // wrap Exception-derived types in old-style classes +#if NETSTANDARD1_5 + if (type.GetTypeInfo().ContainsGenericParameters) +#else if (type.ContainsGenericParameters) +#endif { impl = new GenericType(type); } @@ -88,7 +93,7 @@ private static ClassBase CreateClass(Type type) impl = new ArrayObject(type); } - else if (type.IsInterface) + else if (type.IsInterface()) { impl = new InterfaceObject(type); } @@ -236,7 +241,7 @@ private static ClassInfo GetClassInfo(Type type) } } - if (type.IsInterface) + if (type.IsInterface()) { // Interface inheritance seems to be a different animal: // more contractual, less structural. Thus, a Type that @@ -358,12 +363,21 @@ private static ClassInfo GetClassInfo(Type type) continue; case MemberTypes.NestedType: +#if NETSTANDARD1_5 + tp = ((TypeInfo) mi).AsType(); + if (!(tp.IsNestedPublic() || tp.GetTypeInfo().IsNestedFamily || + tp.GetTypeInfo().IsNestedFamORAssem)) + { + continue; + } +#else tp = (Type)mi; if (!(tp.IsNestedPublic || tp.IsNestedFamily || tp.IsNestedFamORAssem)) { continue; } +#endif // Note the given instance might be uninitialized ob = GetClass(tp); ci.members[mi.Name] = ob; diff --git a/src/runtime/classobject.cs b/src/runtime/classobject.cs index 46257c73f..a0d383cde 100644 --- a/src/runtime/classobject.cs +++ b/src/runtime/classobject.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Reflection; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -65,7 +66,7 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) // Primitive types do not have constructors, but they look like // they do from Python. If the ClassObject represents one of the // convertible primitive types, just convert the arg directly. - if (type.IsPrimitive || type == typeof(string)) + if (type.IsPrimitive() || type == typeof(string)) { if (Runtime.PyTuple_Size(args) != 1) { @@ -84,13 +85,13 @@ public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) return CLRObject.GetInstHandle(result, tp); } - if (type.IsAbstract) + if (type.IsAbstract()) { Exceptions.SetError(Exceptions.TypeError, "cannot instantiate abstract class"); return IntPtr.Zero; } - if (type.IsEnum) + if (type.IsEnum()) { Exceptions.SetError(Exceptions.TypeError, "cannot instantiate enumeration"); return IntPtr.Zero; diff --git a/src/runtime/constructorbinder.cs b/src/runtime/constructorbinder.cs index 1fc541920..59b849950 100644 --- a/src/runtime/constructorbinder.cs +++ b/src/runtime/constructorbinder.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Reflection; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -51,8 +52,8 @@ internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info) { object result; - if (_containingType.IsValueType && !_containingType.IsPrimitive && - !_containingType.IsEnum && _containingType != typeof(decimal) && + if (_containingType.IsValueType() && !_containingType.IsPrimitive() && + !_containingType.IsEnum() && _containingType != typeof(decimal) && Runtime.PyTuple_Size(args) == 0) { // If you are trying to construct an instance of a struct by diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index e0566f5d2..33501117f 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Security; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -136,7 +137,7 @@ internal static IntPtr ToPython(object value, Type type) return result; } - if (value is IList && value.GetType().IsGenericType) + if (value is IList && value.GetType().IsGenericType()) { using (var resultlist = new PyList()) { @@ -314,7 +315,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType, return false; } - if (value == Runtime.PyNone && !obType.IsValueType) + if (value == Runtime.PyNone && !obType.IsValueType()) { result = null; return true; @@ -325,7 +326,7 @@ internal static bool ToManagedValue(IntPtr value, Type obType, return ToArray(value, obType, out result, setError); } - if (obType.IsEnum) + if (obType.IsEnum()) { return ToEnum(value, obType, out result, setError); } diff --git a/src/runtime/delegatemanager.cs b/src/runtime/delegatemanager.cs index df5eec427..adcae3949 100644 --- a/src/runtime/delegatemanager.cs +++ b/src/runtime/delegatemanager.cs @@ -1,7 +1,8 @@ -using System; +using System; using System.Collections; using System.Reflection; using System.Reflection.Emit; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -118,7 +119,7 @@ private Type GetDispatcher(Type dtype) il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Ldarg_S, (byte)(c + 1)); - if (t.IsValueType) + if (t.IsValueType()) { il.Emit(OpCodes.Box, t); } @@ -135,7 +136,7 @@ private Type GetDispatcher(Type dtype) { il.Emit(OpCodes.Pop); } - else if (method.ReturnType.IsValueType) + else if (method.ReturnType.IsValueType()) { il.Emit(OpCodes.Unbox_Any, method.ReturnType); } diff --git a/src/runtime/genericutil.cs b/src/runtime/genericutil.cs index 9772d082f..510da0287 100644 --- a/src/runtime/genericutil.cs +++ b/src/runtime/genericutil.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Collections.Generic; +using ReflectionBridge.Extensions; namespace Python.Runtime { diff --git a/src/runtime/interop.cs b/src/runtime/interop.cs index 4ae4b61e0..42eedafe9 100644 --- a/src/runtime/interop.cs +++ b/src/runtime/interop.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Specialized; using System.Runtime.InteropServices; @@ -342,7 +342,11 @@ static Interop() // Here we build a mapping of PyTypeObject slot names to the // appropriate prototype (delegate) type to use for the slot. +#if NETSTANDARD1_5 + Type[] items = typeof(Interop).GetTypeInfo().GetNestedTypes(); +#else Type[] items = typeof(Interop).GetNestedTypes(); +#endif Hashtable p = new Hashtable(); for (int i = 0; i < items.Length; i++) diff --git a/src/runtime/metatype.cs b/src/runtime/metatype.cs index bfb71e26d..4f96127d2 100644 --- a/src/runtime/metatype.cs +++ b/src/runtime/metatype.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Runtime.InteropServices; namespace Python.Runtime diff --git a/src/runtime/moduleobject.cs b/src/runtime/moduleobject.cs index e683026f9..b4995fc45 100644 --- a/src/runtime/moduleobject.cs +++ b/src/runtime/moduleobject.cs @@ -1,8 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.InteropServices; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -108,7 +109,7 @@ public ManagedType GetAttribute(string name, bool guess) type = AssemblyManager.LookupType(qname); if (type != null) { - if (!type.IsPublic) + if (!type.IsPublic()) { return null; } @@ -134,7 +135,7 @@ public ManagedType GetAttribute(string name, bool guess) type = AssemblyManager.LookupType(qname); if (type != null) { - if (!type.IsPublic) + if (!type.IsPublic()) { return null; } @@ -238,7 +239,7 @@ internal void InitializeModuleMembers() StoreAttribute(name, p); } } - type = type.BaseType; + type = type.BaseType(); } } diff --git a/src/runtime/packages.config b/src/runtime/packages.config new file mode 100644 index 000000000..549563b28 --- /dev/null +++ b/src/runtime/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/runtime/typemanager.cs b/src/runtime/typemanager.cs index 6f373f036..c72c5567e 100644 --- a/src/runtime/typemanager.cs +++ b/src/runtime/typemanager.cs @@ -1,8 +1,10 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using System.Reflection; using System.Runtime.InteropServices; +using ReflectionBridge.Extensions; namespace Python.Runtime { @@ -132,9 +134,9 @@ internal static IntPtr CreateType(ManagedType impl, Type clrType) { base_ = Exceptions.Exception; } - else if (clrType.BaseType != null) + else if (clrType.BaseType() != null) { - ClassBase bc = ClassManager.GetClass(clrType.BaseType); + ClassBase bc = ClassManager.GetClass(clrType.BaseType()); base_ = bc.pyHandle; } @@ -485,7 +487,7 @@ internal static void InitializeSlots(IntPtr type, Type impl) seen[name] = 1; } - impl = impl.BaseType; + impl = impl.BaseType(); } } @@ -510,7 +512,7 @@ private static void InitMethods(IntPtr pytype, Type type) { if (!addedMethods.Contains(method.Name)) { - object[] attrs = method.GetCustomAttributes(marker, false); + object[] attrs = method.GetCustomAttributes(marker, false).ToArray(); if (attrs.Length > 0) { string method_name = method.Name; @@ -522,7 +524,7 @@ private static void InitMethods(IntPtr pytype, Type type) } } } - type = type.BaseType; + type = type.BaseType(); } } From c53c3e9186a62e5ae3cdd18c4cd8d110d75479c2 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 21:09:29 +0400 Subject: [PATCH 06/14] Migration issues resolved for Python3+Windows+x64. Compilable but not debugged. --- src/runtime/classderived.cs | 45 ++++++++++++++++++++++++++-- src/runtime/codegenerator.cs | 6 +++- src/runtime/debughelper.cs | 13 +++++--- src/runtime/delegatemanager.cs | 8 +++++ src/runtime/delegateobject.cs | 6 ++-- src/runtime/eventobject.cs | 18 ++++++++++- src/runtime/interfaceobject.cs | 7 ++++- src/runtime/interop.cs | 8 +++++ src/runtime/metatype.cs | 3 ++ src/runtime/methodbinder.cs | 12 ++++++-- src/runtime/methodwrapper.cs | 5 +++- src/runtime/moduleobject.cs | 7 +++-- src/runtime/nativecall.cs | 20 +++++++++++-- src/runtime/polyfill/DelegateShim.cs | 32 ++++++++++++++++++++ src/runtime/pythonengine.cs | 6 +++- src/runtime/typemethod.cs | 6 +++- 16 files changed, 180 insertions(+), 22 deletions(-) create mode 100644 src/runtime/polyfill/DelegateShim.cs diff --git a/src/runtime/classderived.cs b/src/runtime/classderived.cs index b90a52a3e..6e2cd5447 100644 --- a/src/runtime/classderived.cs +++ b/src/runtime/classderived.cs @@ -124,7 +124,11 @@ internal static Type CreateDerivedType(string name, if (null == assemblyName) { +#if NETSTANDARD1_5 + assemblyName = Assembly.GetEntryAssembly().FullName; +#else assemblyName = Assembly.GetExecutingAssembly().FullName; +#endif } ModuleBuilder moduleBuilder = GetModuleBuilder(assemblyName, moduleName); @@ -182,7 +186,11 @@ internal static Type CreateDerivedType(string name, } // override any virtual methods not already overridden by the properties above +#if NETSTANDARD1_5 + MethodInfo[] methods = baseType.GetTypeInfo().GetMethods(); +#else MethodInfo[] methods = baseType.GetMethods(); +#endif var virtualMethods = new HashSet(); foreach (MethodInfo method in methods) { @@ -250,16 +258,25 @@ internal static Type CreateDerivedType(string name, il.Emit(OpCodes.Call, baseClass.GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance)); il.Emit(OpCodes.Ret); +#if NETSTANDARD1_5 + TypeInfo type = typeBuilder.CreateTypeInfo(); + // scan the assembly so the newly added class can be imported + Assembly assembly = type.Assembly; +#else Type type = typeBuilder.CreateType(); - // scan the assembly so the newly added class can be imported Assembly assembly = Assembly.GetAssembly(type); +#endif AssemblyManager.ScanAssembly(assembly); // FIXME: assemblyBuilder not used AssemblyBuilder assemblyBuilder = assemblyBuilders[assemblyName]; +#if NETSTANDARD1_5 + return type.AsType(); +#else return type; +#endif } /// @@ -592,8 +609,13 @@ private static ModuleBuilder GetModuleBuilder(string assemblyName, string module } else { +#if NETSTANDARD1_5 + assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(assemblyName), + AssemblyBuilderAccess.Run); +#else assemblyBuilder = domain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Run); +#endif assemblyBuilders[assemblyName] = assemblyBuilder; } @@ -678,12 +700,18 @@ public static T InvokeMethod(IPythonDerivedType obj, string methodName, strin { throw new NotImplementedException("Python object does not have a '" + methodName + "' method"); } - +#if NETSTANDARD1_5 + return (T)obj.GetType() + .GetTypeInfo() + .GetMethod(origMethodName, BindingFlags.InvokeMethod) + .Invoke(obj, args); +#else return (T)obj.GetType().InvokeMember(origMethodName, BindingFlags.InvokeMethod, null, obj, args); +#endif } public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, string origMethodName, @@ -741,11 +769,18 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s throw new NotImplementedException($"Python object does not have a '{methodName}' method"); } +#if NETSTANDARD1_5 + obj.GetType() + .GetTypeInfo() + .GetMethod(origMethodName, BindingFlags.InvokeMethod) + .Invoke(obj, args); +#else obj.GetType().InvokeMember(origMethodName, BindingFlags.InvokeMethod, null, obj, args); +#endif } public static T InvokeGetProperty(IPythonDerivedType obj, string propertyName) @@ -803,11 +838,17 @@ public static void InvokeSetProperty(IPythonDerivedType obj, string propertyN public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, object[] args) { // call the base constructor +#if NETSTANDARD1_5 + obj.GetType() + .GetMethod(origCtorName, BindingFlags.InvokeMethod) + .Invoke(obj, args); +#else obj.GetType().InvokeMember(origCtorName, BindingFlags.InvokeMethod, null, obj, args); +#endif var disposeList = new List(); CLRObject self = null; diff --git a/src/runtime/codegenerator.cs b/src/runtime/codegenerator.cs index dc466bafb..b0f5784ce 100644 --- a/src/runtime/codegenerator.cs +++ b/src/runtime/codegenerator.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using System.Reflection.Emit; using System.Threading; @@ -21,7 +21,11 @@ internal CodeGenerator() var aname = new AssemblyName { Name = "__CodeGenerator_Assembly" }; var aa = AssemblyBuilderAccess.Run; +#if NETSTANDARD1_5 + aBuilder = AssemblyBuilder.DefineDynamicAssembly(aname, aa); +#else aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa); +#endif mBuilder = aBuilder.DefineDynamicModule("__CodeGenerator_Module"); } diff --git a/src/runtime/debughelper.cs b/src/runtime/debughelper.cs index 2a91a74b4..83bacc378 100644 --- a/src/runtime/debughelper.cs +++ b/src/runtime/debughelper.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; @@ -105,8 +105,13 @@ internal static void DumpInst(IntPtr ob) [Conditional("DEBUG")] internal static void debug(string msg) { +#if NETSTANDARD1_5 + StackTrace st = new StackTrace(new Exception(msg), true); + StackFrame sf = st.GetFrames()[0]; +#else var st = new StackTrace(1, true); StackFrame sf = st.GetFrame(0); +#endif MethodBase mb = sf.GetMethod(); Type mt = mb.DeclaringType; string caller = mt.Name + "." + sf.GetMethod().Name; @@ -116,10 +121,10 @@ internal static void debug(string msg) Console.WriteLine(" {0}", msg); } - /// + /// /// Helper function to inspect/compare managed to native conversions. - /// Especially useful when debugging CustomMarshaler. - /// + /// Especially useful when debugging CustomMarshaler. + /// /// [Conditional("DEBUG")] public static void PrintHexBytes(byte[] bytes) diff --git a/src/runtime/delegatemanager.cs b/src/runtime/delegatemanager.cs index adcae3949..2d3c142fd 100644 --- a/src/runtime/delegatemanager.cs +++ b/src/runtime/delegatemanager.cs @@ -143,7 +143,11 @@ private Type GetDispatcher(Type dtype) il.Emit(OpCodes.Ret); +#if NETSTANDARD1_5 + Type disp = tb.CreateTypeInfo().AsType(); +#else Type disp = tb.CreateType(); +#endif cache[dtype] = disp; return disp; } @@ -158,7 +162,11 @@ internal Delegate GetDelegate(Type dtype, IntPtr callable) Type dispatcher = GetDispatcher(dtype); object[] args = { callable, dtype }; object o = Activator.CreateInstance(dispatcher, args); +#if NETSTANDARD1_5 + return DelegateShim.CreateDelegate(dtype, o, "Invoke"); +#else return Delegate.CreateDelegate(dtype, o, "Invoke"); +#endif } } diff --git a/src/runtime/delegateobject.cs b/src/runtime/delegateobject.cs index e1103cbc7..608aed25c 100644 --- a/src/runtime/delegateobject.cs +++ b/src/runtime/delegateobject.cs @@ -1,5 +1,7 @@ -using System; - +using System; +#if NETSTANDARD1_5 +using System.Reflection; +#endif namespace Python.Runtime { /// diff --git a/src/runtime/eventobject.cs b/src/runtime/eventobject.cs index 5f18c4609..033cc82b3 100644 --- a/src/runtime/eventobject.cs +++ b/src/runtime/eventobject.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Reflection; @@ -47,7 +47,11 @@ internal bool AddEventHandler(IntPtr target, IntPtr handler) { reg = new Hashtable(); } +#if NETSTANDARD1_5 + object key = obj ?? info.DeclaringType; +#else object key = obj ?? info.ReflectedType; +#endif var list = reg[key] as ArrayList; if (list == null) { @@ -60,7 +64,11 @@ internal bool AddEventHandler(IntPtr target, IntPtr handler) // so we have to get the underlying add method explicitly. object[] args = { d }; MethodInfo mi = info.GetAddMethod(true); +#if NETSTANDARD1_5 + mi.Invoke(obj, args); +#else mi.Invoke(obj, BindingFlags.Default, null, args, null); +#endif return true; } @@ -85,7 +93,11 @@ internal bool RemoveEventHandler(IntPtr target, IntPtr handler) return false; } +#if NETSTANDARD1_5 + object key = obj ?? info.DeclaringType; +#else object key = obj ?? info.ReflectedType; +#endif var list = reg[key] as ArrayList; if (list == null) @@ -107,7 +119,11 @@ internal bool RemoveEventHandler(IntPtr target, IntPtr handler) args[0] = item.del; try { +#if NETSTANDARD1_5 + mi.Invoke(obj, args); +#else mi.Invoke(obj, BindingFlags.Default, null, args, null); +#endif } catch { diff --git a/src/runtime/interfaceobject.cs b/src/runtime/interfaceobject.cs index ce1bc9eb0..374ce0cc2 100644 --- a/src/runtime/interfaceobject.cs +++ b/src/runtime/interfaceobject.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using System.Runtime.InteropServices; @@ -16,7 +16,12 @@ internal class InterfaceObject : ClassBase internal InterfaceObject(Type tp) : base(tp) { +#if NETSTANDARD1_5 + CoClassAttribute coclass = (CoClassAttribute) + tp.GetTypeInfo().GetCustomAttribute(cc_attr); +#else var coclass = (CoClassAttribute)Attribute.GetCustomAttribute(tp, cc_attr); +#endif if (coclass != null) { ctor = coclass.CoClass.GetConstructor(Type.EmptyTypes); diff --git a/src/runtime/interop.cs b/src/runtime/interop.cs index 42eedafe9..afc9d018a 100644 --- a/src/runtime/interop.cs +++ b/src/runtime/interop.cs @@ -457,14 +457,22 @@ internal static IntPtr GetThunk(MethodInfo method, string funcType = null) { Type dt; if (funcType != null) +#if NETSTANDARD1_5 + dt = typeof(Interop).GetTypeInfo().GetNestedType(funcType) as Type; +#else dt = typeof(Interop).GetNestedType(funcType) as Type; +#endif else dt = GetPrototype(method.Name); if (dt != null) { IntPtr tmp = Marshal.AllocHGlobal(IntPtr.Size); +#if NETSTANDARD1_5 + Delegate d = DelegateShim.CreateDelegate(dt, method); +#else Delegate d = Delegate.CreateDelegate(dt, method); +#endif Thunk cb = new Thunk(d); Marshal.StructureToPtr(cb, tmp, false); IntPtr fp = Marshal.ReadIntPtr(tmp, 0); diff --git a/src/runtime/metatype.cs b/src/runtime/metatype.cs index 4f96127d2..f7187a095 100644 --- a/src/runtime/metatype.cs +++ b/src/runtime/metatype.cs @@ -1,4 +1,7 @@ using System; +#if NETSTANDARD1_5 +using System.Reflection; +#endif using System.Runtime.InteropServices; namespace Python.Runtime diff --git a/src/runtime/methodbinder.cs b/src/runtime/methodbinder.cs index f0c58f34f..fd6fec98b 100644 --- a/src/runtime/methodbinder.cs +++ b/src/runtime/methodbinder.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Reflection; @@ -327,7 +327,11 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth } } else if (pynargs > clrnargs && clrnargs > 0 && - Attribute.IsDefined(pi[clrnargs - 1], typeof(ParamArrayAttribute))) +#if NETSTANDARD1_5 + pi[clrnargs - 1].IsDefined(typeof(ParamArrayAttribute))) +#else + Attribute.IsDefined(pi[clrnargs - 1], typeof(ParamArrayAttribute))) +#endif { // This is a `foo(params object[] bar)` style method match = true; @@ -518,7 +522,11 @@ internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase i try { +#if NETSTANDARD1_5 + result = binding.info.Invoke(binding.inst, binding.args); +#else result = binding.info.Invoke(binding.inst, BindingFlags.Default, null, binding.args, null); +#endif } catch (Exception e) { diff --git a/src/runtime/methodwrapper.cs b/src/runtime/methodwrapper.cs index 2f3ce3ef2..e41c25cd1 100644 --- a/src/runtime/methodwrapper.cs +++ b/src/runtime/methodwrapper.cs @@ -1,4 +1,7 @@ -using System; +using System; +#if NETSTANDARD1_5 +using System.Reflection; +#endif namespace Python.Runtime { diff --git a/src/runtime/moduleobject.cs b/src/runtime/moduleobject.cs index b4995fc45..9aeed54a6 100644 --- a/src/runtime/moduleobject.cs +++ b/src/runtime/moduleobject.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using ReflectionBridge.Extensions; @@ -215,8 +216,8 @@ internal void InitializeModuleMembers() MethodInfo[] methods = type.GetMethods(flags); foreach (MethodInfo method in methods) { - object[] attrs = method.GetCustomAttributes(funcmarker, false); - object[] forbid = method.GetCustomAttributes(ftmarker, false); + object[] attrs = method.GetCustomAttributes(funcmarker, false).ToArray(); + object[] forbid = method.GetCustomAttributes(ftmarker, false).ToArray(); bool allow_threads = forbid.Length == 0; if (attrs.Length > 0) { @@ -231,7 +232,7 @@ internal void InitializeModuleMembers() PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { - object[] attrs = property.GetCustomAttributes(propmarker, false); + object[] attrs = property.GetCustomAttributes(propmarker, false).ToArray(); if (attrs.Length > 0) { string name = property.Name; diff --git a/src/runtime/nativecall.cs b/src/runtime/nativecall.cs index 9d1b0f41c..9cd428179 100644 --- a/src/runtime/nativecall.cs +++ b/src/runtime/nativecall.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; @@ -39,8 +39,11 @@ static NativeCall() var aname = new AssemblyName { Name = "e__NativeCall_Assembly" }; var aa = AssemblyBuilderAccess.Run; - +#if NETSTANDARD1_5 + aBuilder = AssemblyBuilder.DefineDynamicAssembly(aname, aa); +#else aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa); +#endif mBuilder = aBuilder.DefineDynamicModule("e__NativeCall_Module"); var ta = TypeAttributes.Public; @@ -57,7 +60,11 @@ static NativeCall() GenerateThunk(tBuilder, method); } +#if NETSTANDARD1_5 + Type theType = tBuilder.CreateTypeInfo().AsType(); +#else Type theType = tBuilder.CreateType(); +#endif Impl = (INativeCall)Activator.CreateInstance(theType); } @@ -106,12 +113,19 @@ private static void GenerateThunk(TypeBuilder tb, MethodInfo method) il.Emit(OpCodes.Ldarg_1); +#if NETSTANDARD1_5 + il.EmitCalli(OpCodes.Calli, + CallingConventions.ExplicitThis, + method.ReturnType, + nargs, null + ); +#else il.EmitCalli(OpCodes.Calli, CallingConvention.Cdecl, method.ReturnType, nargs ); - +#endif il.Emit(OpCodes.Ret); tb.DefineMethodOverride(mb, method); diff --git a/src/runtime/polyfill/DelegateShim.cs b/src/runtime/polyfill/DelegateShim.cs new file mode 100644 index 000000000..0fcbcbd52 --- /dev/null +++ b/src/runtime/polyfill/DelegateShim.cs @@ -0,0 +1,32 @@ +#if NETSTANDARD1_5 +namespace Python.Runtime +{ + using System; + using System.Linq; + using System.Reflection; + + public static class DelegateShim + { + public static Delegate CreateDelegate(Type dtype, object obj, string methodName) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + var methods = obj.GetType().GetTypeInfo().GetMember(methodName, BindingFlags.Default); + if (!methods.Any()) + { + throw new InvalidOperationException("Method does not exist"); + } + + return CreateDelegate(dtype, (MethodInfo)methods[0]); + } + + internal static Delegate CreateDelegate(Type dtype, MethodInfo method) + { + return method.CreateDelegate(dtype); + } + } +} +#endif \ No newline at end of file diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index 1fd3b239a..788fedab3 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -193,7 +193,11 @@ public static void Initialize(IEnumerable args, bool setSysArgv = true) IntPtr builtins = Runtime.PyEval_GetBuiltins(); Runtime.PyDict_SetItemString(module_globals, "__builtins__", builtins); +#if NETSTANDARD1_5 + Assembly assembly = typeof(Runtime).GetTypeInfo().Assembly; +#else Assembly assembly = Assembly.GetExecutingAssembly(); +#endif using (Stream stream = assembly.GetManifestResourceStream("clr.py")) using (var reader = new StreamReader(stream)) { diff --git a/src/runtime/typemethod.cs b/src/runtime/typemethod.cs index 4da92613c..757945d7f 100644 --- a/src/runtime/typemethod.cs +++ b/src/runtime/typemethod.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; namespace Python.Runtime @@ -33,7 +33,11 @@ public override IntPtr Invoke(IntPtr ob, IntPtr args, IntPtr kw) { inst = GetManagedObject(ob); } +#if NETSTANDARD1_5 + return (IntPtr)mi.Invoke(inst, arglist); +#else return (IntPtr)mi.Invoke(inst, BindingFlags.Default, null, arglist, null); +#endif } catch (Exception e) { From 487530236e7ce65c5de0153590e9482e3b969468 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 21:33:11 +0400 Subject: [PATCH 07/14] CHANGELOG edited. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b446cc4b6..0932bfd87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning][]. This document follows the conventions laid out in [Keep a CHANGELOG][]. ## [unreleased][] +- Added VS 2017 build with net40 and NetStandard 1.5 targets ### Added - Added clr.GetClrType (#432)(#433) From e4ad62008036153b9db4ee9c093f617087bbf118 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 28 Mar 2017 22:10:45 +0400 Subject: [PATCH 08/14] ReflectionBridge extension sources injected. Build fix. --- src/runtime/Python.Runtime.15.csproj | 7 +- src/runtime/Python.Runtime.csproj | 9 +- src/runtime/packages.config | 4 - .../polyfill/ReflectionBridge/LICENSE.txt | 22 ++ .../ReflectionBridgeExtensions.cs | 231 ++++++++++++++++++ 5 files changed, 258 insertions(+), 15 deletions(-) delete mode 100644 src/runtime/packages.config create mode 100644 src/runtime/polyfill/ReflectionBridge/LICENSE.txt create mode 100644 src/runtime/polyfill/ReflectionBridge/ReflectionBridgeExtensions.cs diff --git a/src/runtime/Python.Runtime.15.csproj b/src/runtime/Python.Runtime.15.csproj index e1d485046..c9a7926a7 100644 --- a/src/runtime/Python.Runtime.15.csproj +++ b/src/runtime/Python.Runtime.15.csproj @@ -34,7 +34,7 @@ - $(DefineConstants);PYTHON3; PYTHON35 + $(DefineConstants);REFLECTIONBRIDGE;PYTHON3;PYTHON35 @@ -69,9 +69,4 @@ 4.0.0 - - - - - \ No newline at end of file diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index 7afbd7c6e..8d6e14fa2 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -71,13 +71,10 @@ full - - ..\..\packages\ReflectionBridge.0.0.11\lib\net40\ReflectionBridge.dll - True - + Properties\SharedAssemblyInfo.cs @@ -152,13 +149,15 @@ - clr.py + + + $(TargetPath) diff --git a/src/runtime/packages.config b/src/runtime/packages.config deleted file mode 100644 index 549563b28..000000000 --- a/src/runtime/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/runtime/polyfill/ReflectionBridge/LICENSE.txt b/src/runtime/polyfill/ReflectionBridge/LICENSE.txt new file mode 100644 index 000000000..9bb94287b --- /dev/null +++ b/src/runtime/polyfill/ReflectionBridge/LICENSE.txt @@ -0,0 +1,22 @@ + +MIT License + +Copyright (c) 2016 to 2099 Stef Heyenrath + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/src/runtime/polyfill/ReflectionBridge/ReflectionBridgeExtensions.cs b/src/runtime/polyfill/ReflectionBridge/ReflectionBridgeExtensions.cs new file mode 100644 index 000000000..c421408ac --- /dev/null +++ b/src/runtime/polyfill/ReflectionBridge/ReflectionBridgeExtensions.cs @@ -0,0 +1,231 @@ +using System; +using System.Reflection; + +#if REFLECTIONBRIDGE +using System.Collections.Generic; +using System.Linq; +#endif + +namespace ReflectionBridge.Extensions +{ + public static class ReflectionBridgeExtensions + { + public static Assembly GetAssembly(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().Assembly; +#else + return type.Assembly; +#endif + } + + public static bool IsSealed(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsSealed; +#else + return type.IsSealed; +#endif + } + + public static bool IsAbstract(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsAbstract; +#else + return type.IsAbstract; +#endif + } + + public static bool IsEnum(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsEnum; +#else + return type.IsEnum; +#endif + } + + public static bool IsClass(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsClass; +#else + return type.IsClass; +#endif + } + + public static bool IsPrimitive(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsPrimitive; +#else + return type.IsPrimitive; +#endif + } + + public static bool IsPublic(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsPublic; +#else + return type.IsPublic; +#endif + } + + public static bool IsNestedPublic(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsNestedPublic; +#else + return type.IsNestedPublic; +#endif + } + + public static bool IsFromLocalAssembly(this Type type) + { +#if SILVERLIGHT + string assemblyName = type.GetAssembly().FullName; +#else + string assemblyName = type.GetAssembly().GetName().Name; +#endif + + try + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + Assembly.Load(new AssemblyName { Name = assemblyName }); +#else + Assembly.Load(assemblyName); +#endif + return true; + } + catch + { + return false; + } + } + + public static bool IsGenericType(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsGenericType; +#else + return type.IsGenericType; +#endif + } + + public static bool IsInterface(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsInterface; +#else + return type.IsInterface; +#endif + } + + public static Type BaseType(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().BaseType; +#else + return type.BaseType; +#endif + } + + public static bool IsValueType(this Type type) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + return type.GetTypeInfo().IsValueType; +#else + return type.IsValueType; +#endif + } + + public static T GetPropertyValue(this Type type, string propertyName, object target) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + PropertyInfo property = type.GetTypeInfo().GetDeclaredProperty(propertyName); + return (T)property.GetValue(target); +#else + return (T)type.InvokeMember(propertyName, BindingFlags.GetProperty, null, target, null); +#endif + } + + public static void SetPropertyValue(this Type type, string propertyName, object target, object value) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + PropertyInfo property = type.GetTypeInfo().GetDeclaredProperty(propertyName); + property.SetValue(target, value); +#else + type.InvokeMember(propertyName, BindingFlags.SetProperty, null, target, new object[] { value }); +#endif + } + + public static void SetFieldValue(this Type type, string fieldName, object target, object value) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + FieldInfo field = type.GetTypeInfo().GetDeclaredField(fieldName); + if (field != null) + { + field.SetValue(target, value); + } + else + { + type.SetPropertyValue(fieldName, target, value); + } +#else + type.InvokeMember(fieldName, BindingFlags.SetField | BindingFlags.SetProperty, null, target, new object[] { value }); +#endif + } + + public static void InvokeMethod(this Type type, string methodName, object target, T value) + { +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + MethodInfo method = type.GetTypeInfo().GetDeclaredMethod(methodName); + method.Invoke(target, new object[] { value }); +#else + type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, target, new object[] { value }); +#endif + } + +#if REFLECTIONBRIDGE && (!(NET40 || NET35 || NET20 || SILVERLIGHT)) + public static IEnumerable GetMethods(this Type someType) + { + var t = someType; + while (t != null) + { + var ti = t.GetTypeInfo(); + foreach (var m in ti.DeclaredMethods) + yield return m; + t = ti.BaseType; + } + } + + public static Type[] GetGenericArguments(this Type type) + { + return type.GetTypeInfo().GenericTypeArguments; + } + + /* + public static bool IsAssignableFrom(this Type type, Type otherType) + { + return type.GetTypeInfo().IsAssignableFrom(otherType.GetTypeInfo()); + }*/ + + public static bool IsSubclassOf(this Type type, Type c) + { + return type.GetTypeInfo().IsSubclassOf(c); + } + + public static Attribute[] GetCustomAttributes(this Type type) + { + return type.GetTypeInfo().GetCustomAttributes().ToArray(); + } + + public static Attribute[] GetCustomAttributes(this Type type, Type attributeType, bool inherit) + { + return type.GetTypeInfo().GetCustomAttributes(attributeType, inherit).Cast().ToArray(); + } +#endif + } +} \ No newline at end of file From 26966d371b0768d3fd43e359e4c6fba1b3d697d8 Mon Sep 17 00:00:00 2001 From: dse Date: Wed, 29 Mar 2017 01:08:20 +0400 Subject: [PATCH 09/14] Added embed_tests project to VS 2017 build. All tests are passed for net40 build. --- pythonnet.15.sln | 22 ++++++- .../Python.EmbeddingTest.15.csproj | 61 +++++++++++++++++++ src/embed_tests/TestRuntime.cs | 2 +- src/embed_tests/pyimport.cs | 6 +- src/runtime/Python.Runtime.15.csproj | 10 ++- src/runtime/pythonengine.cs | 4 ++ 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/embed_tests/Python.EmbeddingTest.15.csproj diff --git a/pythonnet.15.sln b/pythonnet.15.sln index 2384f8e5a..1bd0396fa 100644 --- a/pythonnet.15.sln +++ b/pythonnet.15.sln @@ -1,26 +1,46 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.26228.10 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.Runtime.15", "src\runtime\Python.Runtime.15.csproj", "{4D2089E1-5B6A-4799-984B-178AE467902D}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.EmbeddingTest.15", "src\embed_tests\Python.EmbeddingTest.15.csproj", "{4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 Debug-Linux-x64|x64 = Debug-Linux-x64|x64 Debug-Win-x64|x64 = Debug-Win-x64|x64 + Release|x64 = Release|x64 Release-Linux-x64|x64 = Release-Linux-x64|x64 Release-Win-x64|x64 = Release-Win-x64|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug|x64.ActiveCfg = Debug|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug|x64.Build.0 = Debug|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.ActiveCfg = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.Build.0 = Debug|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.ActiveCfg = Debug-Win-x64|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.Build.0 = Debug-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release|x64.ActiveCfg = Release|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release|x64.Build.0 = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.ActiveCfg = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.Build.0 = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.ActiveCfg = Release|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.Build.0 = Release|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug|x64.ActiveCfg = Debug|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug|x64.Build.0 = Debug|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Linux-x64|x64.ActiveCfg = Debug|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Linux-x64|x64.Build.0 = Debug|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Win-x64|x64.ActiveCfg = Debug|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Win-x64|x64.Build.0 = Debug|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release|x64.ActiveCfg = Release|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release|x64.Build.0 = Release|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Linux-x64|x64.ActiveCfg = Release|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Linux-x64|x64.Build.0 = Release|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Win-x64|x64.ActiveCfg = Release|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Win-x64|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/embed_tests/Python.EmbeddingTest.15.csproj b/src/embed_tests/Python.EmbeddingTest.15.csproj new file mode 100644 index 000000000..0ed1fbc33 --- /dev/null +++ b/src/embed_tests/Python.EmbeddingTest.15.csproj @@ -0,0 +1,61 @@ + + + net40;netstandard1.6 + + Python.EmbeddingTest + Python.EmbeddingTest + Python.EmbeddingTest + 3.0.0 + Alpha + false + false + True + + 1.6.0 + Full + + + x86 + + + x64 + + + + + + + + + + + + $(DefineConstants);XPLAT + + + + + 3.6.1 + + + + + + + + + + + + 4.0.1 + + + + + + + + PreserveNewest + + + \ No newline at end of file diff --git a/src/embed_tests/TestRuntime.cs b/src/embed_tests/TestRuntime.cs index 22e6db0a9..b3a024148 100644 --- a/src/embed_tests/TestRuntime.cs +++ b/src/embed_tests/TestRuntime.cs @@ -1,4 +1,4 @@ -using System; +using System; using NUnit.Framework; using Python.Runtime; diff --git a/src/embed_tests/pyimport.cs b/src/embed_tests/pyimport.cs index 3bb9a34d6..dbf71da24 100644 --- a/src/embed_tests/pyimport.cs +++ b/src/embed_tests/pyimport.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using NUnit.Framework; using Python.Runtime; @@ -30,7 +30,11 @@ public void SetUp() /* Append the tests directory to sys.path * using reflection to circumvent the private * modifiers placed on most Runtime methods. */ +#if XPLAT + const string s = "fixtures"; +#else const string s = "../fixtures"; +#endif string testPath = Path.Combine(TestContext.CurrentContext.TestDirectory, s); IntPtr str = Runtime.Runtime.PyString_FromString(testPath); diff --git a/src/runtime/Python.Runtime.15.csproj b/src/runtime/Python.Runtime.15.csproj index c9a7926a7..9e3a03140 100644 --- a/src/runtime/Python.Runtime.15.csproj +++ b/src/runtime/Python.Runtime.15.csproj @@ -34,7 +34,7 @@ - $(DefineConstants);REFLECTIONBRIDGE;PYTHON3;PYTHON35 + $(DefineConstants);XPLAT;REFLECTIONBRIDGE;PYTHON3;PYTHON35 @@ -69,4 +69,12 @@ 4.0.0 + + + + + + + + \ No newline at end of file diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index 788fedab3..4116679fa 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -198,7 +198,11 @@ public static void Initialize(IEnumerable args, bool setSysArgv = true) #else Assembly assembly = Assembly.GetExecutingAssembly(); #endif +#if XPLAT + using (Stream stream = assembly.GetManifestResourceStream("Python.Runtime.resources.clr.py")) +#else using (Stream stream = assembly.GetManifestResourceStream("clr.py")) +#endif using (var reader = new StreamReader(stream)) { // add the contents of clr.py to the module From 36c2e85a9eeaba6e187bac6898ea66819ce4ea19 Mon Sep 17 00:00:00 2001 From: dse Date: Wed, 29 Mar 2017 04:07:48 +0400 Subject: [PATCH 10/14] DotNetCore + Windows x64 + Python 3.5 = all tests passed. --- src/embed_tests/Program.cs | 21 +++++++++++++++++++ .../Properties/launchSettings.json | 7 +++++++ .../Python.EmbeddingTest.15.csproj | 11 ++++++---- src/runtime/importhook.cs | 5 ++++- 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/embed_tests/Program.cs create mode 100644 src/embed_tests/Properties/launchSettings.json diff --git a/src/embed_tests/Program.cs b/src/embed_tests/Program.cs new file mode 100644 index 000000000..349bf4522 --- /dev/null +++ b/src/embed_tests/Program.cs @@ -0,0 +1,21 @@ +using System; +using System.Reflection; +using NUnit.Common; +using NUnitLite; + +namespace Python.EmbeddingTest +{ + public class Program + { + public static int Main(string[] args) + { + ////var example = new TestExample(); + ////example.SetUp(); + ////example.TestReadme(); + ////example.Dispose(); + ////return 0; + return new AutoRun(typeof(Program).GetTypeInfo().Assembly) + .Execute(args, new ExtendedTextWrapper(Console.Out), Console.In); + } + } +} diff --git a/src/embed_tests/Properties/launchSettings.json b/src/embed_tests/Properties/launchSettings.json new file mode 100644 index 000000000..a8841cea2 --- /dev/null +++ b/src/embed_tests/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "Python.EmbeddingTest.15": { + "commandName": "Project" + } + } +} \ No newline at end of file diff --git a/src/embed_tests/Python.EmbeddingTest.15.csproj b/src/embed_tests/Python.EmbeddingTest.15.csproj index 0ed1fbc33..e8a13713e 100644 --- a/src/embed_tests/Python.EmbeddingTest.15.csproj +++ b/src/embed_tests/Python.EmbeddingTest.15.csproj @@ -1,7 +1,8 @@  - net40;netstandard1.6 - + exe + netcoreapp1.0 + win7-x64;ubuntu.16.04-x64 Python.EmbeddingTest Python.EmbeddingTest Python.EmbeddingTest @@ -12,6 +13,7 @@ True 1.6.0 + Full @@ -25,9 +27,9 @@ - + - + $(DefineConstants);XPLAT @@ -37,6 +39,7 @@ 3.6.1 + diff --git a/src/runtime/importhook.cs b/src/runtime/importhook.cs index bc9ac5eee..291b0ad4c 100644 --- a/src/runtime/importhook.cs +++ b/src/runtime/importhook.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Runtime.InteropServices; namespace Python.Runtime @@ -75,7 +75,10 @@ internal static void Shutdown() if (Runtime.Py_IsInitialized() != 0) { Runtime.XDecref(py_clr_module); + // TODO: Very strange behavior under CoreCLR. System.ExecutionEngineException (Crash) +#if !NETSTANDARD1_5 Runtime.XDecref(root.pyHandle); +#endif Runtime.XDecref(py_import); } } From 563a844e0e8dfbfc18d87cb63f480900bd506bf5 Mon Sep 17 00:00:00 2001 From: dse Date: Thu, 30 Mar 2017 01:12:50 +0400 Subject: [PATCH 11/14] DotNetCore + Ubuntu 14.04 x64 + Python 3.5 = all tests passed. --- pythonnet.15.sln | 34 +++++++----------- .../PublishProfiles/FolderProfile.pubxml | 14 ++++++++ .../Python.EmbeddingTest.15.csproj | 2 +- src/runtime/Python.Runtime.15.csproj | 35 +++++++++++++----- src/runtime/runtime.cs | 36 +++++++++++++++---- 5 files changed, 83 insertions(+), 38 deletions(-) create mode 100644 src/embed_tests/Properties/PublishProfiles/FolderProfile.pubxml diff --git a/pythonnet.15.sln b/pythonnet.15.sln index 1bd0396fa..0358a736b 100644 --- a/pythonnet.15.sln +++ b/pythonnet.15.sln @@ -9,38 +9,28 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Python.EmbeddingTest.15", " EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 Debug-Linux-x64|x64 = Debug-Linux-x64|x64 Debug-Win-x64|x64 = Debug-Win-x64|x64 - Release|x64 = Release|x64 Release-Linux-x64|x64 = Release-Linux-x64|x64 Release-Win-x64|x64 = Release-Win-x64|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug|x64.ActiveCfg = Debug|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug|x64.Build.0 = Debug|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.ActiveCfg = Release|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.Build.0 = Debug|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.ActiveCfg = Debug-Linux-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Linux-x64|x64.Build.0 = Debug-Linux-x64|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.ActiveCfg = Debug-Win-x64|x64 {4D2089E1-5B6A-4799-984B-178AE467902D}.Debug-Win-x64|x64.Build.0 = Debug-Win-x64|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Release|x64.ActiveCfg = Release|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Release|x64.Build.0 = Release|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.ActiveCfg = Release|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.Build.0 = Release|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.ActiveCfg = Release|x64 - {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.Build.0 = Release|x64 - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug|x64.ActiveCfg = Debug|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug|x64.Build.0 = Debug|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Linux-x64|x64.ActiveCfg = Debug|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Linux-x64|x64.Build.0 = Debug|Any CPU + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.ActiveCfg = Release-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Linux-x64|x64.Build.0 = Release-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.ActiveCfg = Release-Win-x64|x64 + {4D2089E1-5B6A-4799-984B-178AE467902D}.Release-Win-x64|x64.Build.0 = Release-Win-x64|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Linux-x64|x64.ActiveCfg = Debug|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Linux-x64|x64.Build.0 = Debug|x64 {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Win-x64|x64.ActiveCfg = Debug|x64 {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Debug-Win-x64|x64.Build.0 = Debug|x64 - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release|x64.ActiveCfg = Release|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release|x64.Build.0 = Release|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Linux-x64|x64.ActiveCfg = Release|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Linux-x64|x64.Build.0 = Release|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Win-x64|x64.ActiveCfg = Release|Any CPU - {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Win-x64|x64.Build.0 = Release|Any CPU + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Linux-x64|x64.ActiveCfg = Release|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Linux-x64|x64.Build.0 = Release|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Win-x64|x64.ActiveCfg = Release|x64 + {4CACAA81-CC6A-4381-85B8-3A4B57EFE3BD}.Release-Win-x64|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/embed_tests/Properties/PublishProfiles/FolderProfile.pubxml b/src/embed_tests/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 000000000..6bdb8c414 --- /dev/null +++ b/src/embed_tests/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,14 @@ + + + + + FileSystem + Debug-Linux-x64 + netcoreapp1.0 + bin\publish\Debug-Linux-x64 + ubuntu.14.04-x64 + + \ No newline at end of file diff --git a/src/embed_tests/Python.EmbeddingTest.15.csproj b/src/embed_tests/Python.EmbeddingTest.15.csproj index e8a13713e..8d4321025 100644 --- a/src/embed_tests/Python.EmbeddingTest.15.csproj +++ b/src/embed_tests/Python.EmbeddingTest.15.csproj @@ -2,7 +2,7 @@ exe netcoreapp1.0 - win7-x64;ubuntu.16.04-x64 + win7-x64;ubuntu.16.04-x64;ubuntu.14.04-x64 Python.EmbeddingTest Python.EmbeddingTest Python.EmbeddingTest diff --git a/src/runtime/Python.Runtime.15.csproj b/src/runtime/Python.Runtime.15.csproj index 9e3a03140..f58a1360e 100644 --- a/src/runtime/Python.Runtime.15.csproj +++ b/src/runtime/Python.Runtime.15.csproj @@ -12,30 +12,49 @@ True 1.6.0 + + + x64 + TRACE;DEBUG;X64;LINUX;UNIX;UCS4;PYTHON_WITH_PYMALLOC;MONO_LINUX - + x64 - TRACE;DEBUG;X64;LINUX;UNIX;NET40;UCS2; + TRACE;DEBUG;X64;LINUX;UNIX;UCS4;PYTHON_WITH_PYMALLOC;MONO_LINUX - - + x64 - TRACE;X64;LINUX;UNIX;NET40;UCS2; + TRACE;DEBUG;X64;LINUX;UNIX;UCS4;PYTHON_WITH_PYMALLOC;MONO_LINUX + + + x64 + TRACE;DEBUG;X64;LINUX;UNIX;UCS4;PYTHON_WITH_PYMALLOC;MONO_LINUX + + x64 + TRACE;DEBUG;X64;UCS2 + x64 - TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_5;UCS2; + TRACE;DEBUG;X64;UCS2 + + + x64 + TRACE;X64;UCS2 - x64 - TRACE;DEBUG;X64;LINUX;UNIX;NETSTANDARD1_5;UCS2; + TRACE;DEBUG;X64;UCS2; $(DefineConstants);XPLAT;REFLECTIONBRIDGE;PYTHON3;PYTHON35 + + + + bin\Debug + diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 0a2bc4317..8cf01f06c 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -24,7 +24,11 @@ internal static class NativeMethods public static IntPtr LoadLibrary(string fileName) { +#if NETSTANDARD1_5 + return dlopen(fileName, RTLD_NOW); +#else return dlopen(fileName, RTLD_NOW | RTLD_SHARED); +#endif } public static void FreeLibrary(IntPtr handle) @@ -281,9 +285,13 @@ internal static void Initialize() IntPtr dllLocal = IntPtr.Zero; if (PythonDll != "__Internal") { +#if !NETSTANDARD1_5 NativeMethods.LoadLibrary(PythonDll); +#endif } +#if !NETSTANDARD1_5 _PyObject_NextNotImplemented = NativeMethods.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); +#endif #if !(MONO_LINUX || MONO_OSX) if (dllLocal != IntPtr.Zero) { @@ -648,8 +656,10 @@ string[] argv var unmanagedDataPtr = marshaler.MarshalManagedToNative(argv); int result = Py_Main(argc, unmanagedDataPtr); - - marshaler.CleanUpNativeData(unmanagedDataPtr); + if (unmanagedDataPtr != IntPtr.Zero) + { + marshaler.CleanUpNativeData(unmanagedDataPtr); + } return result; } #else @@ -1207,7 +1217,10 @@ int size IntPtr result = PyString_FromStringAndSize(unmanagedDataPtr, size); - marshaler.CleanUpNativeData(unmanagedDataPtr); + if (unmanagedDataPtr != IntPtr.Zero) + { + marshaler.CleanUpNativeData(unmanagedDataPtr); + } return result; } #else @@ -1262,7 +1275,11 @@ int size IntPtr result = PyUnicode_FromKindAndData(kind, unmanagedDataPtr, size); - marshaler.CleanUpNativeData(unmanagedDataPtr); + if (unmanagedDataPtr != IntPtr.Zero) + { + marshaler.CleanUpNativeData(unmanagedDataPtr); + } + return result; } #else @@ -1310,8 +1327,11 @@ int size var unmanagedDataPtr = marshaler.MarshalManagedToNative(s); IntPtr result = PyUnicode_FromUnicode(unmanagedDataPtr, size); + if (unmanagedDataPtr != IntPtr.Zero) + { + marshaler.CleanUpNativeData(unmanagedDataPtr); + } - marshaler.CleanUpNativeData(unmanagedDataPtr); return result; } #else @@ -1576,8 +1596,10 @@ int updatepath var unmanagedDataPtr = marshaler.MarshalManagedToNative(argv); PySys_SetArgvEx(argc, unmanagedDataPtr, updatepath); - - marshaler.CleanUpNativeData(unmanagedDataPtr); + if (unmanagedDataPtr != IntPtr.Zero) + { + marshaler.CleanUpNativeData(unmanagedDataPtr); + } } #else [DllImport(PythonDll)] From 94295127e8a880b606c70b52b4d6e3b5346ce44b Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 25 Apr 2017 21:38:38 +0400 Subject: [PATCH 12/14] Binary substitution of Python.Runtime.Dll becomes safe. Public constants replaced to static readonly fields. Relaxed platform binding. --- src/embed_tests/TestPythonEngineProperties.cs | 4 +- src/runtime/CustomMarshaler.cs | 6 +- src/runtime/Python.Runtime.csproj | 6 +- src/runtime/runtime.cs | 495 +++++++++--------- 4 files changed, 266 insertions(+), 245 deletions(-) diff --git a/src/embed_tests/TestPythonEngineProperties.cs b/src/embed_tests/TestPythonEngineProperties.cs index 01c6ae7e3..76177d05a 100644 --- a/src/embed_tests/TestPythonEngineProperties.cs +++ b/src/embed_tests/TestPythonEngineProperties.cs @@ -146,7 +146,7 @@ public void SetProgramName() [Test] public void SetPythonPath() { - if (Runtime.Runtime.pyversion == "2.7") + if (Runtime.Runtime._pyversion == "2.7") { // Assert.Skip outputs as a warning (ie. pending to fix) Assert.Pass(); @@ -166,7 +166,7 @@ public void SetPythonPath() [Test] public void SetPythonPathExceptionOn27() { - if (Runtime.Runtime.pyversion != "2.7") + if (Runtime.Runtime._pyversion != "2.7") { Assert.Pass(); } diff --git a/src/runtime/CustomMarshaler.cs b/src/runtime/CustomMarshaler.cs index 90bb77a71..b51911816 100644 --- a/src/runtime/CustomMarshaler.cs +++ b/src/runtime/CustomMarshaler.cs @@ -91,13 +91,13 @@ public static int GetUnicodeByteLength(IntPtr p) var len = 0; while (true) { - int c = Runtime.UCS == 2 + int c = Runtime._UCS == 2 ? Marshal.ReadInt16(p, len * 2) : Marshal.ReadInt32(p, len * 4); if (c == 0) { - return len * Runtime.UCS; + return len * Runtime._UCS; } checked { @@ -163,7 +163,7 @@ public override IntPtr MarshalManagedToNative(object managedObj) } int totalStrLength = argv.Sum(arg => arg.Length + 1); - int memSize = argv.Length * IntPtr.Size + totalStrLength * Runtime.UCS; + int memSize = argv.Length * IntPtr.Size + totalStrLength * Runtime._UCS; IntPtr mem = Marshal.AllocHGlobal(memSize); try diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index 8580b7f61..c020fcf50 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -20,12 +20,14 @@ false ..\pythonnet.snk - + + + PYTHON2;PYTHON27;UCS4 true diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 9ee693cf3..dd97cccb7 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -80,8 +80,14 @@ public static IntPtr GetProcAddress(IntPtr dllHandle, string name) /// public class Runtime { + // 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 int UCS = _UCS; + #if UCS4 - public const int UCS = 4; + internal const int _UCS = 4; /// /// EntryPoint to be used in DllImport to map to correct Unicode @@ -89,7 +95,7 @@ public class Runtime /// private const string PyUnicodeEntryPoint = "PyUnicodeUCS4_"; #elif UCS2 - public const int UCS = 2; + public const int _UCS = 2; /// /// EntryPoint to be used in DllImport to map to correct Unicode @@ -100,24 +106,31 @@ public class Runtime #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 const string pyversion = _pyversion; + public const string pyver = _pyver; + #if PYTHON27 - public const string pyversion = "2.7"; - public const string pyver = "27"; + internal const string _pyversion = "2.7"; + internal const string _pyver = "27"; #elif PYTHON33 - public const string pyversion = "3.3"; - public const string pyver = "33"; + public const string _pyversion = "3.3"; + public const string _pyver = "33"; #elif PYTHON34 - public const string pyversion = "3.4"; - public const string pyver = "34"; + public const string _pyversion = "3.4"; + public const string _pyver = "34"; #elif PYTHON35 - public const string pyversion = "3.5"; + public const string _pyversion = "3.5"; public const string pyver = "35"; #elif PYTHON36 - public const string pyversion = "3.6"; - public const string pyver = "36"; + public const string _pyversion = "3.6"; + public const string _pyver = "36"; #elif PYTHON37 // TODO: Add `interop37.cs` after PY37 is released - public const string pyversion = "3.7"; - public const string pyver = "37"; + public const string _pyversion = "3.7"; + public const string _pyver = "37"; #else #error You must define one of PYTHON33 to PYTHON37 or PYTHON27 #endif @@ -125,7 +138,7 @@ public class Runtime #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; + internal const string dllBase = "python" + _pyver; #endif #if PYTHON_WITH_PYDEBUG @@ -139,13 +152,19 @@ public class Runtime 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 - public const string PythonDll = "__Internal"; + public const string _PythonDll = "__Internal"; #else - public const string PythonDll = dllBase + dllWithPyDebug + dllWithPyMalloc; + public const string _PythonDll = dllBase + dllWithPyDebug + dllWithPyMalloc; #endif - public static readonly int pyversionnumber = Convert.ToInt32(pyver); + public static readonly int pyversionnumber = Convert.ToInt32(_pyver); // set to true when python is finalizing internal static object IsFinalizingLock = new object(); @@ -158,7 +177,7 @@ public class Runtime /// /// Encoding to use to convert Unicode to/from Managed to Native /// - internal static readonly Encoding PyEncoding = UCS == 2 ? Encoding.Unicode : Encoding.UTF32; + internal static readonly Encoding PyEncoding = _UCS == 2 ? Encoding.Unicode : Encoding.UTF32; /// /// Initialize the runtime... @@ -570,7 +589,7 @@ internal static unsafe long Refcount(IntPtr op) /// Limit this function usage for Testing and Py_Debug builds /// /// PyObject Ptr - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_IncRef(IntPtr ob); /// @@ -578,50 +597,50 @@ internal static unsafe long Refcount(IntPtr op) /// Limit this function usage for Testing and Py_Debug builds /// /// PyObject Ptr - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_DecRef(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_Initialize(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int Py_IsInitialized(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_Finalize(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_NewInterpreter(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_EndInterpreter(IntPtr threadState); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyThreadState_New(IntPtr istate); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyThreadState_Get(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyThread_get_key_value(IntPtr key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyThread_get_thread_ident(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyThread_set_key_value(IntPtr key, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyThreadState_Swap(IntPtr key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyGILState_Ensure(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyGILState_Release(IntPtr gs); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyGILState_GetThisThreadState(); #if PYTHON3 @@ -631,104 +650,104 @@ public static extern int Py_Main( [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrArrayMarshaler))] string[] argv ); #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] public static extern int Py_Main(int argc, string[] argv); #endif - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyEval_InitThreads(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyEval_ThreadsInitialized(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyEval_AcquireLock(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyEval_ReleaseLock(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyEval_AcquireThread(IntPtr tstate); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyEval_ReleaseThread(IntPtr tstate); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyEval_SaveThread(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyEval_RestoreThread(IntPtr tstate); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyEval_GetBuiltins(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyEval_GetGlobals(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyEval_GetLocals(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetProgramName(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_SetProgramName(IntPtr name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetPythonHome(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_SetPythonHome(IntPtr home); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetPath(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void Py_SetPath(IntPtr home); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetVersion(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetPlatform(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetCopyright(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetCompiler(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_GetBuildInfo(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyRun_SimpleString(string code); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyRun_String(string code, IntPtr st, IntPtr globals, IntPtr locals); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr Py_CompileString(string code, string file, IntPtr tok); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyImport_ExecCodeModule(string name, IntPtr code); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyCFunction_NewEx(IntPtr ml, IntPtr self, IntPtr mod); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyCFunction_Call(IntPtr func, IntPtr args, IntPtr kw); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyClass_New(IntPtr bases, IntPtr dict, IntPtr name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyInstance_New(IntPtr cls, IntPtr args, IntPtr kw); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyInstance_NewRaw(IntPtr cls, IntPtr dict); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyMethod_New(IntPtr func, IntPtr self, IntPtr cls); @@ -777,40 +796,40 @@ internal static string PyObject_GetTypeName(IntPtr op) return Marshal.PtrToStringAnsi(ppName); } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_HasAttrString(IntPtr pointer, string name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_GetAttrString(IntPtr pointer, string name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_SetAttrString(IntPtr pointer, string name, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_HasAttr(IntPtr pointer, IntPtr name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_GetAttr(IntPtr pointer, IntPtr name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_SetAttr(IntPtr pointer, IntPtr name, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_GetItem(IntPtr pointer, IntPtr key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_SetItem(IntPtr pointer, IntPtr key, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_DelItem(IntPtr pointer, IntPtr key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_GetIter(IntPtr op); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Call(IntPtr pointer, IntPtr args, IntPtr kw); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_CallObject(IntPtr pointer, IntPtr args); #if PYTHON3 @@ -842,35 +861,35 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) return -1; } #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_Compare(IntPtr value1, IntPtr value2); #endif - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_IsInstance(IntPtr ob, IntPtr type); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_IsSubclass(IntPtr ob, IntPtr type); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyCallable_Check(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_IsTrue(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_Not(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_Size(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Hash(IntPtr op); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Repr(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Str(IntPtr pointer); #if PYTHON3 @@ -878,11 +897,11 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) EntryPoint = "PyObject_Str")] internal static extern IntPtr PyObject_Unicode(IntPtr pointer); #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Unicode(IntPtr pointer); #endif - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_Dir(IntPtr pointer); @@ -895,17 +914,17 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) EntryPoint = "PyNumber_Long")] internal static extern IntPtr PyNumber_Int(IntPtr ob); #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Int(IntPtr ob); #endif - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Long(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Float(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern bool PyNumber_Check(IntPtr ob); internal static bool PyInt_Check(IntPtr ob) @@ -947,16 +966,16 @@ internal static IntPtr PyInt_FromInt64(long value) EntryPoint = "PyLong_GetMax")] internal static extern int PyInt_GetMax(); #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr PyInt_FromLong(IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyInt_AsLong(IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyInt_FromString(string value, IntPtr end, int radix); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyInt_GetMax(); #endif @@ -965,34 +984,34 @@ internal static bool PyLong_Check(IntPtr ob) return PyObject_TYPE(ob) == PyLongType; } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyLong_FromLong(long value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyLong_FromUnsignedLong(uint value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyLong_FromDouble(double value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyLong_FromLongLong(long value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyLong_FromUnsignedLongLong(ulong value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyLong_FromString(string value, IntPtr end, int radix); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyLong_AsLong(IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern uint PyLong_AsUnsignedLong(IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern long PyLong_AsLongLong(IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern ulong PyLong_AsUnsignedLongLong(IntPtr value); internal static bool PyFloat_Check(IntPtr ob) @@ -1000,88 +1019,88 @@ internal static bool PyFloat_Check(IntPtr ob) return PyObject_TYPE(ob) == PyFloatType; } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyFloat_FromDouble(double value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyFloat_FromString(IntPtr value, IntPtr junk); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern double PyFloat_AsDouble(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Add(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Subtract(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Multiply(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Divide(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_And(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Xor(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Or(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Lshift(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Rshift(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Power(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Remainder(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceAdd(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceSubtract(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceMultiply(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceDivide(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceAnd(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceXor(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceOr(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceLshift(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceRshift(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlacePower(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_InPlaceRemainder(IntPtr o1, IntPtr o2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Negative(IntPtr o1); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Positive(IntPtr o1); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyNumber_Invert(IntPtr o1); @@ -1089,49 +1108,49 @@ internal static bool PyFloat_Check(IntPtr ob) // Python sequence API //==================================================================== - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern bool PySequence_Check(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PySequence_GetItem(IntPtr pointer, int index); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_SetItem(IntPtr pointer, int index, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_DelItem(IntPtr pointer, int index); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PySequence_GetSlice(IntPtr pointer, int i1, int i2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_SetSlice(IntPtr pointer, int i1, int i2, IntPtr v); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_DelSlice(IntPtr pointer, int i1, int i2); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_Size(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_Contains(IntPtr pointer, IntPtr item); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PySequence_Concat(IntPtr pointer, IntPtr other); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PySequence_Repeat(IntPtr pointer, int count); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_Index(IntPtr pointer, IntPtr item); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySequence_Count(IntPtr pointer, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PySequence_Tuple(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PySequence_List(IntPtr pointer); @@ -1177,13 +1196,13 @@ int size [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size); #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyString_FromStringAndSize(string value, int size); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyString_AsString(IntPtr op); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyString_Size(IntPtr pointer); #endif @@ -1220,30 +1239,30 @@ internal static IntPtr PyUnicode_FromUnicode(string s, int size) [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromOrdinal(int c); #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = PyUnicodeEntryPoint + "FromObject")] internal static extern IntPtr PyUnicode_FromObject(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = PyUnicodeEntryPoint + "FromEncodedObject")] internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = PyUnicodeEntryPoint + "FromUnicode")] internal static extern IntPtr PyUnicode_FromUnicode( [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UcsMarshaler))] string s, int size ); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = PyUnicodeEntryPoint + "GetSize")] internal static extern int PyUnicode_GetSize(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = PyUnicodeEntryPoint + "AsUnicode")] internal static extern IntPtr PyUnicode_AsUnicode(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = PyUnicodeEntryPoint + "FromOrdinal")] internal static extern IntPtr PyUnicode_FromOrdinal(int c); #endif @@ -1282,7 +1301,7 @@ internal static string GetManagedString(IntPtr op) IntPtr p = PyUnicode_AsUnicode(op); int length = PyUnicode_GetSize(op); - int size = length * UCS; + int size = length * _UCS; var buffer = new byte[size]; Marshal.Copy(p, buffer, 0, size); return PyEncoding.GetString(buffer, 0, size); @@ -1301,52 +1320,52 @@ internal static bool PyDict_Check(IntPtr ob) return PyObject_TYPE(ob) == PyDictType; } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDict_New(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDictProxy_New(IntPtr dict); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDict_GetItem(IntPtr pointer, IntPtr key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDict_GetItemString(IntPtr pointer, string key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyDict_SetItem(IntPtr pointer, IntPtr key, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyDict_SetItemString(IntPtr pointer, string key, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyDict_DelItem(IntPtr pointer, IntPtr key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyDict_DelItemString(IntPtr pointer, string key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyMapping_HasKey(IntPtr pointer, IntPtr key); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDict_Keys(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDict_Values(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDict_Items(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyDict_Copy(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyDict_Update(IntPtr pointer, IntPtr other); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyDict_Clear(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyDict_Size(IntPtr pointer); @@ -1359,37 +1378,37 @@ internal static bool PyList_Check(IntPtr ob) return PyObject_TYPE(ob) == PyListType; } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyList_New(int size); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyList_AsTuple(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyList_GetItem(IntPtr pointer, int index); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyList_SetItem(IntPtr pointer, int index, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyList_Insert(IntPtr pointer, int index, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyList_Append(IntPtr pointer, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyList_Reverse(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyList_Sort(IntPtr pointer); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyList_GetSlice(IntPtr pointer, int start, int end); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyList_SetSlice(IntPtr pointer, int start, int end, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyList_Size(IntPtr pointer); @@ -1402,19 +1421,19 @@ internal static bool PyTuple_Check(IntPtr ob) return PyObject_TYPE(ob) == PyTupleType; } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyTuple_New(int size); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyTuple_GetItem(IntPtr pointer, int index); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyTuple_SetItem(IntPtr pointer, int index, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyTuple_GetSlice(IntPtr pointer, int start, int end); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyTuple_Size(IntPtr pointer); @@ -1423,7 +1442,7 @@ internal static bool PyTuple_Check(IntPtr ob) //==================================================================== #if PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern bool PyIter_Check(IntPtr pointer); #elif PYTHON3 internal static bool PyIter_Check(IntPtr pointer) @@ -1434,7 +1453,7 @@ internal static bool PyIter_Check(IntPtr pointer) } #endif - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyIter_Next(IntPtr pointer); @@ -1442,16 +1461,16 @@ internal static bool PyIter_Check(IntPtr pointer) // Python module API //==================================================================== - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyModule_New(string name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern string PyModule_GetName(IntPtr module); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyModule_GetDict(IntPtr module); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern string PyModule_GetFilename(IntPtr module); #if PYTHON3 @@ -1459,19 +1478,19 @@ internal static bool PyIter_Check(IntPtr pointer) internal static extern IntPtr PyModule_Create2(IntPtr module, int apiver); #endif - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyImport_Import(IntPtr name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyImport_ImportModule(string name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyImport_ReloadModule(IntPtr module); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyImport_AddModule(string name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyImport_GetModuleDict(); #if PYTHON3 @@ -1482,7 +1501,7 @@ internal static extern void PySys_SetArgvEx( int updatepath ); #elif PYTHON2 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PySys_SetArgvEx( int argc, string[] argv, @@ -1490,10 +1509,10 @@ int updatepath ); #endif - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PySys_GetObject(string name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PySys_SetObject(string name, IntPtr ob); @@ -1506,10 +1525,10 @@ internal static bool PyType_Check(IntPtr ob) return PyObject_TypeCheck(ob, PyTypeType); } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyType_Modified(IntPtr type); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern bool PyType_IsSubtype(IntPtr t1, IntPtr t2); internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp) @@ -1518,37 +1537,37 @@ internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp) return (t == tp) || PyType_IsSubtype(t, tp); } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyType_GenericNew(IntPtr type, IntPtr args, IntPtr kw); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyType_GenericAlloc(IntPtr type, int n); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyType_Ready(IntPtr type); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr _PyType_Lookup(IntPtr type, IntPtr name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_GenericGetAttr(IntPtr obj, IntPtr name); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_GenericSetAttr(IntPtr obj, IntPtr name, IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr _PyObject_GetDictPtr(IntPtr obj); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyObject_GC_New(IntPtr tp); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyObject_GC_Del(IntPtr tp); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyObject_GC_Track(IntPtr tp); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyObject_GC_UnTrack(IntPtr tp); @@ -1556,13 +1575,13 @@ internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp) // Python memory API //==================================================================== - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyMem_Malloc(int size); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyMem_Realloc(IntPtr ptr, int size); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyMem_Free(IntPtr ptr); @@ -1570,40 +1589,40 @@ internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp) // Python exception API //==================================================================== - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_SetString(IntPtr ob, string message); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_SetObject(IntPtr ob, IntPtr message); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyErr_SetFromErrno(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_SetNone(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyErr_ExceptionMatches(IntPtr exception); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyErr_GivenExceptionMatches(IntPtr ob, IntPtr val); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_NormalizeException(IntPtr ob, IntPtr val, IntPtr tb); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyErr_Occurred(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_Fetch(ref IntPtr ob, ref IntPtr val, ref IntPtr tb); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_Restore(IntPtr ob, IntPtr val, IntPtr tb); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_Clear(); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PyErr_Print(); @@ -1611,10 +1630,10 @@ internal static bool PyObject_TypeCheck(IntPtr ob, IntPtr tp) // Miscellaneous //==================================================================== - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyMethod_Self(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyMethod_Function(IntPtr ob); } } From 49edf48b1bced0e82a8964a928fe11cf0869d3cd Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 25 Apr 2017 22:02:09 +0400 Subject: [PATCH 13/14] Build fix, lost renames performed. --- src/runtime/runtime.cs | 44 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index dd97cccb7..f1ca1524b 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -295,9 +295,9 @@ internal static void Initialize() #if PYTHON3 IntPtr dllLocal = IntPtr.Zero; - if (PythonDll != "__Internal") + if (_PythonDll != "__Internal") { - NativeMethods.LoadLibrary(PythonDll); + NativeMethods.LoadLibrary(_PythonDll); } _PyObject_NextNotImplemented = NativeMethods.GetProcAddress(dllLocal, "_PyObject_NextNotImplemented"); #if !(MONO_LINUX || MONO_OSX) @@ -644,7 +644,7 @@ internal static unsafe long Refcount(IntPtr op) internal static extern IntPtr PyGILState_GetThisThreadState(); #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] public static extern int Py_Main( int argc, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrArrayMarshaler))] string[] argv @@ -833,7 +833,7 @@ internal static string PyObject_GetTypeName(IntPtr op) internal static extern IntPtr PyObject_CallObject(IntPtr pointer, IntPtr args); #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyObject_RichCompareBool(IntPtr value1, IntPtr value2, int opid); internal static int PyObject_Compare(IntPtr value1, IntPtr value2) @@ -893,7 +893,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) internal static extern IntPtr PyObject_Str(IntPtr pointer); #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyObject_Str")] internal static extern IntPtr PyObject_Unicode(IntPtr pointer); #elif PYTHON2 @@ -910,7 +910,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2) //==================================================================== #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyNumber_Long")] internal static extern IntPtr PyNumber_Int(IntPtr ob); #elif PYTHON2 @@ -950,19 +950,19 @@ internal static IntPtr PyInt_FromInt64(long value) } #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyLong_FromLong")] private static extern IntPtr PyInt_FromLong(IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyLong_AsLong")] internal static extern int PyInt_AsLong(IntPtr value); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyLong_FromString")] internal static extern IntPtr PyInt_FromString(string value, IntPtr end, int radix); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyLong_GetMax")] internal static extern int PyInt_GetMax(); #elif PYTHON2 @@ -1175,10 +1175,10 @@ internal static IntPtr PyString_FromString(string value) } #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyBytes_FromString(string op); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyBytes_Size(IntPtr op); internal static IntPtr PyBytes_AS_STRING(IntPtr ob) @@ -1186,14 +1186,14 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob) return ob + BytesOffset.ob_sval; } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl, + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "PyUnicode_FromStringAndSize")] internal static extern IntPtr PyString_FromStringAndSize( [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value, int size ); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, int size); #elif PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] @@ -1212,13 +1212,13 @@ internal static bool PyUnicode_Check(IntPtr ob) } #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromObject(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromKindAndData( int kind, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(UcsMarshaler))] string s, @@ -1230,13 +1230,13 @@ internal static IntPtr PyUnicode_FromUnicode(string s, int size) return PyUnicode_FromKindAndData(UCS, s, size); } - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern int PyUnicode_GetSize(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_AsUnicode(IntPtr ob); - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyUnicode_FromOrdinal(int c); #elif PYTHON2 [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl, @@ -1474,7 +1474,7 @@ internal static bool PyIter_Check(IntPtr pointer) internal static extern string PyModule_GetFilename(IntPtr module); #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr PyModule_Create2(IntPtr module, int apiver); #endif @@ -1494,7 +1494,7 @@ internal static bool PyIter_Check(IntPtr pointer) internal static extern IntPtr PyImport_GetModuleDict(); #if PYTHON3 - [DllImport(PythonDll, CallingConvention = CallingConvention.Cdecl)] + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] internal static extern void PySys_SetArgvEx( int argc, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrArrayMarshaler))] string[] argv, From 938030c28e69a96f6ca93e4c040b172d525c6a71 Mon Sep 17 00:00:00 2001 From: dse Date: Tue, 25 Apr 2017 22:20:27 +0400 Subject: [PATCH 14/14] typo fix. --- src/runtime/runtime.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index f1ca1524b..18b9df7fe 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -124,7 +124,7 @@ public class Runtime public const string _pyver = "34"; #elif PYTHON35 public const string _pyversion = "3.5"; - public const string pyver = "35"; + public const string _pyver = "35"; #elif PYTHON36 public const string _pyversion = "3.6"; public const string _pyver = "36";