8000 detect the size of wchar_t (aka Runtime.UCS) at runtime using PyUnico… · pythonnet/pythonnet@67c6d11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 67c6d11

Browse files
committed
detect the size of wchar_t (aka Runtime.UCS) at runtime using PyUnicode_GetMax
1 parent 182faed commit 67c6d11

File tree

6 files changed

+29
-62
lines changed

6 files changed

+29
-62
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ csharp_new_line_before_finally = true
3434
[*.sln]
3535
indent_style = tab
3636

37+
[*.csproj]
38+
charset = utf-8
39+
insert_final_newline = true
40+
3741
# bumpversion reformats itself after every bump
3842
[.bumpversion.cfg]
3943
trim_trailing_whitespace = false

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1313

1414
### Changed
1515
- Drop support for Python 2, 3.4, and 3.5
16+
- `wchar_t` size aka `Runtime.UCS` is now determined at runtime
1617
- `clr.AddReference` may now throw errors besides `FileNotFoundException`, that provide more
1718
details about the cause of the failure
1819
- `clr.AddReference` no longer adds ".dll" implicitly

setup.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,8 @@ def build_extension(self, ext):
251251
if not os.path.exists(dest_dir):
252252
os.makedirs(dest_dir)
253253

254-
# Up to Python 3.2 sys.maxunicode is used to determine the size of
255-
# Py_UNICODE, but from 3.3 onwards Py_UNICODE is a typedef of wchar_t.
256-
import ctypes
257-
unicode_width = ctypes.sizeof(ctypes.c_wchar)
258-
259254
defines = [
260255
"PYTHON{0}{1}".format(PY_MAJOR, PY_MINOR),
261-
"UCS{0}".format(unicode_width),
262256
]
263257

264258
if CONFIG == "Debug":

src/runtime/Python.Runtime.15.csproj

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@
4949
<Python3Version>$(PYTHONNET_PY3_VERSION)</Python3Version>
5050
<Python3Version Condition="'$(Python3Version)'==''">PYTHON38</Python3Version>
5151
<PythonWinDefineConstants>$(PYTHONNET_WIN_DEFINE_CONSTANTS)</PythonWinDefineConstants>
52-
<PythonWinDefineConstants Condition="'$(PythonWinDefineConstants)'==''">UCS2</PythonWinDefineConstants>
5352
<PythonMonoDefineConstants>$(PYTHONNET_MONO_DEFINE_CONSTANTS)</PythonMonoDefineConstants>
54-
<PythonMonoDefineConstants Condition="'$(PythonMonoDefineConstants)'==''">UCS4;MONO_LINUX;PYTHON_WITH_PYMALLOC</PythonMonoDefineConstants>
53+
<PythonMonoDefineConstants Condition="'$(PythonMonoDefineConstants)'==''">MONO_LINUX;PYTHON_WITH_PYMALLOC</PythonMonoDefineConstants>
5554
<PythonInteropFile Condition="'$(PythonInteropFile)'==''">$(PYTHONNET_INTEROP_FILE)</PythonInteropFile>
5655
</PropertyGroup>
5756
<PropertyGroup Condition="$(Configuration.Contains('Debug')) AND '$(TargetFramework)'=='net40'">
@@ -143,20 +142,20 @@
143142
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
144143
</ItemGroup>
145144

146-
<ItemGroup>
147-
<None Update="intern_.tt">
148-
<Generator>TextTemplatingFileGenerator</Generator>
149-
<LastGenOutput>intern_.cs</LastGenOutput>
150-
</None>
145+
<ItemGroup>
146+
<None Update="intern_.tt">
147+
<Generator>TextTemplatingFileGenerator</Generator>
148+
<LastGenOutput>intern_.cs</LastGenOutput>
149+
</None>
151150
</ItemGroup>
152151

153152

154-
<ItemGroup>
155-
<Compile Update="intern_.cs">
156-
<DesignTime>True</DesignTime>
157-
<AutoGen>True</AutoGen>
158-
<DependentUpon>intern_.tt</DependentUpon>
159-
</Compile>
153+
<ItemGroup>
154+
<Compile Update="intern_.cs">
155+
<DesignTime>True</DesignTime>
156+
<AutoGen>True</AutoGen>
157+
<DependentUpon>intern_.tt</DependentUpon>
158+
</Compile>
160159
</ItemGroup>
161160

162161
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

