Skip to content

Commit 5357401

Browse files
committed
Refactor repeated section from UCS4/UCS2
1 parent cdb9a42 commit 5357401

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed

src/runtime/runtime.cs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,37 +1677,6 @@ internal unsafe static extern IntPtr
16771677
internal unsafe static extern IntPtr
16781678
PyUnicode_FromOrdinal(int c);
16791679
#endif
1680-
1681-
internal static IntPtr PyUnicode_FromString(string s)
1682-
{
1683-
return PyUnicode_FromUnicode(s, (s.Length));
1684-
}
1685-
1686-
internal unsafe static string GetManagedString(IntPtr op)
1687-
{
1688-
IntPtr type = PyObject_TYPE(op);
1689-
1690-
// Python 3 strings are all unicode
1691-
#if PYTHON2
1692-
if (type == Runtime.PyStringType)
1693-
{
1694-
return Marshal.PtrToStringAnsi(
1695-
PyString_AS_STRING(op),
1696-
Runtime.PyString_Size(op)
1697-
);
1698-
}
1699-
#endif
1700-
1701-
if (type == Runtime.PyUnicodeType)
1702-
{
1703-
char* p = Runtime.PyUnicode_AsUnicode(op);
1704-
int size = Runtime.PyUnicode_GetSize(op);
1705-
return new String(p, 0, size);
1706-
}
1707-
1708-
return null;
1709-
}
1710-
17111680
#endif
17121681
#if (UCS4)
17131682
#if (PYTHON33 || PYTHON34 || PYTHON35 || PYTHON36)
@@ -1800,6 +1769,7 @@ internal unsafe static extern IntPtr
18001769
internal unsafe static extern IntPtr
18011770
PyUnicode_FromOrdinal(int c);
18021771

1772+
#endif
18031773
#endif
18041774

18051775
internal static IntPtr PyUnicode_FromString(string s)
@@ -1811,8 +1781,7 @@ internal unsafe static string GetManagedString(IntPtr op)
18111781
{
18121782
IntPtr type = PyObject_TYPE(op);
18131783

1814-
// Python 3 strings are all unicode
1815-
#if PYTHON2
1784+
#if PYTHON2 // Python 3 strings are all Unicode
18161785
if (type == Runtime.PyStringType)
18171786
{
18181787
return Marshal.PtrToStringAnsi(
@@ -1824,17 +1793,22 @@ internal unsafe static string GetManagedString(IntPtr op)
18241793

18251794
if (type == Runtime.PyUnicodeType)
18261795
{
1796+
#if UCS4
18271797
IntPtr p = Runtime.PyUnicode_AsUnicode(op);
18281798
int length = Runtime.PyUnicode_GetSize(op);
18291799
int size = length*4;
18301800
byte[] buffer = new byte[size];
18311801
Marshal.Copy(p, buffer, 0, size);
18321802
return Encoding.UTF32.GetString(buffer, 0, size);
1803+
#elif UCS2
1804+
char* p = Runtime.PyUnicode_AsUnicode(op);
1805+
int size = Runtime.PyUnicode_GetSize(op);
1806+
return new String(p, 0, size);
1807+
#endif
18331808
}
18341809

18351810
return null;
18361811
}
1837-
#endif
18381812

18391813
//====================================================================
18401814
// Python dictionary API

0 commit comments

Comments
 (0)