10000 Merge branch 'unify-unicode' · pythonnet/pythonnet@423407a · GitHub
[go: up one dir, main page]

Skip to content

Commit 423407a

Browse files
committed
Merge branch 'unify-unicode'
2 parents c7b4c21 + 0fdb80a commit 423407a

File tree

3 files changed

+29
-58
lines changed

3 files changed

+29
-58
lines changed

src/runtime/converter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
498498
{
499499
if (Runtime.PyString_Size(value) == 1)
500500
{
501-
op = Runtime.PyString_AS_STRING(value);
501+
op = Runtime.PyString_AsString(value);
502502
result = (byte)Marshal.ReadByte(op);
503503
return true;
504504
}
@@ -543,7 +543,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
543543
{
544544
if (Runtime.PyString_Size(value) == 1)
545545
{
546-
op = Runtime.PyString_AS_STRING(value);
546+
op = Runtime.PyString_AsString(value);
547547
result = (sbyte)Marshal.ReadByte(op);
548548
return true;
549549
}
@@ -588,7 +588,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
588588
{
589589
if (Runtime.PyString_Size(value) == 1)
590590
{
591-
op = Runtime.PyString_AS_STRING(value);
591+
op = Runtime.PyString_AsString(value);
592592
result = (char)Marshal.ReadByte(op);
593593
return true;
594594
}

src/runtime/runtime.cs

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,20 @@ public class Runtime
8282
{
8383
#if UCS4
8484
public const int UCS = 4;
85+
86+
/// <summary>
87+
/// EntryPoint to be used in DllImport to map to correct Unicode
88+
/// methods prior to PEP393. Only used for PY27.
89+
/// </summary>
90+
private const string PyUnicodeEntryPoint = "PyUnicodeUCS4_";
8591
#elif UCS2
8692
public const int UCS = 2;
93+
94+
/// <summary>
95+
/// EntryPoint to be used in DllImport to map to correct Unicode
96+
/// methods prior to PEP393. Only used for PY27.
97+
/// </summary>
98+
private const string PyUnicodeEntryPoint = "PyUnicodeUCS2_";
8799
#else
88100
#error You must define either UCS2 or UCS4!
89101
#endif
@@ -1231,8 +1243,8 @@ int size
12311243
[DllImport(PythonDll)]
12321244
internal static extern IntPtr PyString_FromStringAndSize(string value, int size);
12331245

1234-
[DllImport(PythonDll, EntryPoint = "PyString_AsString")]
1235-
internal static extern IntPtr PyString_AS_STRING(IntPtr op);
1246+
[DllImport(PythonDll)]
1247+
internal static extern IntPtr PyString_AsString(IntPtr op);
12361248

12371249
[DllImport(PythonDll)]
12381250
internal static extern int PyString_Size(IntPtr pointer);
@@ -1243,64 +1255,23 @@ internal static bool PyUnicode_Check(IntPtr ob)
12431255
return PyObject_TYPE(ob) == PyUnicodeType;
12441256
}
12451257

1246-
#if UCS2 && PYTHON3
1258+
#if PYTHON3
12471259
[DllImport(PythonDll)]
12481260
internal static extern IntPtr PyUnicode_FromObject(IntPtr ob);
12491261

12501262
[DllImport(PythonDll)]
12511263
internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err);
12521264

1253-
[DllImport(PythonDll, EntryPoint = "PyUnicode_FromKindAndData", CharSet = CharSet.Unicode)]
1254-
internal static extern IntPtr PyUnicode_FromKindAndString(int kind, string s, int size);
1255-
1256-
internal static IntPtr PyUnicode_FromUnicode(string s, int size)
1257-
{
1258-
return PyUnicode_FromKindAndString(2, s, size);
1259-
}
1260-
1261-
[DllImport(PythonDll)]
1262-
internal static extern int PyUnicode_GetSize(IntPtr ob);
1263-
1264-
[DllImport(PythonDll)]
1265-
internal static extern IntPtr PyUnicode_AsUnicode(IntPtr ob);
1266< A3D4 span class="diff-text-marker">-
12671265
[DllImport(PythonDll)]
1268-
internal static extern IntPtr PyUnicode_FromOrdinal(int c);
1269-
#elif UCS2 && PYTHON2
1270-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS2_FromObject")]
1271-
internal static extern IntPtr PyUnicode_FromObject(IntPtr ob);
1272-
1273-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS2_FromEncodedObject")]
1274-
internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err);
1275-
1276-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS2_FromUnicode", CharSet = CharSet.Unicode)]
1277-
internal static extern IntPtr PyUnicode_FromUnicode(string s, int size);
1278-
1279-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS2_GetSize")]
1280-
internal static extern int PyUnicode_GetSize(IntPtr ob);
1281-
1282-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS2_AsUnicode")]
1283-
internal static extern IntPtr PyUnicode_AsUnicode(IntPtr ob);
1284-
1285-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS2_FromOrdinal")]
1286-
internal static extern IntPtr PyUnicode_FromOrdinal(int c);
1287-
#elif UCS4 && PYTHON3
1288-
[DllImport(PythonDll)]
1289-
internal static extern IntPtr PyUnicode_FromObject(IntPtr ob);
1290-
1291-
[DllImport(PythonDll)]
1292-
internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err);
1293-
1294-
[DllImport(PythonDll, EntryPoint = "PyUnicode_FromKindAndData")]
1295-
internal static extern IntPtr PyUnicode_FromKindAndString(
1266+
internal static extern IntPtr PyUnicode_FromKindAndData(
12961267
int kind,
12971268
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrMarshaler))] string s,
12981269
int size
12991270
);
13001271

