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.
209 lines
7.9 KiB
C
209 lines
7.9 KiB
C
/*
|
|
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.
|
|
|
|
Hanzo additions (2026-05-19):
|
|
- Compat shims for APIs removed in Python 3.12 / 3.13.
|
|
- Free-threading detection helper for Python 3.13+ (PEP 703).
|
|
- See HANZO_CHANGES.md.
|
|
*/
|
|
|
|
#ifndef MACRO_H
|
|
#define MACRO_H
|
|
|
|
#include "Python.h"
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Compat shims for APIs removed in Python 3.12+.
|
|
*
|
|
* Each removed *ClearFreeList() returned an int (always 0 in practice — it
|
|
* was a "best-effort free some cached memory" call). They were no-ops on
|
|
* release-mode builds even before 3.12; the cyclic GC handles the freelists
|
|
* directly now. We return 0 to preserve call-site semantics.
|
|
*
|
|
* `PyEval_InitThreads` / `PyEval_ReInitThreads` / `PyEval_AcquireLock` /
|
|
* `PyEval_ReleaseLock` were no-ops since 3.9 and removed in 3.13. We polyfill
|
|
* them as no-ops so call sites keep compiling.
|
|
*
|
|
* `Py_SetPath` / `Py_SetPythonHome` / `Py_SetProgramName` / `PySys_SetArgv` /
|
|
* `PySys_SetArgvEx` were deprecated in 3.11 and removed in 3.13. The
|
|
* supported replacement is `PyConfig` / `PyConfig_SetBytesArgv`. We polyfill
|
|
* the legacy entry points so existing call sites continue to compile; new
|
|
* code should use the PyConfig API directly.
|
|
* ---------------------------------------------------------------------------
|
|
*/
|
|
|
|
#if PY_VERSION_HEX >= 0x030C0000 /* 3.12+ */
|
|
static inline int PyFloat_ClearFreeList(void) { return 0; }
|
|
static inline int PyDict_ClearFreeList(void) { return 0; }
|
|
static inline int PyList_ClearFreeList(void) { return 0; }
|
|
static inline int PyTuple_ClearFreeList(void) { return 0; }
|
|
static inline int PySet_ClearFreeList(void) { return 0; }
|
|
static inline int PyMethod_ClearFreeList(void) { return 0; }
|
|
static inline int PyCFunction_ClearFreeList(void) { return 0; }
|
|
static inline int PyFrame_ClearFreeList(void) { return 0; }
|
|
static inline int PyAsyncGen_ClearFreeList(void) { return 0; }
|
|
#endif
|
|
|
|
#if PY_VERSION_HEX >= 0x030D0000 /* 3.13+ */
|
|
/* Removed: legacy thread init / lock helpers — no-ops since 3.9. */
|
|
static inline void _hz_PyEval_InitThreads(void) { /* no-op */ }
|
|
static inline void _hz_PyEval_ReInitThreads(void) { /* no-op */ }
|
|
static inline void _hz_PyEval_AcquireLock(void) { /* no-op */ }
|
|
static inline void _hz_PyEval_ReleaseLock(void) { /* no-op */ }
|
|
#define PyEval_InitThreads _hz_PyEval_InitThreads
|
|
#define PyEval_ReInitThreads _hz_PyEval_ReInitThreads
|
|
#define PyEval_AcquireLock _hz_PyEval_AcquireLock
|
|
#define PyEval_ReleaseLock _hz_PyEval_ReleaseLock
|
|
|
|
/* Removed: legacy path / argv setters. Implement using PyConfig where the
|
|
* caller intent is clear ("set path before Py_Initialize"). Calling these
|
|
* AFTER Py_Initialize will not have the historical effect and the binding
|
|
* docs the deprecation in the Go wrappers. */
|
|
static inline void _hz_Py_SetProgramName(const wchar_t *name) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
PyConfig_SetString(&cfg, &cfg.program_name, name);
|
|
/* Best-effort; caller is responsible for calling Py_InitializeFromConfig. */
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
static inline void _hz_Py_SetPath(const wchar_t *path) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
PyConfig_SetString(&cfg, &cfg.pythonpath_env, path);
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
static inline void _hz_Py_SetPythonHome(const wchar_t *home) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
PyConfig_SetString(&cfg, &cfg.home, home);
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
static inline void _hz_PySys_SetArgv(int argc, wchar_t **argv) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
PyConfig_SetArgv(&cfg, argc, argv);
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
static inline void _hz_PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) {
|
|
(void)updatepath;
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
PyConfig_SetArgv(&cfg, argc, argv);
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
#define Py_SetProgramName _hz_Py_SetProgramName
|
|
#define Py_SetPath _hz_Py_SetPath
|
|
#define Py_SetPythonHome _hz_Py_SetPythonHome
|
|
#define PySys_SetArgv _hz_PySys_SetArgv
|
|
#define PySys_SetArgvEx _hz_PySys_SetArgvEx
|
|
|
|
/* Py_SetStandardStreamEncoding removed in 3.13. Use PyConfig.stdio_encoding
|
|
* / PyConfig.stdio_errors instead. Polyfill returns 0 (success). */
|
|
static inline int _hz_Py_SetStandardStreamEncoding(const char *encoding, const char *errors) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
if (encoding) PyConfig_SetBytesString(&cfg, &cfg.stdio_encoding, encoding);
|
|
if (errors) PyConfig_SetBytesString(&cfg, &cfg.stdio_errors, errors);
|
|
PyConfig_Clear(&cfg);
|
|
return 0;
|
|
}
|
|
#define Py_SetStandardStreamEncoding _hz_Py_SetStandardStreamEncoding
|
|
|
|
/* PySys_AddWarnOption / PySys_AddXOption / PySys_SetPath removed in 3.13.
|
|
* Replacements live on PyConfig (warnoptions, xoptions, module_search_paths).
|
|
* Polyfill them so existing call sites compile; effect on already-init'd
|
|
* interpreter is best-effort only. */
|
|
static inline void _hz_PySys_AddWarnOption(const wchar_t *s) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
/* Append to existing warnoptions; PyWideStringList_Append handles alloc. */
|
|
PyWideStringList_Append(&cfg.warnoptions, s);
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
static inline void _hz_PySys_AddXOption(const wchar_t *s) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
PyWideStringList_Append(&cfg.xoptions, s);
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
static inline void _hz_PySys_SetPath(const wchar_t *path) {
|
|
PyConfig cfg;
|
|
PyConfig_InitPythonConfig(&cfg);
|
|
PyConfig_SetString(&cfg, &cfg.pythonpath_env, path);
|
|
PyConfig_Clear(&cfg);
|
|
}
|
|
#define PySys_AddWarnOption _hz_PySys_AddWarnOption
|
|
#define PySys_AddXOption _hz_PySys_AddXOption
|
|
#define PySys_SetPath _hz_PySys_SetPath
|
|
|
|
/* PyEval_ThreadsInitialized removed in 3.13; threads are always initialized
|
|
* on 3.7+, so return 1. */
|
|
static inline int _hz_PyEval_ThreadsInitialized(void) { return 1; }
|
|
#define PyEval_ThreadsInitialized _hz_PyEval_ThreadsInitialized
|
|
#endif
|
|
|
|
/* ---------------------------------------------------------------------------
|
|
* Free-threading awareness (PEP 703 — 3.13+).
|
|
* Py_GIL_DISABLED is defined at build time when CPython was configured with
|
|
* --disable-gil. Py_IsGILEnabled() is the runtime check (3.13+).
|
|
* ---------------------------------------------------------------------------
|
|
*/
|
|
static inline int _hz_gil_disabled(void) {
|
|
#if defined(Py_GIL_DISABLED)
|
|
return 1;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
int _go_Py_EnterRecursiveCall(const char *where);
|
|
void _go_Py_LeaveRecursiveCall();
|
|
|
|
int _go_PyType_Check(PyObject *o);
|
|
int _go_PyType_CheckExact(PyObject *o);
|
|
|
|
int _go_PyLong_Check(PyObject *p);
|
|
int _go_PyLong_CheckExact(PyObject *p);
|
|
|
|
int _go_PyBool_Check(PyObject *o);
|
|
|
|
int _go_PyFloat_Check(PyObject *p);
|
|
int _go_PyFloat_CheckExact(PyObject *p);
|
|
|
|
int _go_PyComplex_Check(PyObject *p);
|
|
int _go_PyComplex_CheckExact(PyObject *p);
|
|
|
|
int _go_PyBytes_Check(PyObject *o);
|
|
int _go_PyBytes_CheckExact(PyObject *o);
|
|
|
|
int _go_PyByteArray_Check(PyObject *o);
|
|
int _go_PyByteArray_CheckExact(PyObject *o);
|
|
|
|
int _go_PyUnicode_Check(PyObject *o);
|
|
int _go_PyUnicode_CheckExact(PyObject *o);
|
|
|
|
int _go_PyTuple_Check(PyObject *p);
|
|
int _go_PyTuple_CheckExact(PyObject *p);
|
|
|
|
int _go_PyList_Check(PyObject *p);
|
|
int _go_PyList_CheckExact(PyObject *p);
|
|
|
|
int _go_PyDict_Check(PyObject *p);
|
|
int _go_PyDict_CheckExact(PyObject *p);
|
|
|
|
int _go_PyModule_Check(PyObject *p);
|
|
int _go_PyModule_CheckExact(PyObject *p);
|
|
|
|
int _go_PyObject_DelAttr(PyObject *o, PyObject *attr_name);
|
|
int _go_PyObject_DelAttrString(PyObject *o, const char *attr_name);
|
|
|
|
int _go_PyObject_TypeCheck(PyObject *o, PyTypeObject *type);
|
|
|
|
/* Hanzo additions: free-threading + sub-interpreter helpers. */
|
|
int _go_hz_gil_disabled(void);
|
|
|
|
#endif
|