Skip to content

Commit 29e46a9

Browse files
committed
Mass checkin (more to follow for other directories).
Introduce truly separate (sub)interpreter objects. For now, these must be used by separate threads, created from C. See Demo/pysvr for an example of how to use this. This also rationalizes Python's initialization and finalization behavior: Py_Initialize() -- initialize the whole interpreter Py_Finalize() -- finalize the whole interpreter tstate = Py_NewInterpreter() -- create a new (sub)interpreter Py_EndInterpreter(tstate) -- delete a new (sub)interpreter There are also new interfaces relating to threads and the interpreter lock, which can be used to create new threads, and sometimes have to be used to manipulate the interpreter lock when creating or deleting sub-interpreters. These are only defined when WITH_THREAD is defined: PyEval_AcquireLock() -- acquire the interpreter lock PyEval_ReleaseLock() -- release the interpreter lock PyEval_AcquireThread(tstate) -- acquire the lock and make the thread current PyEval_ReleaseThread(tstate) -- release the lock and make NULL current Other administrative changes: - The header file bltinmodule.h is deleted. - The init functions for Import, Sys and Builtin are now internal and declared in pythonrun.h. - Py_Setup() and Py_Cleanup() are no longer declared. - The interpreter state and thread state structures are now linked together in a chain (the chain of interpreters is a static variable in pythonrun.c). - Some members of the interpreter and thread structures have new, shorter, more consistent, names. - Added declarations for _PyImport_{Find,Fixup}Extension() to import.h.
1 parent 7a2d611 commit 29e46a9

File tree

7 files changed

+27
-98
lines changed

7 files changed

+27
-98
lines changed

Include/Python.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ PERFORMANCE OF THIS SOFTWARE.
9595
#include "sysmodule.h"
9696
#include "intrcheck.h"
9797
#include "import.h"
98-
#include "bltinmodule.h"
9998

10099
#include "abstract.h"
101100

Include/bltinmodule.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

Include/ceval.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ extern void PyEval_RestoreThread Py_PROTO((PyThreadState *));
105105
#ifdef WITH_THREAD
106106

107107
extern void PyEval_InitThreads Py_PROTO((void));
108+
extern void PyEval_AcquireLock Py_PROTO((void));
109+
extern void PyEval_ReleaseLock Py_PROTO((void));
108110
extern void PyEval_AcquireThread Py_PROTO((PyThreadState *tstate));
109111
extern void PyEval_ReleaseThread Py_PROTO((PyThreadState *tstate));
110112

Include/import.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ PERFORMANCE OF THIS SOFTWARE.
3737

3838
/* Module definition and import interface */
3939

40-
void PyImport_Init Py_PROTO((void));
4140
long PyImport_GetMagicNumber Py_PROTO((void));
4241
PyObject *PyImport_ExecCodeModule Py_PROTO((char *name, PyObject *co));
4342
PyObject *PyImport_GetModuleDict Py_PROTO((void));
@@ -47,6 +46,9 @@ PyObject *PyImport_ReloadModule Py_PROTO((PyObject *m));
4746
void PyImport_Cleanup Py_PROTO((void));
4847
int PyImport_ImportFrozenModule Py_PROTO((char *));
4948

