Skip to content

Commit e35e029

Browse files
Martin Lifacebook-github-bot
Martin Li
authored andcommitted
Native Python for 3.13
Summary: Make native python work for python 3.13. The primary purpose of this is to unblock 3.13 ft native python testing within meta. A (likely different) version of this will be upstreamed: see https://discuss.python.org/t/c-api-for-initializing-statically-linked-extension-modules/43396 and python/cpython#116146. In 3.13 private methods were removed and import.c was refactored significantly. Resolve by patching cpython with exported functions. Calling the functions from our own c code couldn't be done since the function we needed are not exported and if we export those functions we might as well just patch python. The basis of the patch is the same as 3.12 StaticExtensionLoader is a loader that accesses an unsynchronized map. However since this map is only written at compliation time (generated at build time and never updated) this should be thread safe. The loader has been denoted as not using GIL as documented in https://py-free-threading.github.io/porting/ Comments were added and a small refactor was made for clarity. This diff also re-enables tests that currently fail w/ 3.13 and ft. > If youre a future contributor (or possibly me trying to fix 3.14) looking at this being like 'im so confused whats going on' i've documented most of my seplunking [here](https://fb.workplace.com/groups/1059551942441557/permalink/1183541110042639/) and [here](https://fb.workplace.com/groups/1059551942441557/permalink/1177009517362465/) Reviewed By: itamaro, zsol Differential Revision: D58795157 fbshipit-source-id: 08df4604859593295c646bf4386ef267052449f1
1 parent 8923506 commit e35e029

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Include/cpython/import.h

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
PyMODINIT_FUNC PyInit__imp(void);
66

7+
8+
// START META PATCH (expose C API to call a module init function for statically linked extensions)
9+
PyAPI_FUNC(PyObject *) _Ci_PyImport_CallInitFuncWithContext(
10+
const char* context, PyObject* (*initfunc)(void));
11+
12+
13+
PyAPI_FUNC(int) _Ci_PyImport_FinishSinglePhaseExtension(
14+
int *tstate, PyObject *mod, void* cached, PyObject *name, PyObject *modules);
15+
// END META PATCH
716
struct _inittab {
817
const char *name; /* ASCII encoded string */
918
PyObject* (*initfunc)(void);

Python/import.c

+19
Original file line numberDiff line numberDiff line change
@@ -3400,7 +3400,26 @@ PyImport_ImportModule(const char *name)
34003400
Py_DECREF(pname);
34013401
return result;
34023402
}
3403+
// START META PATCH
3404+
int
3405+
_Ci_PyImport_FinishSinglePhaseExtension(
3406+
int *tstate, PyObject *mod, void *cached, PyObject *name, PyObject *modules)
3407+
{
3408+
return finish_singlephase_extension(tstate, mod, cached, name, modules);
3409+
}
3410+
3411+
PyObject *
3412+
_Ci_PyImport_CallInitFuncWithContext(const char* context, PyObject* (*initfunc)(void))
3413+
{
3414+
const char *oldcontext;
3415+
PyObject* mod;
3416+
oldcontext = _PyImport_SwapPackageContext(context);
3417+
mod = initfunc();
3418+
_PyImport_SwapPackageContext(oldcontext);
3419+
return mod;
3420+
}
34033421

3422+
// END META PATCH
34043423

34053424
/* Import a module without blocking
34063425
*

0 commit comments

Comments
 (0)