src/runtime/Python.Runtime.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,46 @@
2929
<PlatformTarget>x64</PlatformTarget>
3030
</PropertyGroup>-->
3131
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMono'">
32-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS4</DefineConstants>
32+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27</DefineConstants>
3333
<Optimize>true</Optimize>
3434
<DebugType>pdbonly</DebugType>
3535
</PropertyGroup>
3636
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseMonoPY3'">
37-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS4</DefineConstants>
37+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38</DefineConstants>
3838
<Optimize>true</Optimize>
3939
<DebugType>pdbonly</DebugType>
4040
</PropertyGroup>
4141
<PropertyGroup Condition=" '$(Configuration)' == 'DebugMono'">
4242
<DebugSymbols>true</DebugSymbols>
43-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS4;TRACE;DEBUG</DefineConstants>
43+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;TRACE;DEBUG</DefineConstants>
4444
<Optimize>false</Optimize>
4545
<DebugType>full</DebugType>
4646
</PropertyGroup>
4747
<PropertyGroup Condition=" '$(Configuration)' == 'DebugMonoPY3'">
4848
<DebugSymbols>true</DebugSymbols>
49-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS4;TRACE;DEBUG</DefineConstants>
49+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;TRACE;DEBUG</DefineConstants>
5050
<Optimize>false</Optimize>
5151
<DebugType>full</DebugType>
5252
</PropertyGroup>
5353
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWin'">
54-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS2</DefineConstants>
54+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27</DefineConstants>
5555
<Optimize>true</Optimize>
5656
<DebugType>pdbonly</DebugType>
5757
</PropertyGroup>
5858
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseWinPY3'">
59-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS2</DefineConstants>
59+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38</DefineConstants>
6060
<Optimize>true</Optimize>
6161
<DebugType>pdbonly</DebugType>
6262
</PropertyGroup>
6363
<PropertyGroup Condition=" '$(Configuration)' == 'DebugWin'">
6464
<DebugSymbols>true</DebugSymbols>
65-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;UCS2;TRACE;DEBUG</DefineConstants>
65+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON2;PYTHON27;TRACE;DEBUG</DefineConstants>
6666
<Optimize>false</Optimize>
6767
<DebugType>full</DebugType>
6868
</PropertyGroup>
6969
<PropertyGroup Condition=" '$(Configuration)' == 'DebugWinPY3'">
7070
<DebugSymbols>true</DebugSymbols>
71-
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;UCS2;TRACE;DEBUG</DefineConstants>
71+
<DefineConstants Condition="'$(DefineConstants)' == ''">PYTHON3;PYTHON38;TRACE;DEBUG</DefineConstants>
7272
<Optimize>false</Optimize>
7373
<DebugType>full</DebugType>
7474
</PropertyGroup>

src/runtime/runtime.cs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,8 @@ namespace Python.Runtime
1818
/// </summary>
1919
public class Runtime
2020
{
21-
// C# compiler copies constants to the assemblies that references this library.
22-
// We needs to replace all public constants to static readonly fields to allow
23-
// binary substitution of different Python.Runtime.dll builds in a target application.
24-
2521
public static int UCS => _UCS;
26-
27-
#if UCS4
28-
internal const int _UCS = 4;
29-
30-
/// <summary>
31-
/// EntryPoint to be used in DllImport to map to correct Unicode
32-
/// methods prior to PEP393. Only used for PY27.
33-
/// </summary>
34-
private const string PyUnicodeEntryPoint = "PyUnicodeUCS4_";
35-
#elif UCS2
36-
internal const int _UCS = 2;
37-
38-
/// <summary>
39-
/// EntryPoint to be used in DllImport to map to correct Unicode
40-
/// methods prior to PEP393. Only used for PY27.
41-
/// </summary>
42-
private const string PyUnicodeEntryPoint = "PyUnicodeUCS2_";
43-
#else
44-
#error You must define either UCS2 or UCS4!
45-
#endif
22+
internal static readonly int _UCS = PyUnicode_GetMax() <= 0xFFFF ? 2 : 4;
4623

4724
#if PYTHON36
4825
const string _minor = "6";
@@ -1537,17 +1514,6 @@ internal static IntPtr PyBytes_AS_STRING(IntPtr ob)
15371514
return ob + BytesOffset.ob_sval;
15381515
}
15391516

1540-
internal static IntPtr PyString_FromStringAndSize(string value, long size)
1541-
{
1542-
return _PyString_FromStringAndSize(value, new IntPtr(size));
1543-
}
1544-
1545-
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl,
1546-
EntryPoint = "PyUnicode_FromStringAndSize")]
1547-
internal static extern IntPtr _PyString_FromStringAndSize(
1548-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value,
1549-
IntPtr size
1550-
);
15511517

15521518
internal static IntPtr PyUnicode_FromStringAndSize(IntPtr value, long size)
15531519
{
@@ -1588,6 +1554,9 @@ internal static IntPtr PyUnicode_FromUnicode(string s, long size)
15881554
return PyUnicode_FromKindAndData(_UCS, s, size);
15891555
}
15901556

1557+
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1558+
internal static extern int PyUnicode_GetMax();
1559+
15911560
internal static long PyUnicode_GetSize(IntPtr ob)
15921561
{
15931562
return (long)_PyUnicode_GetSize(ob);

0 commit comments

Comments
 (0)
0