13011272
internal static IntPtr PyUnicode_FromUnicode(string s, int size)
13021273
{
1303-
return PyUnicode_FromKindAndString(4, s, size);
1274+
return PyUnicode_FromKindAndData(UCS, s, size);
13041275
}
13051276

13061277
[DllImport(PythonDll)]
@@ -1311,26 +1282,26 @@ internal static IntPtr PyUnicode_FromUnicode(string s, int size)
13111282

13121283
[DllImport(PythonDll)]
13131284
internal static extern IntPtr PyUnicode_FromOrdinal(int c);
1314-
#elif UCS4 && PYTHON2
1315-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS4_FromObject")]
1285+
#elif PYTHON2
1286+
[DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "FromObject")]
13161287
internal static extern IntPtr PyUnicode_FromObject(IntPtr ob);
13171288

1318-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS4_FromEncodedObject")]
1289+
[DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "FromEncodedObject")]
13191290
internal static extern IntPtr PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err);
13201291

1321-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS4_FromUnicode")]
1292+
[DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "FromUnicode")]
13221293
internal static extern IntPtr PyUnicode_FromUnicode(
13231294
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StrMarshaler))] string s,
13241295
int size
13251296
);
13261297

1327-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS4_GetSize")]
1298+
[DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "GetSize")]
13281299
internal static extern int PyUnicode_GetSize(IntPtr ob);
13291300

1330-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS4_AsUnicode")]
1301+
[DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "AsUnicode")]
13311302
internal static extern IntPtr PyUnicode_AsUnicode(IntPtr ob);
13321303

1333-
[DllImport(PythonDll, EntryPoint = "PyUnicodeUCS4_FromOrdinal")]
1304+
[DllImport(PythonDll, EntryPoint = PyUnicodeEntryPoint + "FromOrdinal")]
13341305
internal static extern IntPtr PyUnicode_FromOrdinal(int c);
13351306
#endif
13361307

@@ -1359,7 +1330,7 @@ internal static string GetManagedString(IntPtr op)
13591330
#if PYTHON2 // Python 3 strings are all Unicode
13601331
if (type == PyStringType)
13611332
{
1362-
return Marshal.PtrToStringAnsi(PyString_AS_STRING(op), PyString_Size(op));
1333+
return Marshal.PtrToStringAnsi(PyString_AsString(op), PyString_Size(op));
13631334
}
13641335
#endif
13651336

src/runtime/typemanager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ internal static IntPtr AllocateTypeObject(string name)
415415
temp = Runtime.PyUnicode_FromString(name);
416416
#elif PYTHON2
417417
IntPtr temp = Runtime.PyString_FromString(name);
418-
IntPtr raw = Runtime.PyString_AS_STRING(temp);
418+
IntPtr raw = Runtime.PyString_AsString(temp);
419419
#endif
420420
Marshal.WriteIntPtr(type, TypeOffset.tp_name, raw);
421421
Marshal.WriteIntPtr(type, TypeOffset.name, temp);

0 commit comments

Comments
 (0)
0