Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Python 3.11 #1955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
os: [windows, ubuntu, macos]
python: ["3.7", "3.8", "3.9", "3.10"]
python: ["3.7", "3.8", "3.9", "3.10", "3.11"]
platform: [x64, x86]
exclude:
- os: ubuntu
Expand Down Expand Up @@ -54,15 +54,17 @@ jobs:
run: |
pip install -v .

- name: Set Python DLL path (non Windows)
- name: Set Python DLL path and PYTHONHOME (non Windows)
if: ${{ matrix.os != 'windows' }}
run: |
echo PYTHONNET_PYDLL=$(python -m find_libpython) >> $GITHUB_ENV
echo PYTHONHOME=$(python -c 'import sys; print(sys.prefix)') >> $GITHUB_ENV

- name: Set Python DLL path (Windows)
- name: Set Python DLL path and PYTHONHOME (Windows)
if: ${{ matrix.os == 'windows' }}
run: |
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append -InputObject "PYTHONNET_PYDLL=$(python -m find_libpython)"
Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append -InputObject "PYTHONHOME=$(python -c 'import sys; print(sys.prefix)')"

- name: Embedding tests
run: dotnet test --runtime any-${{ matrix.platform }} --logger "console;verbosity=detailed" src/embed_tests/
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"clr_loader>=0.2.2,<0.3.0"
]

requires-python = ">=3.7, <3.11"
requires-python = ">=3.7, <3.12"

