mirror of
https://github.com/hanzoai/cpy3.git
synced 2026-10-30 18:42:30 +00:00
* feat: Python 3.12+/3.13 compat + sub-interpreter wrapper
Hanzo fork of go-python/cpy3. See HANZO_CHANGES.md for full diff.
- Polyfill APIs removed in 3.12 (per-type ClearFreeList helpers) and
3.13 (PyEval_InitThreads, Py_SetPath/PythonHome/ProgramName,
PySys_SetArgv/AddWarnOption/AddXOption/SetPath,
Py_SetStandardStreamEncoding, PyEval_ThreadsInitialized) so existing
call sites compile against modern CPython. Polyfills route to the
PyConfig API where there's a meaningful replacement, return 0/no-op
otherwise.
- New PEP 684 SubInterpreter wrapper (subinterp.go) — recommended
primitive for tenant/per-worker isolation on 3.12+. Defaults follow
PEP 684 (own GIL, threads on, no daemon threads, refuse non-isolated
C extensions). Falls back to Py_NewInterpreter on 3.11 and older.
- Free-threading awareness (PEP 703) — IsGILDisabled() reports at
runtime whether the linked CPython was built with --disable-gil.
- pkg-config switched from "python3" to "python3-embed" so libpython
links cleanly on Linux distros that split the embed flags.
- Module path: github.com/go-python/cpy3 -> github.com/hanzoai/cpy3.
Go 1.21+. Package name (python3) unchanged.
- CI matrix: Python {3.11, 3.12, 3.13} x Go {1.21, 1.22} on Ubuntu.
License remains MIT. Original go-python team credited in HANZO_CHANGES.md
and every modified file's header.
* feat: JSON hot-path helper + 3.13t build script
- LoadSource + CallJSONFunctionByName: single-cgo-crossing helpers
for the common "JSON-in, JSON-out" extension embedding contract.
Roughly 2x faster than the long form via individual wrappers and
stack-safer under sustained load (macOS SIGBUS at ~400k iters of
the 9-call long form is mitigated by collapsing to 1 cgo call).
- ErrUnknownFunction: sentinel error for missing/non-callable
lookups so callers can errors.Is.
- scripts/build-python313t.sh: idempotent local build of CPython
3.13.x with --disable-gil. Writes a python3-embed.pc so callers
steer via PKG_CONFIG_PATH.
- HANZO_CHANGES.md + README updated.
- .gitignore: exclude python313t-build/ python313t-src/.
* ci: split test runs by Python global state
TestSubInterpreter_* and TestCallJSONFunctionByName_* / TestLoadSource_*
share global Py state with the data-type/error tests. Running them
in the same `go test` invocation can hang on darwin after the
data-type tests have already touched the main interpreter. Split
into two test steps so each starts from a fresh process.
* test: move json_hotpath tests in-package + share Py init
Move from package python3_test to package python3 so they share
Py_Initialize() state with subinterp_test.go. Each test pins its
own OS thread, calls Py_Initialize (idempotent), creates a fresh
sub-interp, and unpins on return. No PyEval_SaveThread in teardown
— the sub-interp's Close already released the calling thread's GIL.
This eliminates a SIGSEGV-on-second-test pattern caused by trying
to PyEval_SaveThread a state that's already been saved.
* docs: README example for the JSON hot path
Show LoadSource + CallJSONFunctionByName end-to-end with luxfi/log
for context. The helpers themselves take no logger dependency.
84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
/*
|
|
Unless explicitly stated otherwise all files in this repository are licensed
|
|
under the MIT License.
|
|
This product includes software developed at Datadog (https://www.datadoghq.com/).
|
|
Copyright 2018 Datadog, Inc.
|
|
*/
|
|
|
|
package python3
|
|
|
|
/*
|
|
#include "Python.h"
|
|
#include "macro.h"
|
|
*/
|
|
import "C"
|
|
|
|
// PyEval_InitThreads / PyEval_ReInitThreads were no-ops since Python 3.9
|
|
// and removed in Python 3.13. They are kept here for API compatibility;
|
|
// on 3.13+ they polyfill to no-ops via macro.h.
|
|
//
|
|
// PyGILState_Ensure / PyGILState_Release continue to work but are
|
|
// essentially no-ops on free-threaded (--disable-gil, PEP 703) builds.
|
|
// Use IsGILDisabled() to detect.
|
|
|
|
//PyThreadState : https://docs.python.org/3/c-api/init.html#c.PyThreadState
|
|
type PyThreadState C.PyThreadState
|
|
|
|
//PyGILState is an opaque “handle” to the thread state when PyGILState_Ensure() was called, and must be passed to PyGILState_Release() to ensure Python is left in the same state
|
|
type PyGILState C.PyGILState_STATE
|
|
|
|
//PyEval_InitThreads : https://docs.python.org/3/c-api/init.html#c.PyEval_InitThreads
|
|
func PyEval_InitThreads() {
|
|
C.PyEval_InitThreads()
|
|
}
|
|
|
|
//PyEval_ThreadsInitialized : https://docs.python.org/3/c-api/init.html#c.PyEval_ThreadsInitialized
|
|
func PyEval_ThreadsInitialized() bool {
|
|
return C.PyEval_ThreadsInitialized() != 0
|
|
}
|
|
|
|
//PyEval_SaveThread : https://docs.python.org/3/c-api/init.html#c.PyEval_SaveThread
|
|
func PyEval_SaveThread() *PyThreadState {
|
|
return (*PyThreadState)(C.PyEval_SaveThread())
|
|
}
|
|
|
|
//PyEval_RestoreThread : https://docs.python.org/3/c-api/init.html#c.PyEval_RestoreThread
|
|
func PyEval_RestoreThread(tstate *PyThreadState) {
|
|
C.PyEval_RestoreThread((*C.PyThreadState)(tstate))
|
|
}
|
|
|
|
//PyThreadState_Get : https://docs.python.org/3/c-api/init.html#c.PyThreadState_Get
|
|
func PyThreadState_Get() *PyThreadState {
|
|
return (*PyThreadState)(C.PyThreadState_Get())
|
|
}
|
|
|
|
//PyThreadState_Swap : https://docs.python.org/3/c-api/init.html#c.PyThreadState_Swap
|
|
func PyThreadState_Swap(tstate *PyThreadState) *PyThreadState {
|
|
return (*PyThreadState)(C.PyThreadState_Swap((*C.PyThreadState)(tstate)))
|
|
}
|
|
|
|
//PyEval_ReInitThreads : https://docs.python.org/3/c-api/init.html#c.PyEval_ReInitThreads
|
|
func PyEval_ReInitThreads() {
|
|
C.PyEval_ReInitThreads()
|
|
}
|
|
|
|
//PyGILState_Ensure : https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
|
|
func PyGILState_Ensure() PyGILState {
|
|
return PyGILState(C.PyGILState_Ensure())
|
|
}
|
|
|
|
//PyGILState_Release : https://docs.python.org/3/c-api/init.html#c.PyGILState_Release
|
|
func PyGILState_Release(state PyGILState) {
|
|
C.PyGILState_Release(C.PyGILState_STATE(state))
|
|
}
|
|
|
|
//PyGILState_GetThisThreadState : https://docs.python.org/3/c-api/init.html#c.PyGILState_GetThisThreadState
|
|
func PyGILState_GetThisThreadState() *PyThreadState {
|
|
return (*PyThreadState)(C.PyGILState_GetThisThreadState())
|
|
}
|
|
|
|
//PyGILState_Check : https://docs.python.org/3/c-api/init.html#c.PyGILState_Check
|
|
func PyGILState_Check() bool {
|
|
return C.PyGILState_Check() == 1
|
|
}
|