/* Execute compiled code */ /* XXX TO DO: XXX speed up searching for keywords by using a dictionary XXX document it! */ /* enable more aggressive intra-module optimizations, where available */#define PY_LOCAL_AGGRESSIVE #include "Python.h" #include "code.h"#include "frameobject.h"#include "eval.h"#include "opcode.h"#include "structmember.h" #include <ctype.h> #ifndef WITH_TSC #define READ_TIMESTAMP(var) #else typedef unsigned long long uint64; #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this section should work for GCC on any PowerPC platform, irrespective of OS. POWER? Who knows :-) */ #define READ_TIMESTAMP(var) ppc_getcounter(&var) static voidppc_getcounter(uint64 *v){ register unsigned long tbu, tb, tbu2; loop: asm volatile ("mftbu %0" : "=r" (tbu) ); asm volatile ("mftb %0" : "=r" (tb) ); asm volatile ("mftbu %0" : "=r" (tbu2)); if (__builtin_expect(tbu != tbu2, 0)) goto loop; /* The slightly peculiar way of writing the next lines is compiled better by GCC than any other way I tried. */ ((long*)(v))[0] = tbu; ((long*)(v))[1] = tb;} #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */ #define READ_TIMESTAMP(val) \ __asm__ __volatile__("rdtsc" : "=A" (val)) #endif void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1, uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1){ uint64 intr, inst, loop; PyThreadState *tstate = PyThreadState_Get(); if (!tstate->interp->tscdump) return; intr = intr1 - intr0; inst = inst1 - inst0 - intr; loop = loop1 - loop0 - intr; fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", opcode, ticked, inst, loop);} #endif /* Turn this on if your compiler chokes on the big switch: *//* #define CASE_TOO_BIG 1 */ #ifdef Py_DEBUG/* For debugging the interpreter: */#define LLTRACE 1 /* Low-level trace feature */#define CHECKEXC 1 /* Double-check exception checking */#endif typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); /* Forward declarations */#ifdef WITH_TSCstatic PyObject * call_function(PyObject ***, int, uint64*, uint64*);#elsestatic PyObject * call_function(PyObject ***, int);#endifstatic PyObject * fast_function(PyObject *, PyObject ***, int, int, int);static PyObject * do_call(PyObject *, PyObject ***, int, int);static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *);static PyObject * update_star_args(int, int, PyObject *, PyObject ***);static PyObject * load_args(PyObject ***, int);#define CALL_FLAG_VAR 1#define CALL_FLAG_KW 2 #ifdef LLTRACEstatic int lltrace;static int prtrace(PyObject *, char *);#endifstatic int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, int, PyObject *);static void call_trace_protected(Py_tracefunc, PyObject *, PyFrameObject *, int, PyObject *);static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);static int maybe_call_line_trace(Py_tracefunc, PyObject *, PyFrameObject *, int *, int *, int *); static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);static int assign_slice(PyObject *, PyObject *, PyObject *, PyObject *);static PyObject * cmp_outcome(int, PyObject *, PyObject *);static PyObject * import_from(PyObject *, PyObject *);static int import_all_from(PyObject *, PyObject *);static PyObject * build_class(PyObject *, PyObject *, PyObject *);static int exec_statement(PyFrameObject *, PyObject *, PyObject *, PyObject *);static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);static void reset_exc_info(PyThreadState *);static void format_exc_check_arg(PyObject *, char *, PyObject *);static PyObject * string_concatenate(PyObject *, PyObject *, PyFrameObject *, unsigned char *); #define NAME_ERROR_MSG \ "name '%.200s' is not defined"#define GLOBAL_NAME_ERROR_MSG \ "global name '%.200s' is not defined"#define UNBOUNDLOCAL_ERROR_MSG \ "local variable '%.200s' referenced before assignment"#define UNBOUNDFREE_ERROR_MSG \ "free variable '%.200s' referenced before assignment" \ " in enclosing scope" /* Dynamic execution profile */#ifdef DYNAMIC_EXECUTION_PROFILE#ifdef DXPAIRSstatic long dxpairs[257][256];#define dxp dxpairs[256]#elsestatic long dxp[256];#endif#endif /* Function call profile */#ifdef CALL_PROFILE#define PCALL_NUM 11static int pcall[PCALL_NUM]; #define PCALL_ALL 0#define PCALL_FUNCTION 1#define PCALL_FAST_FUNCTION 2#define PCALL_FASTER_FUNCTION 3#define PCALL_METHOD 4#define PCALL_BOUND_METHOD 5#define PCALL_CFUNCTION 6#define PCALL_TYPE 7#define PCALL_GENERATOR 8#define PCALL_OTHER 9#define PCALL_POP 10 /* Notes about the statistics PCALL_FAST stats FAST_FUNCTION means no argument tuple needs to be created. FASTER_FUNCTION means that the fast-path frame setup code is used. If there is a method call where the call can be optimized by changing the argument tuple and calling the function directly, it gets recorded twice. As a result, the relationship among the statistics appears to be PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION PCALL_METHOD > PCALL_BOUND_METHOD*/ #define PCALL(POS) pcall[POS]++ PyObject *PyEval_GetCallStats(PyObject *self){ return Py_BuildValue("iiiiiiiiiii", pcall[0], pcall[1], pcall[2], pcall[3], pcall[4], pcall[5], pcall[6], pcall[7], pcall[8], pcall[9], pcall[10]);}#else#define PCALL(O) PyObject *PyEval_GetCallStats(PyObject *self){ Py_INCREF(Py_None); return Py_None;}#endif #ifdef WITH_THREAD #ifdef HAVE_ERRNO_H#include <errno.h>#endif#include "pythread.h" static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */static long main_thread = 0; intPyEval_ThreadsInitialized(void){ return interpreter_lock != 0;} voidPyEval_InitThreads(void){ if (interpreter_lock) return; interpreter_lock = PyThread_allocate_lock(); PyThread_acquire_lock(interpreter_lock, 1); main_thread = PyThread_get_thread_ident();} voidPyEval_AcquireLock(void){ PyThread_acquire_lock(interpreter_lock, 1);} voidPyEval_ReleaseLock(void){ PyThread_release_lock(interpreter_lock);} voidPyEval_AcquireThread(PyThreadState *tstate){ if (tstate == NULL) Py_FatalError("PyEval_AcquireThread: NULL new thread state"); /* Check someone has called PyEval_InitThreads() to create the lock */ assert(interpreter_lock); PyThread_acquire_lock(interpreter_lock, 1); if (PyThreadState_Swap(tstate) != NULL) Py_FatalError( "PyEval_AcquireThread: non-NULL old thread state");} voidPyEval_ReleaseThread(PyThreadState *tstate){ if (tstate == NULL) Py_FatalError("PyEval_ReleaseThread: NULL thread state"); if (PyThreadState_Swap(NULL) != tstate) Py_FatalError("PyEval_ReleaseThread: wrong thread state"); PyThread_release_lock(interpreter_lock);} /* This function is called from PyOS_AfterFork to ensure that newly created child processes don't hold locks referring to threads which are not running in the child process. (This could also be done using pthread_atfork mechanism, at least for the pthreads implementation.) */ voidPyEval_ReInitThreads(void){ if (!interpreter_lock) return; /*XXX Can't use PyThread_free_lock here because it does too much error-checking. Doing this cleanly would require adding a new function to each thread_*.h. Instead, just create a new lock and waste a little bit of memory */ interpreter_lock = PyThread_allocate_lock(); PyThread_acquire_lock(interpreter_lock, 1); main_thread = PyThread_get_thread_ident();}#endif /* Functions save_thread and restore_thread are always defined so dynamically loaded modules needn't be compiled separately for use with and without threads: */ PyThreadState *PyEval_SaveThread(void){ PyThreadState *tstate = PyThreadState_Swap(NULL); if (tstate == NULL) Py_FatalError("PyEval_SaveThread: NULL tstate");#ifdef WITH_THREAD if (interpreter_lock) PyThread_release_lock(interpreter_lock);#endif return tstate;} voidPyEval_RestoreThread(PyThreadState *tstate){ if (tstate == NULL) Py_FatalError("PyEval_RestoreThread: NULL tstate");#ifdef WITH_THREAD if (interpreter_lock) { int err = errno; PyThread_acquire_lock(interpreter_lock, 1); errno = err; }#endif PyThreadState_Swap(tstate);} /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX signal handlers or Mac I/O completion routines) can schedule calls to a function to be called synchronously. The synchronous function is called with one void* argument. It should return 0 for success or -1 for failure -- failure should be accompanied by an exception. If registry succeeds, the registry function returns 0; if it fails (e.g. due to too many pending calls) it returns -1 (without setting an exception condition). Note that because registry may occur from within signal handlers, or other asynchronous events, calling malloc() is unsafe! #ifdef WITH_THREAD Any thread can schedule pending calls, but only the main thread will execute them.#endif XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE! There are two possible race conditions: (1) nested asynchronous registry calls; (2) registry calls made while pending calls are being processed. While (1) is very unlikely, (2) is a real possibility. The current code is safe against (2), but not against (1). The safety against (2) is derived from the fact that only one thread (the main thread) ever takes things out of the queue. XXX Darn! With the advent of thread state, we should have an array of pending calls per thread in the thread state! Later...*/ #define NPENDINGCALLS 32static struct { int (*func)(void *); void *arg;} pendingcalls[NPENDINGCALLS];static volatile int pendingfirst = 0;static volatile int pendinglast = 0;static volatile int things_to_do = 0; intPy_AddPendingCall(int (*func)(void *), void *arg){ static int busy = 0; int i, j; /* XXX Begin critical section */ /* XXX If you want this to be safe against nested XXX asynchronous calls, you'll have to work harder! */ if (busy) return -1; busy = 1; i = pendinglast; j = (i + 1) % NPENDINGCALLS; if (j == pendingfirst) { busy = 0; return -1; /* Queue full */ } pendingcalls[i].func = func; pendingcalls[i].arg = arg; pendinglast = j; _Py_Ticker = 0; things_to_do = 1; /* Signal main loop */ busy = 0; /* XXX End critical section */ return 0;} intPy_MakePendingCalls(void){ static int busy = 0;#ifdef WITH_THREAD if (main_thread && PyThread_get_thread_ident() != main_thread) return 0;#endif if (busy) return 0; busy = 1; things_to_do = 0; for (;;) { int i; int (*func)(void *); void *arg; i = pendingfirst; if (i == pendinglast) break; /* Queue empty */ func = pendingcalls[i].func; arg = pendingcalls[i].arg; pendingfirst = (i + 1) % NPENDINGCALLS; if (func(arg) < 0) { busy = 0; things_to_do = 1; /* We're not done yet */ return -1; } } busy = 0; return 0;} /* The interpreter's recursion limit */ #ifndef Py_DEFAULT_RECURSION_LIMIT#define Py_DEFAULT_RECURSION_LIMIT 1000#endifstatic int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; intPy_GetRecursionLimit(void){ return recursion_limit;} voidPy_SetRecursionLimit(int new_limit){ recursion_limit = new_limit; _Py_CheckRecursionLimit = recursion_limit;} /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() if the recursion_depth reaches _Py_CheckRecursionLimit. If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit to guarantee that _Py_CheckRecursiveCall() is regularly called. Without USE_STACKCHECK, there is no need for this. */int_Py_CheckRecursiveCall(char *where){ PyThreadState *tstate = PyThreadState_GET(); #ifdef USE_STACKCHECK if (PyOS_CheckStack()) { --tstate->recursion_depth; PyErr_SetString(PyExc_MemoryError, "Stack overflow"); return -1; }#endif if (tstate->recursion_depth > recursion_limit) { --tstate->recursion_depth; PyErr_Format(PyExc_RuntimeError, "maximum recursion depth exceeded%s", where); return -1; } _Py_CheckRecursionLimit = recursion_limit; return 0;} /* Status code for main loop (reason for stack unwind) */enum why_code { WHY_NOT = 0x0001, /* No error */ WHY_EXCEPTION = 0x0002, /* Exception occurred */ WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ WHY_RETURN = 0x0008, /* 'return' statement */ WHY_BREAK = 0x0010, /* 'break' statement */ WHY_CONTINUE = 0x0020, /* 'continue' statement */ WHY_YIELD = 0x0040 /* 'yield' operator */}; static enum why_code do_raise(PyObject *, PyObject *, PyObject *);static int unpack_iterable(PyObject *, int, PyObject **); /* for manipulating the thread switch and periodic "stuff" - used to be per thread, now just a pair o' globals */int _Py_CheckInterval = 100;volatile int _Py_Ticker = 100; PyObject *PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals){ /* XXX raise SystemError if globals is NULL */ return PyEval_EvalCodeEx(co, globals, locals, (PyObject **)NULL, 0, (PyObject **)NULL, 0, (PyObject **)NULL, 0, NULL);}Lines 1–500 of 4351. Load more
/* Execute compiled code */ /* XXX TO DO: XXX speed up searching for keywords by using a dictionary XXX document it! */ /* enable more aggressive intra-module optimizations, where available */ #define PY_LOCAL_AGGRESSIVE #include "Python.h" #include "code.h" #include "frameobject.h" #include "eval.h" #include "opcode.h" #include "structmember.h" #include <ctype.h> #ifndef WITH_TSC #define READ_TIMESTAMP(var) #else typedef unsigned long long uint64; #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this section should work for GCC on any PowerPC platform, irrespective of OS. POWER? Who knows :-) */ #define READ_TIMESTAMP(var) ppc_getcounter(&var) static void ppc_getcounter(uint64 *v) { register unsigned long tbu, tb, tbu2; loop: asm volatile ("mftbu %0" : "=r" (tbu) ); asm volatile ("mftb %0" : "=r" (tb) ); asm volatile ("mftbu %0" : "=r" (tbu2)); if (__builtin_expect(tbu != tbu2, 0)) goto loop; /* The slightly peculiar way of writing the next lines is compiled better by GCC than any other way I tried. */ ((long*)(v))[0] = tbu; ((long*)(v))[1] = tb; } #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */ #define READ_TIMESTAMP(val) \ __asm__ __volatile__("rdtsc" : "=A" (val)) #endif void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1, uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1) { uint64 intr, inst, loop; PyThreadState *tstate = PyThreadState_Get(); if (!tstate->interp->tscdump) return; intr = intr1 - intr0; inst = inst1 - inst0 - intr; loop = loop1 - loop0 - intr; fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n", opcode, ticked, inst, loop); } #endif /* Turn this on if your compiler chokes on the big switch: */ /* #define CASE_TOO_BIG 1 */ #ifdef Py_DEBUG /* For debugging the interpreter: */ #define LLTRACE 1 /* Low-level trace feature */ #define CHECKEXC 1 /* Double-check exception checking */ #endif typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); /* Forward declarations */ #ifdef WITH_TSC static PyObject * call_function(PyObject ***, int, uint64*, uint64*); #else static PyObject * call_function(PyObject ***, int); #endif static PyObject * fast_function(PyObject *, PyObject ***, int, int, int); static PyObject * do_call(PyObject *, PyObject ***, int, int); static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int); static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *); static PyObject * update_star_args(int, int, PyObject *, PyObject ***); static PyObject * load_args(PyObject ***, int); #define CALL_FLAG_VAR 1 #define CALL_FLAG_KW 2 #ifdef LLTRACE static int lltrace; static int prtrace(PyObject *, char *); #endif static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, int, PyObject *); static void call_trace_protected(Py_tracefunc, PyObject *, PyFrameObject *, int, PyObject *); static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); static int maybe_call_line_trace(Py_tracefunc, PyObject *, PyFrameObject *, int *, int *, int *); static PyObject * apply_slice(PyObject *, PyObject *, PyObject *); static int assign_slice(PyObject *, PyObject *, PyObject *, PyObject *); static PyObject * cmp_outcome(int, PyObject *, PyObject *); static PyObject * import_from(PyObject *, PyObject *); static int import_all_from(PyObject *, PyObject *); static PyObject * build_class(PyObject *, PyObject *, PyObject *); static int exec_statement(PyFrameObject *, PyObject *, PyObject *, PyObject *); static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); static void reset_exc_info(PyThreadState *); static void format_exc_check_arg(PyObject *, char *, PyObject *); static PyObject * string_concatenate(PyObject *, PyObject *, PyFrameObject *, unsigned char *); #define NAME_ERROR_MSG \ "name '%.200s' is not defined" #define GLOBAL_NAME_ERROR_MSG \ "global name '%.200s' is not defined" #define UNBOUNDLOCAL_ERROR_MSG \ "local variable '%.200s' referenced before assignment" #define UNBOUNDFREE_ERROR_MSG \ "free variable '%.200s' referenced before assignment" \ " in enclosing scope" /* Dynamic execution profile */ #ifdef DYNAMIC_EXECUTION_PROFILE #ifdef DXPAIRS static long dxpairs[257][256]; #define dxp dxpairs[256] #else static long dxp[256]; #endif #endif /* Function call profile */ #ifdef CALL_PROFILE #define PCALL_NUM 11 static int pcall[PCALL_NUM]; #define PCALL_ALL 0 #define PCALL_FUNCTION 1 #define PCALL_FAST_FUNCTION 2 #define PCALL_FASTER_FUNCTION 3 #define PCALL_METHOD 4 #define PCALL_BOUND_METHOD 5 #define PCALL_CFUNCTION 6 #define PCALL_TYPE 7 #define PCALL_GENERATOR 8 #define PCALL_OTHER 9 #define PCALL_POP 10 /* Notes about the statistics PCALL_FAST stats FAST_FUNCTION means no argument tuple needs to be created. FASTER_FUNCTION means that the fast-path frame setup code is used. If there is a method call where the call can be optimized by changing the argument tuple and calling the function directly, it gets recorded twice. As a result, the relationship among the statistics appears to be PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD + PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION PCALL_METHOD > PCALL_BOUND_METHOD */ #define PCALL(POS) pcall[POS]++ PyObject * PyEval_GetCallStats(PyObject *self) { return Py_BuildValue("iiiiiiiiiii", pcall[0], pcall[1], pcall[2], pcall[3], pcall[4], pcall[5], pcall[6], pcall[7], pcall[8], pcall[9], pcall[10]); } #else #define PCALL(O) PyObject * PyEval_GetCallStats(PyObject *self) { Py_INCREF(Py_None); return Py_None; } #endif #ifdef WITH_THREAD #ifdef HAVE_ERRNO_H #include <errno.h> #endif #include "pythread.h" static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */ static long main_thread = 0; int PyEval_ThreadsInitialized(void) { return interpreter_lock != 0; } void PyEval_InitThreads(void) { if (interpreter_lock) return; interpreter_lock = PyThread_allocate_lock(); PyThread_acquire_lock(interpreter_lock, 1); main_thread = PyThread_get_thread_ident(); } void PyEval_AcquireLock(void) { PyThread_acquire_lock(interpreter_lock, 1); } void PyEval_ReleaseLock(void) { PyThread_release_lock(interpreter_lock); } void PyEval_AcquireThread(PyThreadState *tstate) { if (tstate == NULL) Py_FatalError("PyEval_AcquireThread: NULL new thread state"); /* Check someone has called PyEval_InitThreads() to create the lock */ assert(interpreter_lock); PyThread_acquire_lock(interpreter_lock, 1); if (PyThreadState_Swap(tstate) != NULL) Py_FatalError( "PyEval_AcquireThread: non-NULL old thread state"); } void PyEval_ReleaseThread(PyThreadState *tstate) { if (tstate == NULL) Py_FatalError("PyEval_ReleaseThread: NULL thread state"); if (PyThreadState_Swap(NULL) != tstate) Py_FatalError("PyEval_ReleaseThread: wrong thread state"); PyThread_release_lock(interpreter_lock); } /* This function is called from PyOS_AfterFork to ensure that newly created child processes don't hold locks referring to threads which are not running in the child process. (This could also be done using pthread_atfork mechanism, at least for the pthreads implementation.) */ void PyEval_ReInitThreads(void) { if (!interpreter_lock) return; /*XXX Can't use PyThread_free_lock here because it does too much error-checking. Doing this cleanly would require adding a new function to each thread_*.h. Instead, just create a new lock and waste a little bit of memory */ interpreter_lock = PyThread_allocate_lock(); PyThread_acquire_lock(interpreter_lock, 1); main_thread = PyThread_get_thread_ident(); } #endif /* Functions save_thread and restore_thread are always defined so dynamically loaded modules needn't be compiled separately for use with and without threads: */ PyThreadState * PyEval_SaveThread(void) { PyThreadState *tstate = PyThreadState_Swap(NULL); if (tstate == NULL) Py_FatalError("PyEval_SaveThread: NULL tstate"); #ifdef WITH_THREAD if (interpreter_lock) PyThread_release_lock(interpreter_lock); #endif return tstate; } void PyEval_RestoreThread(PyThreadState *tstate) { if (tstate == NULL) Py_FatalError("PyEval_RestoreThread: NULL tstate"); #ifdef WITH_THREAD if (interpreter_lock) { int err = errno; PyThread_acquire_lock(interpreter_lock, 1); errno = err; } #endif PyThreadState_Swap(tstate); } /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX signal handlers or Mac I/O completion routines) can schedule calls to a function to be called synchronously. The synchronous function is called with one void* argument. It should return 0 for success or -1 for failure -- failure should be accompanied by an exception. If registry succeeds, the registry function returns 0; if it fails (e.g. due to too many pending calls) it returns -1 (without setting an exception condition). Note that because registry may occur from within signal handlers, or other asynchronous events, calling malloc() is unsafe! #ifdef WITH_THREAD Any thread can schedule pending calls, but only the main thread will execute them. #endif XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE! There are two possible race conditions: (1) nested asynchronous registry calls; (2) registry calls made while pending calls are being processed. While (1) is very unlikely, (2) is a real possibility. The current code is safe against (2), but not against (1). The safety against (2) is derived from the fact that only one thread (the main thread) ever takes things out of the queue. XXX Darn! With the advent of thread state, we should have an array of pending calls per thread in the thread state! Later... */ #define NPENDINGCALLS 32 static struct { int (*func)(void *); void *arg; } pendingcalls[NPENDINGCALLS]; static volatile int pendingfirst = 0; static volatile int pendinglast = 0; static volatile int things_to_do = 0; int Py_AddPendingCall(int (*func)(void *), void *arg) { static int busy = 0; int i, j; /* XXX Begin critical section */ /* XXX If you want this to be safe against nested XXX asynchronous calls, you'll have to work harder! */ if (busy) return -1; busy = 1; i = pendinglast; j = (i + 1) % NPENDINGCALLS; if (j == pendingfirst) { busy = 0; return -1; /* Queue full */ } pendingcalls[i].func = func; pendingcalls[i].arg = arg; pendinglast = j; _Py_Ticker = 0; things_to_do = 1; /* Signal main loop */ busy = 0; /* XXX End critical section */ return 0; } int Py_MakePendingCalls(void) { static int busy = 0; #ifdef WITH_THREAD if (main_thread && PyThread_get_thread_ident() != main_thread) return 0; #endif if (busy) return 0; busy = 1; things_to_do = 0; for (;;) { int i; int (*func)(void *); void *arg; i = pendingfirst; if (i == pendinglast) break; /* Queue empty */ func = pendingcalls[i].func; arg = pendingcalls[i].arg; pendingfirst = (i + 1) % NPENDINGCALLS; if (func(arg) < 0) { busy = 0; things_to_do = 1; /* We're not done yet */ return -1; } } busy = 0; return 0; } /* The interpreter's recursion limit */ #ifndef Py_DEFAULT_RECURSION_LIMIT #define Py_DEFAULT_RECURSION_LIMIT 1000 #endif static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT; int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT; int Py_GetRecursionLimit(void) { return recursion_limit; } void Py_SetRecursionLimit(int new_limit) { recursion_limit = new_limit; _Py_CheckRecursionLimit = recursion_limit; } /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall() if the recursion_depth reaches _Py_CheckRecursionLimit. If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit to guarantee that _Py_CheckRecursiveCall() is regularly called. Without USE_STACKCHECK, there is no need for this. */ int _Py_CheckRecursiveCall(char *where) { PyThreadState *tstate = PyThreadState_GET(); #ifdef USE_STACKCHECK if (PyOS_CheckStack()) { --tstate->recursion_depth; PyErr_SetString(PyExc_MemoryError, "Stack overflow"); return -1; } #endif if (tstate->recursion_depth > recursion_limit) { --tstate->recursion_depth; PyErr_Format(PyExc_RuntimeError, "maximum recursion depth exceeded%s", where); return -1; } _Py_CheckRecursionLimit = recursion_limit; return 0; } /* Status code for main loop (reason for stack unwind) */ enum why_code { WHY_NOT = 0x0001, /* No error */ WHY_EXCEPTION = 0x0002, /* Exception occurred */ WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */ WHY_RETURN = 0x0008, /* 'return' statement */ WHY_BREAK = 0x0010, /* 'break' statement */ WHY_CONTINUE = 0x0020, /* 'continue' statement */ WHY_YIELD = 0x0040 /* 'yield' operator */ }; static enum why_code do_raise(PyObject *, PyObject *, PyObject *); static int unpack_iterable(PyObject *, int, PyObject **); /* for manipulating the thread switch and periodic "stuff" - used to be per thread, now just a pair o' globals */ int _Py_CheckInterval = 100; volatile int _Py_Ticker = 100; PyObject * PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) { /* XXX raise SystemError if globals is NULL */ return PyEval_EvalCodeEx(co, globals, locals, (PyObject **)NULL, 0, (PyObject **)NULL, 0, (PyObject **)NULL, 0, NULL); }