@@ -1231,10 +1231,28 @@ internal unsafe static extern IntPtr
1231
1231
EntryPoint = "PyUnicodeUCS4_FromUnicode" ,
1232
1232
ExactSpelling = true ) ]
1233
1233
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 ) ;
1237
1235
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
+ }
1238
1256
[ DllImport ( dll , CallingConvention = CallingConvention . Cdecl ,
1239
1257
EntryPoint = "PyUnicodeUCS4_GetSize" ,
1240
1258
ExactSpelling = true , CharSet = CharSet . Ansi ) ]
@@ -1268,7 +1286,7 @@ internal static IntPtr PyUnicode_FromString(string s)
1268
1286
1269
1287
internal unsafe static string GetManagedString ( IntPtr op )
1270
1288
{
1271
- IntPtr type = PyObject_TYPE ( op ) ;
1289
+ IntPtr type = Runtime . PyObject_TYPE ( op ) ;
1272
1290
1273
1291
// Python 3 strings are all unicode
1274
1292
#if ! ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 )
@@ -1283,8 +1301,8 @@ internal unsafe static string GetManagedString(IntPtr op)
1283
1301
1284
1302
if ( type == Runtime . PyUnicodeType )
1285
1303
{
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 ) ;
1288
1306
int size = length * 4 ;
1289
1307
byte [ ] buffer = new byte [ size ] ;
1290
1308
Marshal . Copy ( p , buffer , 0 , size ) ;
@@ -2517,7 +2535,11 @@ IntPtr IPythonRuntimeInterop.PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc,
2517
2535
2518
2536
IntPtr IPythonRuntimeInterop . PyUnicode_FromKindAndString ( int kind , string s , int size )
2519
2537
{
2538
+ #if ( UCS2 ) && ( PYTHON33 || PYTHON34 || PYTHON35 )
2520
2539
return PyUnicode_FromKindAndString ( kind , s , size ) ;
2540
+ #else
2541
+ throw new NotSupportedException ( ) ;
2542
+ #endif
2521
2543
}
2522
2544
2523
2545
IntPtr IPythonRuntimeInterop . PyUnicode_FromUnicode ( string s , int size )
@@ -2530,11 +2552,6 @@ int IPythonRuntimeInterop.PyUnicode_GetSize(IntPtr ob)
2530
2552
return PyUnicode_GetSize ( ob ) ;
2531
2553
}
2532
2554
2533
- unsafe char * IPythonRuntimeInterop . PyUnicode_AsUnicode ( IntPtr ob )
2534
- {
2535
- return PyUnicode_AsUnicode ( ob ) ;
2536
- }
2537
-
2538
2555
IntPtr IPythonRuntimeInterop . PyUnicode_AS_UNICODE ( IntPtr op )
2539
2556
{
2540
2557
return PyUnicode_AS_UNICODE ( op ) ;
@@ -2756,7 +2773,11 @@ string IPythonRuntimeInterop.PyModule_GetFilename(IntPtr module)
2756
2773
2757
2774
IntPtr IPythonRuntimeInterop . PyModule_Create2 ( IntPtr module , int apiver )
2758
2775
{
2776
+ #if ( PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35 )
2759
2777
return PyModule_Create2 ( module , apiver ) ;
2778
+ #else
2779
+ throw new NotSupportedException ( ) ;
2780
+ #endif
2760
2781
}
2761
2782
2762
2783
IntPtr IPythonRuntimeInterop . PyImport_Import ( IntPtr name )
0 commit comments