classifiers = [
"Development Status :: 5 - Production/Stable",
Expand All @@ -25,6 +25,7 @@ classifiers = [
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS :: MacOS X",
Expand Down
54 changes: 35 additions & 19 deletions src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class TestPythonEngineProperties
[Test]
public static void GetBuildinfoDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.BuildInfo;
Expand All @@ -21,6 +22,7 @@ public static void GetBuildinfoDoesntCrash()
[Test]
public static void GetCompilerDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Compiler;
Expand All @@ -34,6 +36,7 @@ public static void GetCompilerDoesntCrash()
[Test]
public static void GetCopyrightDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Copyright;
Expand All @@ -46,6 +49,7 @@ public static void GetCopyrightDoesntCrash()
[Test]
public static void GetPlatformDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Platform;
Expand All @@ -58,6 +62,7 @@ public static void GetPlatformDoesntCrash()
[Test]
public static void GetVersionDoesntCrash()
{
PythonEngine.Initialize();
using (Py.GIL())
{
string s = PythonEngine.Version;
Expand Down Expand Up @@ -91,9 +96,6 @@ public static void GetProgramNameDefault()
/// Test default behavior of PYTHONHOME. If ENVVAR is set it will
/// return the same value. If not, returns EmptyString.
/// </summary>
/// <remarks>
/// AppVeyor.yml has been update to tests with ENVVAR set.
/// </remarks>
[Test]
public static void GetPythonHomeDefault()
{
Expand All @@ -109,22 +111,19 @@ public static void GetPythonHomeDefault()
[Test]
public void SetPythonHome()
{
// We needs to ensure that engine was started and shutdown at least once before setting dummy home.
// Otherwise engine will not run with dummy path with random problem.
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
}

PythonEngine.Initialize();
var pythonHomeBackup = PythonEngine.PythonHome;
PythonEngine.Shutdown();

var pythonHomeBackup = PythonEngine.PythonHome;
if (pythonHomeBackup == "")
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");

var pythonHome = "/dummypath/";

PythonEngine.PythonHome = pythonHome;
PythonEngine.Initialize();

Assert.AreEqual(pythonHome, PythonEngine.PythonHome);
PythonEngine.Shutdown();

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

var pythonHomeBackup = PythonEngine.PythonHome;
if (pythonHomeBackup == "")
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");

var pythonHome = "/dummypath/";

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

[Test]
[Ignore("Currently buggy in Python")]
public void SetPythonHomeEmptyString()
{
PythonEngine.Initialize();

var backup = PythonEngine.PythonHome;
if (backup == "")
{
PythonEngine.Shutdown();
Assert.Inconclusive("Can't reset PythonHome to empty string, skipping");
}
PythonEngine.PythonHome = "";

Assert.AreEqual("", PythonEngine.PythonHome);

PythonEngine.PythonHome = backup;
PythonEngine.Shutdown();
}

[Test]
public void SetProgramName()
{
Expand Down Expand Up @@ -202,7 +218,7 @@ public void SetPythonPath()
// The list sys.path is initialized with this value on interpreter startup;
// it can be (and usually is) modified later to change the search path for loading modules.
// See https://docs.python.org/3/c-api/init.html#c.Py_GetPath
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.
// After PythonPath is set, then PythonEngine.PythonPath will correctly return the full search path.

PythonEngine.Shutdown();

Expand Down
141 changes: 141 additions & 0 deletions src/runtime/Native/TypeOffset311.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

// Auto-generated by geninterop.py.
// DO NOT MODIFY BY HAND.

// Python 3.11: ABI flags: ''

// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

using Python.Runtime.Native;

namespace Python.Runtime
{
[SuppressMessage("Style", "IDE1006:Naming Styles",
Justification = "Following CPython",
Scope = "type")]

[StructLayout(LayoutKind.Sequential)]
internal class TypeOffset311 : GeneratedTypeOffsets, ITypeOffsets
{
public TypeOffset311() { }
// Auto-generated from PyHeapTypeObject in Python.h
public int ob_refcnt { get; private set; }
public int ob_type { get; private set; }
public int ob_size { get; private set; }
public int tp_name { get; private set; }
public int tp_basicsize { get; private set; }
public int tp_itemsize { get; private set; }
public int tp_dealloc { get; private set; }
public int tp_vectorcall_offset { get; private set; }
public int tp_getattr { get; private set; }
public int tp_setattr { get; private set; }
public int tp_as_async { get; private set; }
public int tp_repr { get; private set; }
public int tp_as_number { get; private set; }
public int tp_as_sequence { get; private set; }
public int tp_as_mapping { get; private set; }
public int tp_hash { get; private set; }
public int tp_call { get; private set; }
public int tp_str { get; private set; }
public int tp_getattro { get; private set; }
public int tp_setattro { get; private set; }
public int tp_as_buffer { get; private set; }
public int tp_flags { get; private set; }
public int tp_doc { get; private set; }
public int tp_traverse { get; private set; }
public int tp_clear { get; private set; }
public int tp_richcompare { get; private set; }
public int tp_weaklistoffset { get; private set; }
public int tp_iter { get; private set; }
public int tp_iternext { get; private set; }
public int tp_methods { get; private set; }
public int tp_members { get; private set; }
public int tp_getset { get; private set; }
public int tp_base { get; private set; }
public int tp_dict { get; private set; }
public int tp_descr_get { get; private set; }
public int tp_descr_set { get; private set; }
public int tp_dictoffset { get; private set; }
public int tp_init { get; private set; }
public int tp_alloc { get; private set; }
public int tp_new { get; private set; }
public int tp_free { get; private set; }
public int tp_is_gc { get; private set; }
public int tp_bases { get; private set; }
public int tp_mro { get; private set; }
public int tp_cache { get; private set; }
public int tp_subclasses { get; private set; }
public int tp_weaklist { get; private set; }
public int tp_del { get; private set; }
public int tp_version_tag { get; private set; }
public int tp_finalize { get; private set; }
public int tp_vectorcall { get; private set; }
public int am_await { get; private set; }
public int am_aiter { get; private set; }
public int am_anext { get; private set; }
public int am_send { get; private set; }
public int nb_add { get; private set; }
public int nb_subtract { get; private set; }
public int nb_multiply { get; private set; }
public int nb_remainder { get; private set; }
public int nb_divmod { get; private set; }
public int nb_power { get; private set; }
public int nb_negative { get; private set; }
public int nb_positive { get; private set; }
public int nb_absolute { get; private set; }
public int nb_bool { get; private set; }
public int nb_invert { get; private set; }
public int nb_lshift { get; private set; }
public int nb_rshift { get; private set; }
public int nb_and { get; private set; }
public int nb_xor { get; private set; }
public int nb_or { get; private set; }
public int nb_int { get; private set; }
public int nb_reserved { get; private set; }
public int nb_float { get; private set; }
public int nb_inplace_add { get; private set; }
public int nb_inplace_subtract { get; private set; }
public int nb_inplace_multiply { get; private set; }
public int nb_inplace_remainder { get; private set; }
public int nb_inplace_power { get; private set; }
public int nb_inplace_lshift { get; private set; }
public int nb_inplace_rshift { get; private set; }
public int nb_inplace_and { get; private set; }
public int nb_inplace_xor { get; private set; }
public int nb_inplace_or { get; private set; }
public int nb_floor_divide { get; private set; }
public int nb_true_divide { get; private set; }
public int nb_inplace_floor_divide { get; private set; }
public int nb_inplace_true_divide { get; private set; }
public int nb_index { get; private set; }
public int nb_matrix_multiply { get; private set; }
public int nb_inplace_matrix_multiply { get; private set; }
public int mp_length { get; private set; }
public int mp_subscript { get; private set; }
public int mp_ass_subscript { get; private set; }
public int sq_length { get; private set; }
public int sq_concat { get; private set; }
public int sq_repeat { get; private set; }
public int sq_item { get; private set; }
public int was_sq_slice { get; private set; }
public int sq_ass_item { get; private set; }
public int was_sq_ass_slice { get; private set; }
public int sq_contains { get; private set; }
public int sq_inplace_concat { get; private set; }
public int sq_inplace_repeat { get; private set; }
public int bf_getbuffer { get; private set; }
public int bf_releasebuffer { get; private set; }
public int name { get; private set; }
public int ht_slots { get; private set; }
public int qualname { get; private set; }
public int ht_cached_keys { get; private set; }
public int ht_module { get; private set; }
public int _ht_tpname { get; private set; }
public int spec_cache_getitem { get; private set; }
}
}
17 changes: 12 additions & 5 deletions src/runtime/PythonEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public static bool IsInitialized
get { return initialized; }
}

private static void EnsureInitialized()
{
if (!IsInitialized)
throw new InvalidOperationException(
"Python must be initialized for this operation"
);
}

/// <summary>Set to <c>true</c> to enable GIL debugging assistance.</summary>
public static bool DebugGIL { get; set; } = false;

Expand Down Expand Up @@ -96,17 +104,16 @@ public static string PythonHome
{
get
{
EnsureInitialized();
IntPtr p = Runtime.TryUsingDll(() => Runtime.Py_GetPythonHome());
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
}
set
{
// this value is null in the beginning
Marshal.FreeHGlobal(_pythonHome);
_pythonHome = Runtime.TryUsingDll(
() => UcsMarshaler.Py3UnicodePy2StringtoPtr(value)
);
Runtime.Py_SetPythonHome(_pythonHome);
_pythonHome = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
Runtime.TryUsingDll(() => Runtime.Py_SetPythonHome(_pythonHome));
}
}

Expand All @@ -128,7 +135,7 @@ public static string PythonPath
}

public static Version MinSupportedVersion => new(3, 7);
public static Version MaxSupportedVersion => new(3, 10, int.MaxValue, int.MaxValue);
public static Version MaxSupportedVersion => new(3, 11, int.MaxValue, int.MaxValue);
public static bool IsSupportedVersion(Version version) => version >= MinSupportedVersion && version <= MaxSupportedVersion;

public static string Version
Expand Down
1 change: 1 addition & 0 deletions src/runtime/PythonTypes/PyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private static StolenReference FromSpec(TypeSpec spec, PyTuple? bases = null)
using var nativeSpec = new NativeTypeSpec(spec);
var basesRef = bases is null ? default : bases.Reference;
var result = Runtime.PyType_FromSpecWithBases(in nativeSpec, basesRef);
// Runtime.PyErr_Print();
return result.StealOrThrow();
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,9 @@ internal static unsafe nint Refcount(BorrowedReference op)
[Pure]
internal static int Refcount32(BorrowedReference op) => checked((int)Refcount(op));

internal static void TryUsingDll(Action op) =>
TryUsingDll(() => { op(); return 0; });

/// <summary>
/// Call specified function, and handle PythonDLL-related failures.
/// </summary>
Expand Down
Loading