49+
extern PyObject *_PyImport_FindExtension Py_PROTO((char *, char *));
50+
extern PyObject *_PyImport_FixupExtension Py_PROTO((char *, char *));
51+
5052
struct _inittab {
5153
char *name;
5254
void (*initfunc)();

Include/pystate.h

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ PERFORMANCE OF THIS SOFTWARE.
4242

4343
#define NEXITFUNCS 32
4444

45+
struct _ts; /* Forward */
46+
struct _is; /* Forward */
47+
4548
typedef struct _is {
4649

47-
PyObject *import_modules;
48-
PyObject *sysdict;
50+
struct _is *next;
51+
struct _ts *tstate_head;
4952

50-
int nthreads;
53+
PyObject *modules;
54+
PyObject *sysdict;
55+
PyObject *builtins;
5156

52-
void (*exitfuncs[NEXITFUNCS])();
53-
int nexitfuncs;
57+
int checkinterval;
5458

5559
} PyInterpreterState;
5660

@@ -61,7 +65,8 @@ struct _frame; /* Avoid including frameobject.h */
6165

6266
typedef struct _ts {
6367

64-
PyInterpreterState *interpreter_state;
68+
struct _ts *next;
69+
PyInterpreterState *interp;
6570

6671
struct _frame *frame;
6772
int recursion_depth;
@@ -70,7 +75,6 @@ typedef struct _ts {
7075

7176
PyObject *sys_profilefunc;
7277
PyObject *sys_tracefunc;
73-
int sys_checkinterval;
7478

7579
PyObject *curexc_type;
7680
PyObject *curexc_value;
@@ -80,54 +84,22 @@ typedef struct _ts {
8084
PyObject *exc_value;
8185
PyObject *exc_traceback;
8286

83-
/* XXX Other state that should be here:
84-
- signal handlers
85-
- low-level "pending calls"
86-
Problem with both is that they may be referenced from
87-
interrupt handlers where there is no clear concept of a
88-
"current thread"???
89-
*/
87+
/* XXX signal handlers should also be here */
9088

9189
} PyThreadState;
9290

9391

9492
PyInterpreterState *PyInterpreterState_New Py_PROTO((void));
93+
void PyInterpreterState_Clear Py_PROTO((PyInterpreterState *));
9594
void PyInterpreterState_Delete Py_PROTO((PyInterpreterState *));
9695

9796
PyThreadState *PyThreadState_New Py_PROTO((PyInterpreterState *));
97+
void PyThreadState_Clear Py_PROTO((PyThreadState *));
9898
void PyThreadState_Delete Py_PROTO((PyThreadState *));
9999

100100
PyThreadState *PyThreadState_Get Py_PROTO((void));
101101
PyThreadState *PyThreadState_Swap Py_PROTO((PyThreadState *));
102102

103-
/* Some background.
104-
105-
There are lots of issues here.
106-
107-
First, we can build Python without threads, with threads, or (when
108-
Greg Stein's mods are out of beta, on some platforms) with free
109-
threading.
110-
111-
Next, assuming some form of threading is used, there can be several
112-
kinds of threads. Python code can create threads with the thread
113-
module. C code can create threads with the interface defined in
114-
python's "thread.h". Or C code can create threads directly with
115-
the OS threads interface (e.g. Solaris threads, SGI threads or
116-
pthreads, whatever is being used, as long as it's the same that
117-
Python is configured for).
118-
119-
Next, let's discuss sharing of interpreter state between threads.
120-
The exception state (sys.exc_* currently) should never be shared
121-
between threads, because it is stack frame specific. The contents
122-
of the sys module, in particular sys.modules and sys.path, are
123-
generally shared between threads. But occasionally it is useful to
124-
have separate module collections, e.g. when threads originate in C
125-
code and are used to execute unrelated Python scripts.
126-
(Traditionally, one would use separate processes for this, but
127-
there are lots of reasons why threads are attractive.)
128-
129-
*/
130-
131103
#ifdef __cplusplus
132104
}
133105
#endif

Include/pythonrun.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ PERFORMANCE OF THIS SOFTWARE.
4040
void Py_SetProgramName Py_PROTO((char *));
4141
char *Py_GetProgramName Py_PROTO((void));
4242

43-
void Py_Setup Py_PROTO((void));
4443
void Py_Initialize Py_PROTO((void));
44+
PyThreadState *Py_NewInterpreter Py_PROTO((void));
45+
void Py_EndInterpreter Py_PROTO((PyThreadState *));
4546

4647
int PyRun_AnyFile Py_PROTO((FILE *, char *));
4748

@@ -64,11 +65,6 @@ int Py_AtExit Py_PROTO((void (*func) Py_PROTO((void))));
6465

6566
void Py_Exit Py_PROTO((int));
6667

67-
void Py_Cleanup Py_PROTO((void));
68-
69-
void PyImport_Init Py_PROTO((void));
70-
void PyBuiltin_Init Py_PROTO((void));
71-
7268
int Py_FdIsInteractive Py_PROTO((FILE *, char *));
7369

7470
/* In getpath.c */
@@ -84,6 +80,12 @@ const char *Py_GetCopyright Py_PROTO((void));
8480
const char *Py_GetCompiler Py_PROTO((void));
8581
const char *Py_GetBuildInfo Py_PROTO((void));
8682

83+
/* Internal -- various one-time initializations */
84+
85+
PyObject *_PyBuiltin_Init Py_PROTO((void));
86+
PyObject *_PySys_Init Py_PROTO((void));
87+
void _PyImport_Init Py_PROTO((void));
88+
8789
#ifdef __cplusplus
8890
}
8991
#endif

Include/sysmodule.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ PERFORMANCE OF THIS SOFTWARE.
4040
PyObject *PySys_GetObject Py_PROTO((char *));
4141
int PySys_SetObject Py_PROTO((char *, PyObject *));
4242
FILE *PySys_GetFile Py_PROTO((char *, FILE *));
43-
void PySys_Init Py_PROTO((void));
4443
void PySys_SetArgv Py_PROTO((int, char **));
4544
void PySys_SetPath Py_PROTO((char *));
4645

0 commit comments

Comments
 (0)