| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6a8a97d commit 195cde6
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,7 +28,7 @@ public void CanCreateHeapType() | |||
| 28 | 28 | const string name = "nÁmæ"; | |
| 29 | 29 | const string docStr = "dÁcæ"; | |
| 30 | 30 | ||
| 31 | - using var doc = new StrPtr(docStr, Encoding.UTF8); | ||
| 31 | + using var doc = new StrPtr(docStr, Encodings.UTF8); | ||
| 32 | 32 | var spec = new TypeSpec( | |
| 33 | 33 | name: name, | |
| 34 | 34 | basicSize: Util.ReadInt32(Runtime.Runtime.PyBaseObjectType, TypeOffset.tp_basicsize), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ public unsafe static int Initialize(IntPtr data, int size) | |||
| 12 | 12 | { | |
| 13 | 13 | try | |
| 14 | 14 | { | |
| 15 | - var dllPath = Encoding.UTF8.GetString((byte*)data.ToPointer(), size); | ||
| 15 | + var dllPath = Encodings.UTF8.GetString((byte*)data.ToPointer(), size); | ||
| 16 | 16 | ||
| 17 | 17 | if (!string.IsNullOrEmpty(dllPath)) | |
| 18 | 18 | { | |
@@ -33,15 +33,15 @@ public unsafe static int Initialize(IntPtr data, int size) | |||
| 33 | 33 | ); | |
| 34 | 34 | return 1; | |
| 35 | 35 | } | |
| 36 | - | ||
| 36 | + | ||
| 37 | 37 | return 0; | |
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | 40 | public unsafe static int Shutdown(IntPtr data, int size) | |
| 41 | 41 | { | |
| 42 | 42 | try | |
| 43 | 43 | { | |
| 44 | - var command = Encoding.UTF8.GetString((byte*)data.ToPointer(), size); | ||
| 44 | + var command = Encodings.UTF8.GetString((byte*)data.ToPointer(), size); | ||
| 45 | 45 | ||
| 46 | 46 | if (command == "full_shutdown") | |
| 47 | 47 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,7 @@ public int GetNativeDataSize() | |||
| 42 | 42 | internal class UcsMarshaler : MarshalerBase | |
| 43 | 43 | { | |
| 44 | 44 | internal static readonly int _UCS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 4; | |
| 45 | - internal static readonly Encoding PyEncoding = _UCS == 2 ? Encoding.Unicode : Encoding.UTF32; | ||
| 45 | + internal static readonly Encoding PyEncoding = _UCS == 2 ? Encodings.UTF16 : Encodings.UTF32; | ||
| 46 | 46 | private static readonly MarshalerBase Instance = new UcsMarshaler(); | |
| 47 | 47 | ||
| 48 | 48 | public override IntPtr MarshalManagedToNative(object managedObj) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,7 +17,7 @@ public NativeTypeSpec(TypeSpec spec) | |||
| 17 | 17 | { | |
| 18 | 18 | if (spec is null) throw new ArgumentNullException(nameof(spec)); | |
| 19 | 19 | ||
| 20 | - this.Name = new StrPtr(spec.Name, Encoding.UTF8); | ||
| 20 | + this.Name = new StrPtr(spec.Name, Encodings.UTF8); | ||
| 21 | 21 | this.BasicSize = spec.BasicSize; | |
| 22 | 22 | this.ItemSize = spec.ItemSize; | |
| 23 | 23 | this.Flags = (int)spec.Flags; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,7 +53,7 @@ public string Name | |||
| 53 | 53 | { | |
| 54 | 54 | RawPointer = Util.ReadIntPtr(this, TypeOffset.tp_name), | |
| 55 | 55 | }; | |
| 56 | - return namePtr.ToString(System.Text.Encoding.UTF8)!; | ||
| 56 | + return namePtr.ToString(Encodings.UTF8)!; | ||
| 57 | 57 | } | |
| 58 | 58 | } | |
| 59 | 59 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -795,13 +795,13 @@ public static int Py_Main(int argc, string[] argv) | |||
| 795 | 795 | ||
| 796 | 796 | internal static int PyRun_SimpleString(string code) | |
| 797 | 797 | { | |
| 798 | - using var codePtr = new StrPtr(code, Encoding.UTF8); | ||
| 798 | + using var codePtr = new StrPtr(code, Encodings.UTF8); | ||
| 799 | 799 | return Delegates.PyRun_SimpleStringFlags(codePtr, Utf8String); | |
| 800 | 800 | } | |
| 801 | 801 | ||
| 802 | 802 | internal static NewReference PyRun_String(string code, RunFlagType st, BorrowedReference globals, BorrowedReference locals) | |
| 803 | 803 | { | |
| 804 | - using var codePtr = new StrPtr(code, Encoding.UTF8); | ||
| 804 | + using var codePtr = new StrPtr(code, Encodings.UTF8); | ||
| 805 | 805 | return Delegates.PyRun_StringFlags(codePtr, st, globals, locals, Utf8String); | |
| 806 | 806 | } | |
| 807 | 807 | ||
@@ -813,14 +813,14 @@ internal static NewReference PyRun_String(string code, RunFlagType st, BorrowedR | |||
| 813 | 813 | /// </summary> | |
| 814 | 814 | internal static NewReference Py_CompileString(string str, string file, int start) | |
| 815 | 815 | { | |
| 816 | - using var strPtr = new StrPtr(str, Encoding.UTF8); | ||
| 816 | + using var strPtr = new StrPtr(str, Encodings.UTF8); | ||
| 817 | 817 | using var fileObj = new PyString(file); | |
| 818 | 818 | return Delegates.Py_CompileStringObject(strPtr, fileObj, start, Utf8String, -1); | |
| 819 | 819 | } | |
| 820 | 820 | ||
| 821 | 821 | internal static NewReference PyImport_ExecCodeModule(string name, BorrowedReference code) | |
| 822 | 822 | { | |
| 823 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 823 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 824 | 824 | return Delegates.PyImport_ExecCodeModule(namePtr, code); | |
| 825 | 825 | } | |
| 826 | 826 | ||
@@ -867,13 +867,13 @@ internal static bool PyObject_IsIterable(BorrowedReference ob) | |||
| 867 | 867 | ||
| 868 | 868 | internal static int PyObject_HasAttrString(BorrowedReference pointer, string name) | |
| 869 | 869 | { | |
| 870 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 870 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 871 | 871 | return Delegates.PyObject_HasAttrString(pointer, namePtr); | |
| 872 | 872 | } | |
| 873 | 873 | ||
| 874 | 874 | internal static NewReference PyObject_GetAttrString(BorrowedReference pointer, string name) | |
| 875 | 875 | { | |
| 876 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 876 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 877 | 877 | return Delegates.PyObject_GetAttrString(pointer, namePtr); | |
| 878 | 878 | } | |
| 879 | 879 | ||
@@ -884,12 +884,12 @@ internal static NewReference PyObject_GetAttrString(BorrowedReference pointer, S | |||
| 884 | 884 | internal static int PyObject_DelAttr(BorrowedReference @object, BorrowedReference name) => Delegates.PyObject_SetAttr(@object, name, null); | |
| 885 | 885 | internal static int PyObject_DelAttrString(BorrowedReference @object, string name) | |
| 886 | 886 | { | |
| 887 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 887 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 888 | 888 | return Delegates.PyObject_SetAttrString(@object, namePtr, null); | |
| 889 | 889 | } | |
| 890 | 890 | internal static int PyObject_SetAttrString(BorrowedReference @object, string name, BorrowedReference value) | |
| 891 | 891 | { | |
| 892 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 892 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 893 | 893 | return Delegates.PyObject_SetAttrString(@object, namePtr, value); | |
| 894 | 894 | } | |
| 895 | 895 | ||
@@ -1071,7 +1071,7 @@ internal static bool PyBool_CheckExact(BorrowedReference ob) | |||
| 1071 | 1071 | ||
| 1072 | 1072 | internal static NewReference PyLong_FromString(string value, int radix) | |
| 1073 | 1073 | { | |
| 1074 | - using var valPtr = new StrPtr(value, Encoding.UTF8); | ||
| 1074 | + using var valPtr = new StrPtr(value, Encodings.UTF8); | ||
| 1075 | 1075 | return Delegates.PyLong_FromString(valPtr, IntPtr.Zero, radix); | |
| 1076 | 1076 | } | |
| 1077 | 1077 | ||
@@ -1252,12 +1252,14 @@ internal static bool PyString_CheckExact(BorrowedReference ob) | |||
| 1252 | 1252 | ||
| 1253 | 1253 | internal static NewReference PyString_FromString(string value) | |
| 1254 | 1254 | { | |
| 1255 | + int byteorder = BitConverter.IsLittleEndian ? -1 : 1; | ||
| 1256 | + int* byteorderPtr = &byteorder; | ||
| 1255 | 1257 | fixed(char* ptr = value) | |
| 1256 | 1258 | return Delegates.PyUnicode_DecodeUTF16( | |
| 1257 | 1259 | (IntPtr)ptr, | |
| 1258 | 1260 | value.Length * sizeof(Char), | |
| 1259 | 1261 | IntPtr.Zero, | |
| 1260 | - IntPtr.Zero | ||
| 1262 | + (IntPtr)byteorderPtr | ||
| 1261 | 1263 | ); | |
| 1262 | 1264 | } | |
| 1263 | 1265 | ||
@@ -1272,7 +1274,7 @@ internal static NewReference EmptyPyBytes() | |||
| 1272 | 1274 | internal static NewReference PyByteArray_FromStringAndSize(IntPtr strPtr, nint len) => Delegates.PyByteArray_FromStringAndSize(strPtr, len); | |
| 1273 | 1275 | internal static NewReference PyByteArray_FromStringAndSize(string s) | |
| 1274 | 1276 | { | |
| 1275 | - using var ptr = new StrPtr(s, Encoding.UTF8); | ||
| 1277 | + using var ptr = new StrPtr(s, Encodings.UTF8); | ||
| 1276 | 1278 | return PyByteArray_FromStringAndSize(ptr.RawPointer, checked((nint)ptr.ByteCount)); | |
| 1277 | 1279 | } | |
| 1278 | 1280 | ||
@@ -1300,7 +1302,7 @@ internal static IntPtr PyBytes_AsString(BorrowedReference ob) | |||
| 1300 | 1302 | ||
| 1301 | 1303 | internal static NewReference PyUnicode_InternFromString(string s) | |
| 1302 | 1304 | { | |
| 1303 | - using var ptr = new StrPtr(s, Encoding.UTF8); | ||
| 1305 | + using var ptr = new StrPtr(s, Encodings.UTF8); | ||
| 1304 | 1306 | return Delegates.PyUnicode_InternFromString(ptr); | |
| 1305 | 1307 | } | |
| 1306 | 1308 | ||
@@ -1375,7 +1377,7 @@ internal static bool PyDict_Check(BorrowedReference ob) | |||
| 1375 | 1377 | ||
| 1376 | 1378 | internal static BorrowedReference PyDict_GetItemString(BorrowedReference pointer, string key) | |
| 1377 | 1379 | { | |
| 1378 | - using var keyStr = new StrPtr(key, Encoding.UTF8); | ||
| 1380 | + using var keyStr = new StrPtr(key, Encodings.UTF8); | ||
| 1379 | 1381 | return Delegates.PyDict_GetItemString(pointer, keyStr); | |
| 1380 | 1382 | } | |
| 1381 | 1383 | ||
@@ -1391,7 +1393,7 @@ internal static BorrowedReference PyDict_GetItemString(BorrowedReference pointer | |||
| 1391 | 1393 | /// </summary> | |
| 1392 | 1394 | internal static int PyDict_SetItemString(BorrowedReference dict, string key, BorrowedReference value) | |
| 1393 | 1395 | { | |
| 1394 | - using var keyPtr = new StrPtr(key, Encoding.UTF8); | ||
| 1396 | + using var keyPtr = new StrPtr(key, Encodings.UTF8); | ||
| 1395 | 1397 | return Delegates.PyDict_SetItemString(dict, keyPtr, value); | |
| 1396 | 1398 | } | |
| 1397 | 1399 | ||
@@ -1400,7 +1402,7 @@ internal static int PyDict_SetItemString(BorrowedReference dict, string key, Bor | |||
| 1400 | 1402 | ||
| 1401 | 1403 | internal static int PyDict_DelItemString(BorrowedReference pointer, string key) | |
| 1402 | 1404 | { | |
| 1403 | - using var keyPtr = new StrPtr(key, Encoding.UTF8); | ||
| 1405 | + using var keyPtr = new StrPtr(key, Encodings.UTF8); | ||
| 1404 | 1406 | return Delegates.PyDict_DelItemString(pointer, keyPtr); | |
| 1405 | 1407 | } | |
| 1406 | 1408 | ||
@@ -1515,7 +1517,7 @@ internal static bool PyIter_Check(BorrowedReference ob) | |||
| 1515 | 1517 | ||
| 1516 | 1518 | internal static NewReference PyModule_New(string name) | |
| 1517 | 1519 | { | |
| 1518 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 1520 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 1519 | 1521 | return Delegates.PyModule_New(namePtr); | |
| 1520 | 1522 | } | |
| 1521 | 1523 | ||
@@ -1529,7 +1531,7 @@ internal static NewReference PyModule_New(string name) | |||
| 1529 | 1531 | /// <returns>Return -1 on error, 0 on success.</returns> | |
| 1530 | 1532 | internal static int PyModule_AddObject(BorrowedReference module, string name, StolenReference value) | |
| 1531 | 1533 | { | |
| 1532 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 1534 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 1533 | 1535 | IntPtr valueAddr = value.DangerousGetAddressOrNull(); | |
| 1534 | 1536 | int res = Delegates.PyModule_AddObject(module, namePtr, valueAddr); | |
| 1535 | 1537 | // We can't just exit here because the reference is stolen only on success. | |
@@ -1547,7 +1549,7 @@ internal static int PyModule_AddObject(BorrowedReference module, string name, St | |||
| 1547 | 1549 | ||
| 1548 | 1550 | internal static NewReference PyImport_ImportModule(string name) | |
| 1549 | 1551 | { | |
| 1550 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 1552 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 1551 | 1553 | return Delegates.PyImport_ImportModule(namePtr); | |
| 1552 | 1554 | } | |
| 1553 | 1555 | ||
@@ -1556,7 +1558,7 @@ internal static NewReference PyImport_ImportModule(string name) | |||
| 1556 | 1558 | ||
| 1557 | 1559 | internal static BorrowedReference PyImport_AddModule(string name) | |
| 1558 | 1560 | { | |
| 1559 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 1561 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 1560 | 1562 | return Delegates.PyImport_AddModule(namePtr); | |
| 1561 | 1563 | } | |
| 1562 | 1564 | ||
@@ -1584,13 +1586,13 @@ internal static void PySys_SetArgvEx(int argc, string[] argv, int updatepath) | |||
| 1584 | 1586 | ||
| 1585 | 1587 | internal static BorrowedReference PySys_GetObject(string name) | |
| 1586 | 1588 | { | |
| 1587 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 1589 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 1588 | 1590 | return Delegates.PySys_GetObject(namePtr); | |
| 1589 | 1591 | } | |
| 1590 | 1592 | ||
| 1591 | 1593 | internal static int PySys_SetObject(string name, BorrowedReference ob) | |
| 1592 | 1594 | { | |
| 1593 | - using var namePtr = new StrPtr(name, Encoding.UTF8); | ||
| 1595 | + using var namePtr = new StrPtr(name, Encodings.UTF8); | ||
| 1594 | 1596 | return Delegates.PySys_SetObject(namePtr, ob); | |
| 1595 | 1597 | } | |
| 1596 | 1598 | ||
@@ -1689,7 +1691,7 @@ internal static IntPtr PyMem_Malloc(long size) | |||
| 1689 | 1691 | ||
| 1690 | 1692 | internal static void PyErr_SetString(BorrowedReference ob, string message) | |
| 1691 | 1693 | { | |
| 1692 | - using var msgPtr = new StrPtr(message, Encoding.UTF8); | ||
| 1694 | + using var msgPtr = new StrPtr(message, Encodings.UTF8); | ||
| 1693 | 1695 | Delegates.PyErr_SetString(ob, msgPtr); | |
| 1694 | 1696 | } | |
| 1695 | 1697 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Text; | ||
| 3 | + | ||
| 4 | + namespace Python.Runtime; | ||
| 5 | + | ||
| 6 | + static class Encodings { | ||
| 7 | + public static System.Text.Encoding UTF8 = new UTF8Encoding(false, true); | ||
| 8 | + public static System.Text.Encoding UTF16 = new UnicodeEncoding(!BitConverter.IsLittleEndian, false, true); | ||
| 9 | + public static System.Text.Encoding UTF32 = new UTF32Encoding(!BitConverter.IsLittleEndian, false, true); | ||
| 10 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -510,6 +510,9 @@ def test_string_conversion(): | |||
| 510 | 510 | ob.StringField = System.String(u'\uffff\uffff') | |
| 511 | 511 | assert ob.StringField == u'\uffff\uffff' | |
| 512 | 512 | ||
| 513 | + ob.StringField = System.String("\ufeffbom") | ||
| 514 | + assert ob.StringField == "\ufeffbom" | ||
| 515 | + | ||
| 513 | 516 | ob.StringField = None | |
| 514 | 517 | assert ob.StringField is None | |
| 515 | 518 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments