Skip to content

Commit 6968d01

Browse files
authored
Merge pull request #1955 from filmor/python-3.11
Python 3.11
2 parents fb17907 + 15e2e95 commit 6968d01

File tree

11 files changed

+307
-104
lines changed

11 files changed

+307
-104
lines changed

.github/workflows/main.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
fail-fast: false
1717
matrix:
1818
os: [windows, ubuntu, macos]
19-
python: ["3.7", "3.8", "3.9", "3.10"]
19+
python: ["3.7", "3.8", "3.9", "3.10", "3.11"]
2020
platform: [x64, x86]
2121
exclude:
2222
- os: ubuntu
@@ -54,15 +54,17 @@ jobs:
5454
run: |
5555
pip install -v .
5656
57-
- name: Set Python DLL path (non Windows)
57+
- name: Set Python DLL path and PYTHONHOME (non Windows)
5858
if: ${{ matrix.os != 'windows' }}
5959
run: |
6060
echo PYTHONNET_PYDLL=$(python -m find_libpython) >> $GITHUB_ENV
61+
echo PYTHONHOME=$(python -c 'import sys; print(sys.prefix)') >> $GITHUB_ENV
6162
62-
- name: Set Python DLL path (Windows)
63+
- name: Set Python DLL path and PYTHONHOME (Windows)
6364
if: ${{ matrix.os == 'windows' }}
6465
run: |
6566
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append -InputObject "PYTHONNET_PYDLL=$(python -m find_libpython)"
67+
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append -InputObject "PYTHONHOME=$(python -c 'import sys; print(sys.prefix)')"
6668
6769
- name: Embedding tests
6870
run: dotnet test --runtime any-${{ matrix.platform }} --logger "console;verbosity=detailed" src/embed_tests/

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
"clr_loader>=0.2.2,<0.3.0"
1414
]
1515

16-
requires-python = ">=3.7, <3.11"
16+
requires-python = ">=3.7, <3.12"
1717

1818
classifiers = [
1919
"Development Status :: 5 - Production/Stable",
@@ -25,6 +25,7 @@ classifiers = [
2525
"Programming Language :: Python :: 3.8",
2626
"Programming Language :: Python :: 3.9",
2727
"Programming Language :: Python :: 3.10",
28+
"Programming Language :: Python :: 3.11",
2829
"Operating System :: Microsoft :: Windows",
2930
"Operating System :: POSIX :: Linux",
3031
"Operating System :: MacOS :: MacOS X",

src/embed_tests/TestPythonEngineProperties.cs

+35-19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class TestPythonEngineProperties
99
[Test]
1010
public static void GetBuildinfoDoesntCrash()
1111
{
12+
PythonEngine.Initialize();
1213
using (Py.GIL())
1314
{
1415
string s = PythonEngine.BuildInfo;
@@ -21,6 +22,7 @@ public static void GetBuildinfoDoesntCrash()
2122
[Test]
2223
public static void GetCompilerDoesntCrash()
2324
{
25+
PythonEngine.Initialize();
2426
using (Py.GIL())
2527
{
2628
string s = PythonEngine.Compiler;
@@ -34,6 +36,7 @@ public static void GetCompilerDoesntCrash()
3436
[Test]
3537
public static void GetCopyrightDoesntCrash()
3638
{
39+
PythonEngine.Initialize();
3740
using (Py.GIL())
3841
{
3942
string s = PythonEngine.Copyright;
@@ -46,6 +49,7 @@ public static void GetCopyrightDoesntCrash()
4649
[Test]
4750
public static void GetPlatformDoesntCrash()
4851
{
52+
PythonEngine.Initialize();
4953
using (Py.GIL())
5054
{
5155
string s = PythonEngine.Platform;
@@ -58,6 +62,7 @@ public static void GetPlatformDoesntCrash()
5862
[Test]
5963
public static void GetVersionDoesntCrash()
6064
{
65+
PythonEngine.Initialize();
6166
using (Py.GIL())
6267
{
6368
string s = PythonEngine.Version;
@@ -91,9 +96,6 @@ public static void GetProgramNameDefault()
9196
/// Test default behavior of PYTHONHOME. If ENVVAR is set it will
9297
/// return the same value. If not, returns EmptyString.
9398
/// </summary>
94-
/// <remarks>
95-
/// AppVeyor.yml has been update to tests with ENVVAR set.
96-
/// </remarks>
9799
[Test]
98100
public static void GetPythonHomeDefault()
99101
{
@@ -109,22 +111,19 @@ public static void GetPythonHomeDefault()
109111
[Test]
110112
public void SetPythonHome()
111113
{
112-
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
113-
// Otherwise engine will not run with dummy path with random problem.
114-
if (!PythonEngine.IsInitialized)
115-
{
116-
PythonEngine.Initialize();
117-
}
118-
114+
PythonEngine.Initialize();
115+
var pythonHomeBackup = PythonEngine.PythonHome;
119116
PythonEngine.Shutdown();
120117

121-
var pythonHomeBackup = PythonEngine.PythonHome;
118+
if (pythonHomeBackup == "")
119+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
122120

123121
var pythonHome = "/dummypath/";
124122

125123
PythonEngine.PythonHome = pythonHome;
126124
PythonEngine.Initialize();
127125

126+
Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
128127
PythonEngine.Shutdown();
129128

130129
// Restoring valid pythonhome.
@@ -134,15 +133,12 @@ public void SetPythonHome()
134133
[Test]
135134
public void SetPythonHomeTwice()
136135
{
137-
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
138-
// Otherwise engine will not run with dummy path with random problem.
139-
if (!PythonEngine.IsInitialized)
140-
{
141-
PythonEngine.Initialize();
142-
}
136+
PythonEngine.Initialize();
137+
var pythonHomeBackup = PythonEngine.PythonHome;
143138
PythonEngine.Shutdown();
144139

145-
var pythonHomeBackup = PythonEngine.PythonHome;
140+
if (pythonHomeBackup == "")
141+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
146142

147143
var pythonHome = "/dummypath/";
148144

@@ -156,6 +152,26 @@ public void SetPythonHomeTwice()
156152
PythonEngine.PythonHome = pythonHomeBackup;
157153
}
158154

155+
[Test]
156+
[Ignore("Currently buggy in Python")]
157+
public void SetPythonHomeEmptyString()
158+
{
159+
PythonEngine.Initialize();
160+
161+
var backup = PythonEngine.PythonHome;
162+
if (backup == "")
163+
{
164+
PythonEngine.Shutdown();
165+
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
166+
}
167+
PythonEngine.PythonHome = "";
168+
169+
Assert.AreEqual("", PythonEngine.PythonHome);
170+
171+
PythonEngine.PythonHome = backup;
172+
PythonEngine.Shutdown();
173+
}
174+
159175
[Test]
160176
public void SetProgramName()
161177
{
@@ -202,7 +218,7 @@ public void SetPythonPath()
202218
// The list sys.path is initialized with this value on interpreter startup;
203219
// it can be (and usually is) modified later to change the search path for loading modules.
204220
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
205-
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
221+
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
206222

207223
PythonEngine.Shutdown();
208224

src/runtime/Native/TypeOffset311.cs

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
// Auto-generated by geninterop.py.
3+
// DO NOT MODIFY BY HAND.
4+
5+
// Python 3.11: ABI flags: ''
6+
7+
// ReSharper disable InconsistentNaming
8+
// ReSharper disable IdentifierTypo
9+
10+
using System;
11+
using System.Diagnostics.CodeAnalysis;
12+
using System.Runtime.InteropServices;
13+
14+
using Python.Runtime.Native;
15+
16+
namespace Python.Runtime
17+
{
18+
[SuppressMessage("Style", "IDE1006:Naming Styles",
19+
Justification = "Following CPython",
20+
Scope = "type")]
21+
22+
[StructLayout(LayoutKind.Sequential)]
23+
internal class TypeOffset311 : GeneratedTypeOffsets, ITypeOffsets
24+
{
25+
public TypeOffset311() { }
26+
// Auto-generated from PyHeapTypeObject in Python.h
27+
public int ob_refcnt { get; private set; }
28+
public int ob_type { get; private set; }
29+
public int ob_size { get; private set; }
30+
public int tp_name { get; private set; }
31+
public int tp_basicsize { get; private set; }
32+
public int tp_itemsize { get; private set; }
33+
public int tp_dealloc { get; private set; }
34+
public int tp_vectorcall_offset { get; private set; }
35+
public int tp_getattr { get; private set; }
36+
public int tp_setattr { get; private set; }
37+
public int tp_as_async { get; private set; }
38+
public int tp_repr { get; private set; }
39+
public int tp_as_number { get; private set; }
40+
public int tp_as_sequence { get; private set; }
41+
public int tp_as_mapping { get; private set; }
42+
public int tp_hash { get; private set; }
43+
public int tp_call { get; private set; }
44+
public int tp_str { get; private set; }
45+
public int tp_getattro { get; private set; }
46+
public int tp_setattro { get; private set; }
47+
public int tp_as_buffer { get; private set; }
48+
public int tp_flags { get; private set; }
49+
public int tp_doc { get; private set; }
50+
public int tp_traverse { get; private set; }
51+
public int tp_clear { get; private set; }
52+
public int tp_richcompare { get; private set; }
53+
public int tp_weaklistoffset { get; private set; }
54+
public int tp_iter { get; private set; }
55+
public int tp_iternext { get; private set; }
56+
public int tp_methods { get; private set; }
57+
public int tp_members { get; private set; }
58+
public int tp_getset { get; private set; }
59+
public int tp_base { get; private set; }
60+
public int tp_dict { get; private set; }
61+
public int tp_descr_get { get; private set; }
62+
public int tp_descr_set { get; private set; }
63+
public int tp_dictoffset { get; private set; }
64+
public int tp_init { get; private set; }
65+
public int tp_alloc { get; private set; }
66+
public int tp_new { get; private set; }
67+
public int tp_free { get; private set; }
68+
public int tp_is_gc { get; private set; }
69+
public int tp_bases { get; private set; }
70+
public int tp_mro { get; private set; }
71+
public int tp_cache { get; private set; }
72+
public int tp_subclasses { get; private set; }
73+
public int tp_weaklist { get; private set; }
74+
public int tp_del { get; private set; }
75+
public int tp_version_tag { get; private set; }
76+
public int tp_finalize { get; private set; }
77+
public int tp_vectorcall { get; private set; }
78+
public int am_await { get; private set; }
79+
public int am_aiter { get; private set; }
80+
public int am_anext { get; private set; }
81+
public int am_send { get; private set; }
82+
public int nb_add { get; private set; }
83+
public int nb_subtract { get; private set; }
84+
public int nb_multiply { get; private set; }
85+
public int nb_remainder { get; private set; }
86+
public int nb_divmod { get; private set; }
87+
public int nb_power { get; private set; }
88+
public int nb_negative { get; private set; }
89+
public int nb_positive { get; private set; }
90+
public int nb_absolute { get; private set; }
91+
public int nb_bool { get; private set; }
92+
public int nb_invert { get; private set; }
93+
public int nb_lshift { get; private set; }
94+
public int nb_rshift { get; private set; }
95+
public int nb_and { get; private set; }
96+
public int nb_xor { get; private set; }
97+
public int nb_or { get; private set; }
98+
public int nb_int { get; private set; }
99+
public int nb_reserved { get; private set; }
100+
public int nb_float { get; private set; }
101+
public int nb_inplace_add { get; private set; }
102+
public int nb_inplace_subtract { get; private set; }
103+
public int nb_inplace_multiply { get; private set; }
104+
public int nb_inplace_remainder { get; private set; }
105+
public int nb_inplace_power { get; private set; }
106+
public int nb_inplace_lshift { get; private set; }
107+
public int nb_inplace_rshift { get; private set; }
108+
public int nb_inplace_and { get; private set; }
109+
public int nb_inplace_xor { get; private set; }
110+
public int nb_inplace_or { get; private set; }
111+
public int nb_floor_divide { get; private set; }
112+
public int nb_true_divide { get; private set; }
113+
public int nb_inplace_floor_divide { get; private set; }
114+
public int nb_inplace_true_divide { get; private set; }
115+
public int nb_index { get; private set; }
116+
public int nb_matrix_multiply { get; private set; }
117+
public int nb_inplace_matrix_multiply { get; private set; }
118+
public int mp_length { get; private set; }
119+
public int mp_subscript { get; private set; }
120+
public int mp_ass_subscript { get; private set; }
121+
public int sq_length { get; private set; }
122+
public int sq_concat { get; private set; }
123+
public int sq_repeat { get; private set; }
124+
public int sq_item { get; private set; }
125+
public int was_sq_slice { get; private set; }
126+
public int sq_ass_item { get; private set; }
127+
public int was_sq_ass_slice { get; private set; }
128+
public int sq_contains { get; private set; }
129+
public int sq_inplace_concat { get; private set; }
130+
public int sq_inplace_repeat { get; private set; }
131+
public int bf_getbuffer { get; private set; }
132+
public int bf_releasebuffer { get; private set; }
133+
public int name { get; private set; }
134+
public int ht_slots { get; private set; }
135+
public int qualname { get; private set; }
136+
public int ht_cached_keys { get; private set; }
137+
public int ht_module { get; private set; }
138+
public int _ht_tpname { get; private set; }
139+
public int spec_cache_getitem { get; private set; }
140+
}
141+
}

src/runtime/PythonEngine.cs

+12-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public static bool IsInitialized
4747
get { return initialized; }
4848
}
4949

50+
private static void EnsureInitialized()
51+
{
52+
if (!IsInitialized)
53+
throw new InvalidOperationException(
54+
"Python must be initialized for this operation"
55+
);
56+
}
57+
5058
/// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary>
5159
public static bool DebugGIL { get; set; } = false;
5260

@@ -96,17 +104,16 @@ public static string PythonHome
96104
{
97105
get
98106
{
107+
EnsureInitialized();
99108
IntPtr p = Runtime.TryUsingDll(() => Runtime.Py_GetPythonHome());
100109
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
101110
}
102111
set
103112
{
104113
// this value is null in the beginning
105114
Marshal.FreeHGlobal(_pythonHome);
106-
_pythonHome = Runtime.TryUsingDll(
107-
() => UcsMarshaler.Py3UnicodePy2StringtoPtr(value)
108-
);
109-
Runtime.Py_SetPythonHome(_pythonHome);
115+
_pythonHome = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
116+
Runtime.TryUsingDll(() => Runtime.Py_SetPythonHome(_pythonHome));
110117
}
111118
}
112119

@@ -128,7 +135,7 @@ public static string PythonPath
128135
}
129136

130137
public static Version MinSupportedVersion => new(3, 7);
131-
public static Version MaxSupportedVersion => new(3, 10, int.MaxValue, int.MaxValue);
138+
public static Version MaxSupportedVersion => new(3, 11, int.MaxValue, int.MaxValue);
132139
public static bool IsSupportedVersion(Version version) => version >= MinSupportedVersion && version <= MaxSupportedVersion;
133140

134141
public static string Version

src/runtime/PythonTypes/PyType.cs

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ private static StolenReference FromSpec(TypeSpec spec, PyTuple? bases = null)
155155
using var nativeSpec = new NativeTypeSpec(spec);
156156
var basesRef = bases is null ? default : bases.Reference;
157157
var result = Runtime.PyType_FromSpecWithBases(in nativeSpec, basesRef);
158+
// Runtime.PyErr_Print();
158159
return result.StealOrThrow();
159160
}
160161
}

src/runtime/Runtime.cs

+3
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,9 @@ internal static unsafe nint Refcount(BorrowedReference op)
672672
[Pure]
673673
internal static int Refcount32(BorrowedReference op) => checked((int)Refcount(op));
674674

675+
internal static void TryUsingDll(Action op) =>
676+
TryUsingDll(() => { op(); return 0; });
677+
675678
/// <summary>
676679
/// Call specified function, and handle PythonDLL-related failures.
677680
/// </summary>

0 commit comments

Comments
 (0)