From ef490d1fb04b32e616c0fd15cc44347d49211e05 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Wed, 10 May 2023 18:07:03 +0000 Subject: [PATCH 1/4] chore: use the Python version configured by pyenv for compilation if available --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d34aea9..9fd40977 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,7 +64,10 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) set(PYTHON_LIBRARIES ${Python_LIBRARIES}) message("Apple - Using Python:${Python_VERSION_MAJOR} - Libraries:${PYTHON_LIBRARIES} - IncludeDirs: ${PYTHON_INCLUDE_DIR}") elseif(UNIX) - find_package(PythonLibs 3.9...3.11 REQUIRED) + find_package(Python 3.9...<3.12 COMPONENTS Interpreter Development REQUIRED) + set(Python_FIND_VIRTUALENV FIRST) # (require cmake >= v3.15 and this is the default) use the Python version configured by pyenv if available + set(PYTHON_LIBRARIES ${Python_LIBRARIES}) + set(PYTHON_INCLUDE_DIR ${Python_INCLUDE_DIRS}) find_package(SpiderMonkey REQUIRED) elseif(WIN32) find_package(PythonInterp 3.9...3.11 REQUIRED) From 31fd6a06df12989372711c3048e9750715292b02 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Tue, 23 May 2023 13:59:32 +0000 Subject: [PATCH 2/4] fix(bigint): avoid reusing the cached objects for small ints --- src/IntType.cc | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/IntType.cc b/src/IntType.cc index cb8cdb18..76fc67b2 100644 --- a/src/IntType.cc +++ b/src/IntType.cc @@ -58,14 +58,13 @@ IntType::IntType(JSContext *cx, JS::BigInt *bigint) { // If the native endianness is also little-endian, // we now have consecutive bytes of 8-bit "digits" in little-endian order const uint8_t *bytes = const_cast((uint8_t *)jsDigits); - if (jsDigitCount == 0) { - // Create a new object instead of reusing the object for int 0 - // see https://github.com/python/cpython/blob/3.9/Objects/longobject.c#L862 - // https://github.com/python/cpython/blob/3.9/Objects/longobject.c#L310 - pyObject = (PyObject *)_PyLong_New(0); - } else { - pyObject = _PyLong_FromByteArray(bytes, jsDigitCount * JS_DIGIT_BYTE, true, false); - } + PyObject *pyIntObj = _PyLong_FromByteArray(bytes, jsDigitCount * JS_DIGIT_BYTE, true, false); + + // Cast to a pythonmonkey.bigint to differentiate it from a normal Python int, + // allowing Py<->JS two-way BigInt conversion. + // We don't do `Py_SET_TYPE` because `_PyLong_FromByteArray` may cache and reuse objects for small ints + pyObject = PyObject_CallOneArg(PythonMonkey_BigInt, pyIntObj); // pyObject = pythonmonkey.bigint(pyIntObj) + Py_DECREF(pyIntObj); // Set the sign bit // see https://github.com/python/cpython/blob/3.9/Objects/longobject.c#L956 @@ -73,10 +72,6 @@ IntType::IntType(JSContext *cx, JS::BigInt *bigint) { ssize_t pyDigitCount = Py_SIZE(pyObject); Py_SET_SIZE(pyObject, -pyDigitCount); } - - // Cast to a pythonmonkey.bigint to differentiate it from a normal Python int, - // allowing Py<->JS two-way BigInt conversion - Py_SET_TYPE(pyObject, (PyTypeObject *)(PythonMonkey_BigInt)); } JS::BigInt *IntType::toJsBigInt(JSContext *cx) { @@ -144,4 +139,4 @@ void IntType::print(std::ostream &os) const { os << PyUnicode_AsUTF8(str); // https://pythonextensionpatterns.readthedocs.io/en/latest/refcount.html#new-references Py_DECREF(str); // free -} \ No newline at end of file +} From d0cb5e2923145cb6e369b9efced487787945445f Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Thu, 25 May 2023 19:44:58 +0000 Subject: [PATCH 3/4] ci: temporarily test on Python 3.11 --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 01ef0077..d5896781 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -12,7 +12,7 @@ jobs: strategy: matrix: os: [ 'ubuntu-latest' ] # , 'windows-latest', 'macos-latest' ] - python_version: [ '3.10' ] + python_version: [ '3.11' ] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 From 4e5c5cf7e370dc35008b21e0a9d0cb6d02385f68 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Thu, 25 May 2023 19:55:09 +0000 Subject: [PATCH 4/4] ci: test on all supported Python versions (Python 3.9 to 3.11) --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index d5896781..96085e88 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -12,7 +12,7 @@ jobs: strategy: matrix: os: [ 'ubuntu-latest' ] # , 'windows-latest', 'macos-latest' ] - python_version: [ '3.11' ] + python_version: [ '3.9', '3.10', '3.11' ] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2