Skip to content

Commit 1ea3773

Browse files
committed
Finally removed compilation constants from Python.Runtime.dll + some debug.
1 parent 014dde5 commit 1ea3773

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

src/Python.Runtime.Interop/PythonRuntimeInterop.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,10 +1231,28 @@ internal unsafe static extern IntPtr
12311231
EntryPoint = "PyUnicodeUCS4_FromUnicode",
12321232
ExactSpelling = true)]
12331233
internal unsafe static extern IntPtr
1234-
PyUnicode_FromUnicode(
1235-
[MarshalAs(UnmanagedType.CustomMarshaler,
1236-
MarshalTypeRef = typeof(Utf32Marshaler))] string s, int size);
1234+
PyUnicode_FromUnicode(IntPtr pstr, int size);
12371235

1236+
internal unsafe static IntPtr PyUnicode_FromUnicode(string s, int size)
1237+
{
1238+
var bufLength = Math.Max(s.Length, size) * 4;
1239+
1240+
IntPtr mem = Marshal.AllocHGlobal(bufLength);
1241+
try
1242+
{
1243+
fixed (char* ps = s)
1244+
{
1245+
Encoding.UTF32.GetBytes(ps, s.Length, (byte*)mem, bufLength);
1246+
}
1247+
1248+
var result = PyUnicode_FromUnicode(mem, bufLength);
1249+
return result;
1250+
}
1251+
finally
1252+
{
1253+
Marshal.FreeHGlobal(mem);
1254+
}
1255+
}
12381256
[DllImport(dll, CallingConvention = CallingConvention.Cdecl,
12391257
EntryPoint = "PyUnicodeUCS4_GetSize",
12401258
ExactSpelling = true, CharSet = CharSet.Ansi)]
@@ -1268,7 +1286,7 @@ internal static IntPtr PyUnicode_FromString(string s)
12681286

12691287
internal unsafe static string GetManagedString(IntPtr op)
12701288
{
1271-
IntPtr type = PyObject_TYPE(op);
1289+
IntPtr type = Runtime.PyObject_TYPE(op);
12721290

12731291
// Python 3 strings are all unicode
12741292
#if !(PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
@@ -1283,8 +1301,8 @@ internal unsafe static string GetManagedString(IntPtr op)
12831301

12841302
if (type == Runtime.PyUnicodeType)
12851303
{
1286-
IntPtr p = Runtime.PyUnicode_AsUnicode(op);
1287-
int length = Runtime.PyUnicode_GetSize(op);
1304+
IntPtr p = PyUnicode_AsUnicode(op);
1305+
int length = PyUnicode_GetSize(op);
12881306
int size = length*4;
12891307
byte[] buffer = new byte[size];
12901308
Marshal.Copy(p, buffer, 0, size);
@@ -2517,7 +2535,11 @@ IntPtr IPythonRuntimeInterop.PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc,
25172535

25182536
IntPtr IPythonRuntimeInterop.PyUnicode_FromKindAndString(int kind, string s, int size)
25192537
{
2538+
#if (UCS2) && (PYTHON33 || PYTHON34 || PYTHON35)
25202539
return PyUnicode_FromKindAndString(kind, s, size);
2540+
#else
2541+
throw new NotSupportedException();
2542+
#endif
25212543
}
25222544

25232545
IntPtr IPythonRuntimeInterop.PyUnicode_FromUnicode(string s, int size)
@@ -2530,11 +2552,6 @@ int IPythonRuntimeInterop.PyUnicode_GetSize(IntPtr ob)
25302552
return PyUnicode_GetSize(ob);
25312553
}
25322554

2533-
unsafe char* IPythonRuntimeInterop.PyUnicode_AsUnicode(IntPtr ob)
2534-
{
2535-
return PyUnicode_AsUnicode(ob);
2536-
}
2537-
25382555
IntPtr IPythonRuntimeInterop.PyUnicode_AS_UNICODE(IntPtr op)
25392556
{
25402557
return PyUnicode_AS_UNICODE(op);
@@ -2756,7 +2773,11 @@ string IPythonRuntimeInterop.PyModule_GetFilename(IntPtr module)
27562773

27572774
IntPtr IPythonRuntimeInterop.PyModule_Create2(IntPtr module, int apiver)
27582775
{
2776+
#if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35)
27592777
return PyModule_Create2(module, apiver);
2778+
#else
2779+
throw new NotSupportedException();
2780+
#endif
27602781
}
27612782

27622783
IntPtr IPythonRuntimeInterop.PyImport_Import(IntPtr name)

src/Python.Runtime/InteropContracts/IPythonRuntimeInterop.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,6 @@ public interface IPythonRuntimeInterop
318318

319319
IntPtr PyUnicode_FromObject(IntPtr ob);
320320

321-
unsafe char* PyUnicode_AsUnicode(IntPtr ob);
322-
323321
IntPtr PyUnicode_AS_UNICODE(IntPtr op);
324322

325323
IntPtr PyUnicode_FromOrdinal(int c);

src/Python.Runtime/project.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
"version": "2.2.0-*",
33

44
"buildOptions": {
5-
"define": [ "TRACE", "DEBUG", "UCS2", "PYTHON35"],
6-
"compile": {
7-
"exclude": [
8-
"interop33.cs"
9-
]
10-
},
115
"allowUnsafe": true,
126
"embed": {
137
"include": [ "**/*.resx", "resources/**/*" ]

src/Python.Runtime/runtime.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,6 @@ internal static IntPtr PyUnicode_FromKindAndString(int kind, string s, int size)
920920

921921
internal static int PyUnicode_GetSize(IntPtr ob) => _interop.PyUnicode_GetSize(ob);
922922

923-
internal static unsafe char* PyUnicode_AsUnicode(IntPtr ob) => _interop.PyUnicode_AsUnicode(ob);
924-
925923
internal static IntPtr PyUnicode_AS_UNICODE(IntPtr op) => _interop.PyUnicode_AS_UNICODE(op);
926924

927925
internal static IntPtr PyUnicode_FromOrdinal(int c) => _interop.PyUnicode_FromOrdinal(c);

0 commit comments

Comments
 (0)