-
Notifications
You must be signed in to change notification settings - Fork 40
python 3.13 support #442
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
python 3.13 support #442
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
a9144ea
chore(deps): upgrade numpy to support Python 3.13
Xmader d80c162
fix: `_PyDictView` removed from Python's public API
Xmader dac79df
fix: Python 3.13 removed `_PyThreadState_GetDict(PyThreadState *)` fr…
Xmader 9b7c598
chore(CI): enable Python 3.13 build in the CI
Xmader fc734e2
Merge branch 'main' into Xmader/feat/python-3.13-support
Xmader dc4442d
WIP: remove the use of the `_PyArg_CheckPositional` private API
Xmader 3feb5b7
fix: Python 3.13 moved the undocumented `_PyArg_UnpackKeywords` funct…
Xmader e4a4fda
fix: Python 3.13 moved the undocumented `_PyErr_SetKeyError` function…
Xmader 428353c
fix: Python 3.13 added the `PyLong_AsNativeBytes` API, but changed th…
Xmader a96d52b
Merge branch 'main' into Xmader/feat/python-3.13-support
Xmader 2f30f99
cleanup unused code
philippedistributive db5ba71
fix: `_Py_IsFinalizing` is renamed to `Py_IsFinalizing` in Python 3.13
Xmader 07da1db
refactor: move the `_PyDictViewObject` shim to the `pyshim.hh` header…
Xmader a8ac1f6
feat: add shim for `_PyDictView_New`
Xmader 9fd5412
fix: `_PyErr_SetKeyError` is removed in Python 3.13
Xmader 8d2f1a1
WIP: `_PyThreadState_GetDict(tstate)` API gets removed in Python 3.13
Xmader 9826939
WIP: `_PyArg_UnpackKeywords` is removed
Xmader f6de9c3
fix: `_PyDictViewObject` typedef redefinition with different types on…
Xmader 7d07ee1
fix: `Py_NewRef` API is not available in Python < 3.10
Xmader 3368d3b
Revert "WIP: remove the use of the `_PyArg_CheckPositional` private API"
Xmader 943a431
fix: `_PyArg_CheckPositional` function became an internal API since P…
Xmader 7c451e5
refactor: move the `_PyDictView_New` shim to pyshim.hh
Xmader d6e4cff
refactor: shim for `_PyArg_CheckPositional`
Xmader 7c03098
Revert "refactor: shim for `_PyArg_CheckPositional`"
Xmader d3c0f85
fix: `_PyArg_UnpackKeywords` API is removed, use `PyArg_ParseTupleAnd…
Xmader 3706a47
fix: `_PyArg_UnpackKeywords` API is removed, use `PyArg_ParseTupleAnd…
Xmader 92798a8
fix: `_PyThreadState_GetDict(tstate)` API gets removed in Python 3.13
Xmader ec20bdf
fix: replacement for the `_PyThreadState_GetDict(tstate)` API in Pyth…
Xmader aea53bd
get rid of the build warnings
Xmader b2fb169
refactor: `_getLoopOnThread` getting thread dict
Xmader a5fbef8
feat: to get the running event-loop in Python 3.13 is as simple as `t…
Xmader 29b6bae
feat: keep track of the local variables inside Python `eval`/`exec` t…
Xmader 7f02431
fix: `_PyArg_CheckPositional` may also be when unpacking a tuple
Xmader a2d9ce8
refactor: use the `_PyDictView_New` function in Python < 3.13
Xmader 623d83e
refactor: keep using `_PyErr_SetKeyError` in Python < 3.13
Xmader a1cdfb3
refactor: move `_PyErr_SetKeyError` shim to pyshim.hh
Xmader 56b6409
refactor: rename the `PyDictViewObject_new` shim to `PyDictView_New`
Xmader 3f7f722
refactor: add shim for `Py_SET_SIZE`
Xmader bcfcbf3
refactor: add shim for `PyObject_CallOneArg`
Xmader 68c288e
refactor: `_PyObject_CallOneArg` is already an alias for `PyObject_Ca…
Xmader 23d0576
revert commit 68c288eadaf6fe37f62b09b2107e2363eedc46b1.
Xmader 6bf3d70
refactor: replace `PyObject_CallNoArgs(func)` calls with `PyObject_Ca…
Xmader c4d9405
refactor: add shim for `PyLong_AsByteArray` to work with different fu…
Xmader 3e2aad2
feat: add back the ability to mutate parent scope variables with `pm.…
Xmader 7ebe149
fix: add back `python.getenv` that was accidentally deleted in `3e2aad2`
Xmader bb78bbd
CI: use the release version of Python 3.13
Xmader 1a98043
fix the reference count for dicts `test_get_default_not_found`
Xmader File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
#include <jsapi.h> | ||
|
||
#include <Python.h> | ||
#include "include/pyshim.hh" | ||
|
||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
#include <jsapi.h> | ||
|
||
#include <Python.h> | ||
#include "include/pyshim.hh" | ||
|
||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
#include <jsapi.h> | ||
|
||
#include <Python.h> | ||
#include "include/pyshim.hh" | ||
|
||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/** | ||
* @file pyshim.hh | ||
* @author Tom Tang ([email protected]) | ||
* @brief Python's C APIs are constantly changing in different versions of CPython. | ||
* PythonMonkey has a wide variety of CPython versions' support. (Currently Python 3.8-3.13) | ||
* This file helps our Python API calls work with different Python versions in the same code base. | ||
* @date 2024-09-20 | ||
* | ||
* @copyright Copyright (c) 2024 Distributive Corp. | ||
* | ||
*/ | ||
|
||
#ifndef PythonMonkey_py_version_shim_ | ||
#define PythonMonkey_py_version_shim_ | ||
|
||
#include <Python.h> | ||
|
||
/** | ||
* @brief `_Py_IsFinalizing` becomes a stable API in Python 3.13, | ||
* and renames to `Py_IsFinalizing` | ||
*/ | ||
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13 | ||
#define Py_IsFinalizing Py_IsFinalizing | ||
#else | ||
#define Py_IsFinalizing _Py_IsFinalizing | ||
#endif | ||
|
||
/** | ||
* @brief `_PyDictViewObject` type definition moved from Python's public API | ||
* to the **internal** header file `internal/pycore_dict.h` in Python 3.13. | ||
* | ||
* @see https://github.com/python/cpython/blob/v3.13.0rc1/Include/internal/pycore_dict.h#L64-L72 | ||
*/ | ||
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13 | ||
typedef struct { | ||
PyObject_HEAD | ||
PyDictObject *dv_dict; | ||
} _PyDictViewObject; | ||
#endif | ||
|
||
/** | ||
* @brief Shim for `_PyArg_CheckPositional`. | ||
* Since Python 3.13, `_PyArg_CheckPositional` function became an internal API. | ||
* @see Modified from https://github.com/python/cpython/blob/v3.13.0rc1/Python/getargs.c#L2738-L2780 | ||
*/ | ||
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13 | ||
inline int _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, Py_ssize_t min, Py_ssize_t max) { | ||
if (!name) { // _PyArg_CheckPositional may also be when unpacking a tuple | ||
name = "unpacked tuple"; // https://github.com/python/cpython/blob/v3.13.0rc1/Python/getargs.c#L2746 | ||
} | ||
|
||
if (nargs < min) { | ||
PyErr_Format( | ||
PyExc_TypeError, | ||
"%.200s expected %s%zd argument%s, got %zd", | ||
name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); | ||
return 0; | ||
} | ||
|
||
if (nargs == 0) { | ||
return 1; | ||
} | ||
|
||
if (nargs > max) { | ||
PyErr_Format( | ||
PyExc_TypeError, | ||
"%.200s expected %s%zd argument%s, got %zd", | ||
name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); | ||
return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
#endif | ||
|
||
/** | ||
* @brief Shim for `_PyDictView_New`. | ||
* Since Python 3.13, `_PyDictView_New` function became an internal API. | ||
* @see Modified from https://github.com/python/cpython/blob/v3.13.0rc1/Objects/dictobject.c#L5806-L5827 | ||
*/ | ||
inline PyObject *PyDictView_New(PyObject *dict, PyTypeObject *type) { | ||
#if PY_VERSION_HEX < 0x030d0000 // Python version is lower than 3.13 | ||
return _PyDictView_New(dict, type); | ||
#else | ||
_PyDictViewObject *dv; | ||
dv = PyObject_GC_New(_PyDictViewObject, type); | ||
if (dv == NULL) | ||
return NULL; | ||
Py_INCREF(dict); | ||
dv->dv_dict = (PyDictObject *)dict; | ||
PyObject_GC_Track(dv); | ||
return (PyObject *)dv; | ||
#endif | ||
} | ||
|
||
/** | ||
* @brief Shim for `_PyErr_SetKeyError`. | ||
* Since Python 3.13, `_PyErr_SetKeyError` function became an internal API. | ||
*/ | ||
inline void PyErr_SetKeyError(PyObject *key) { | ||
// Use the provided API when possible, as `PyErr_SetObject`'s behaviour is more complex than originally thought | ||
// see also: https://github.com/python/cpython/issues/101578 | ||
#if PY_VERSION_HEX < 0x030d0000 // Python version is lower than 3.13 | ||
return _PyErr_SetKeyError(key); | ||
#else | ||
return PyErr_SetObject(PyExc_KeyError, key); | ||
#endif | ||
} | ||
|
||
/** | ||
* @brief Shim for `Py_SET_SIZE`. | ||
* `Py_SET_SIZE` is not available in Python < 3.9 | ||
*/ | ||
#ifndef Py_SET_SIZE | ||
static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) { | ||
ob->ob_size = size; | ||
} | ||
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject *)(ob), size) | ||
#endif | ||
|
||
/** | ||
* @brief Shim for `PyObject_CallOneArg`. | ||
* `PyObject_CallOneArg` is not available in Python < 3.9 | ||
*/ | ||
#if PY_VERSION_HEX < 0x03090000 // Python version is less than 3.9 | ||
inline PyObject *PyObject_CallOneArg(PyObject *func, PyObject *arg) { | ||
return PyObject_CallFunction(func, "O", arg); | ||
} | ||
#endif | ||
|
||
/** | ||
* @brief Shim for `_PyLong_AsByteArray`. | ||
* Python 3.13.0a4 added a new public API `PyLong_AsNativeBytes()` to replace the private `_PyLong_AsByteArray()`. | ||
* But this change also modified the function signature of `_PyLong_AsByteArray()`. | ||
* @see https://github.com/python/cpython/issues/111140 | ||
*/ | ||
inline int PyLong_AsByteArray(PyLongObject *v, unsigned char *bytes, size_t n, bool little_endian, bool is_signed) { | ||
#if PY_VERSION_HEX >= 0x030d0000 // Python version is 3.13 or higher | ||
return _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed, /*with_exceptions*/ false); | ||
#else | ||
return _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed); | ||
#endif | ||
} | ||
|
||
#endif // #ifndef PythonMonkey_py_version_shim_ | ||
Xmader marked this conversation as resolved.
Show resolved
Hide resolved
Xmader marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.