Skip to content

Commit 993c3cc

Browse files
gh-76785: Add More Tests to test_interpreters.test_api (gh-117662)
In addition to the increase test coverage, this is a precursor to sorting out how we handle interpreters created directly via the C-API.
1 parent 0cc71bd commit 993c3cc

18 files changed

+2012
-418
lines changed

Include/internal/pycore_crossinterp.h

+20
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ typedef struct _excinfo {
217217
const char *errdisplay;
218218
} _PyXI_excinfo;
219219

220+
PyAPI_FUNC(int) _PyXI_InitExcInfo(_PyXI_excinfo *info, PyObject *exc);
221+
PyAPI_FUNC(PyObject *) _PyXI_FormatExcInfo(_PyXI_excinfo *info);
222+
PyAPI_FUNC(PyObject *) _PyXI_ExcInfoAsObject(_PyXI_excinfo *info);
223+
PyAPI_FUNC(void) _PyXI_ClearExcInfo(_PyXI_excinfo *info);
224+
220225

221226
typedef enum error_code {
222227
_PyXI_ERR_NO_ERROR = 0,
@@ -313,6 +318,21 @@ PyAPI_FUNC(PyObject *) _PyXI_ApplyCapturedException(_PyXI_session *session);
313318
PyAPI_FUNC(int) _PyXI_HasCapturedException(_PyXI_session *session);
314319

315320

321+
/*************/
322+
/* other API */
323+
/*************/
324+
325+
// Export for _testinternalcapi shared extension
326+
PyAPI_FUNC(PyInterpreterState *) _PyXI_NewInterpreter(
327+
PyInterpreterConfig *config,
328+
PyThreadState **p_tstate,
329+
PyThreadState **p_save_tstate);
330+
PyAPI_FUNC(void) _PyXI_EndInterpreter(
331+
PyInterpreterState *interp,
332+
PyThreadState *tstate,
333+
PyThreadState **p_save_tstate);
334+
335+
316336
#ifdef __cplusplus
317337
}
318338
#endif

Include/internal/pycore_interp.h

+16
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,22 @@ struct _is {
103103
int requires_idref;
104104
PyThread_type_lock id_mutex;
105105

106+
#define _PyInterpreterState_WHENCE_NOTSET -1
107+
#define _PyInterpreterState_WHENCE_UNKNOWN 0
108+
#define _PyInterpreterState_WHENCE_RUNTIME 1
109+
#define _PyInterpreterState_WHENCE_LEGACY_CAPI 2
110+
#define _PyInterpreterState_WHENCE_CAPI 3
111+
#define _PyInterpreterState_WHENCE_XI 4
112+
#define _PyInterpreterState_WHENCE_MAX 4
113+
long _whence;
114+
106115
/* Has been initialized to a safe state.
107116
108117
In order to be effective, this must be set to 0 during or right
109118
after allocation. */
110119
int _initialized;
120+
/* Has been fully initialized via pylifecycle.c. */
121+
int _ready;
111122
int finalizing;
112123

113124
uintptr_t last_restart_version;
@@ -305,6 +316,11 @@ PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
305316
PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
306317
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
307318

319+
PyAPI_FUNC(long) _PyInterpreterState_GetWhence(PyInterpreterState *interp);
320+
extern void _PyInterpreterState_SetWhence(
321+
PyInterpreterState *interp,
322+
long whence);
323+
308324
extern const PyConfig* _PyInterpreterState_GetConfig(PyInterpreterState *interp);
309325

310326
// Get a copy of the current interpreter configuration.

Include/internal/pycore_pystate.h

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ _Py_IsMainInterpreterFinalizing(PyInterpreterState *interp)
7777
interp == &_PyRuntime._main_interpreter);
7878
}
7979

80+
// Export for _xxsubinterpreters module.
81+
PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *);
82+
8083
// Export for _xxsubinterpreters module.
8184
PyAPI_FUNC(int) _PyInterpreterState_SetRunningMain(PyInterpreterState *);
8285
PyAPI_FUNC(void) _PyInterpreterState_SetNotRunningMain(PyInterpreterState *);

Include/internal/pycore_runtime_init.h

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ extern PyTypeObject _PyExc_MemoryError;
162162
#define _PyInterpreterState_INIT(INTERP) \
163163
{ \
164164
.id_refcount = -1, \
165+
._whence = _PyInterpreterState_WHENCE_NOTSET, \
165166
.imports = IMPORTS_INIT, \
166167
.ceval = { \
167168
.recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \

Lib/test/support/interpreters/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,19 @@ def create():
7979

8080
def list_all():
8181
"""Return all existing interpreters."""
82-
return [Interpreter(id) for id in _interpreters.list_all()]
82+
return [Interpreter(id)
83+
for id, in _interpreters.list_all()]
8384

8485

8586
def get_current():
8687
"""Return the currently running interpreter."""
87-
id = _interpreters.get_current()
88+
id, = _interpreters.get_current()
8889
return Interpreter(id)
8990

9091

9192
def get_main():
9293
"""Return the main interpreter."""
93-
id = _interpreters.get_main()
94+
id, = _interpreters.get_main()
9495
return Interpreter(id)
9596

9697

0 commit comments

Comments
 (0)