Commit 9b30997a authored by Marius Wachtler's avatar Marius Wachtler

call threading._after_fork and threading._shutdown, return real thread id

parent 75bd1059
......@@ -1359,7 +1359,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
*/
#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o, char *key) PYSTON_NOEXCEPT;
PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o, const char *key) PYSTON_NOEXCEPT;
/*
Return element of o corresponding to the object, key, or NULL
......@@ -1367,7 +1367,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
o[key].
*/
PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, char *key,
PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
PyObject *value) PYSTON_NOEXCEPT;
/*
......
......@@ -1804,7 +1804,7 @@ extern "C" int PyMapping_HasKey(PyObject* o, PyObject* key) noexcept {
return 0;
}
extern "C" PyObject* PyMapping_GetItemString(PyObject* o, char* key) noexcept {
extern "C" PyObject* PyMapping_GetItemString(PyObject* o, const char* key) noexcept {
PyObject* okey, *r;
if (key == NULL)
......@@ -1818,7 +1818,7 @@ extern "C" PyObject* PyMapping_GetItemString(PyObject* o, char* key) noexcept {
return r;
}
extern "C" int PyMapping_SetItemString(PyObject* o, char* key, PyObject* value) noexcept {
extern "C" int PyMapping_SetItemString(PyObject* o, const char* key, PyObject* value) noexcept {
PyObject* okey;
int r;
......
......@@ -41,7 +41,10 @@ public:
static BoxedString* __repr__(BoxedCApiFunction* self) {
assert(self->cls == capifunc_cls);
return boxString(self->method_def->ml_name);
if (self->passthrough == NULL)
return (BoxedString*)PyString_FromFormat("<built-in function %s>", self->method_def->ml_name);
return (BoxedString*)PyString_FromFormat("<built-in method %s of %s object at %p>", self->method_def->ml_name,
self->passthrough->cls->tp_name, self->passthrough);
}
static Box* __call__(BoxedCApiFunction* self, BoxedTuple* varargs, BoxedDict* kwargs);
......
......@@ -522,11 +522,34 @@ void registerMainThread() {
assert(!PyErr_Occurred());
}
/* Wait until threading._shutdown completes, provided
the threading module was imported in the first place.
The shutdown routine will wait until all non-daemon
"threading" threads have completed. */
static void wait_for_thread_shutdown(void) noexcept {
#ifdef WITH_THREAD
PyObject* result;
PyThreadState* tstate = PyThreadState_GET();
PyObject* threading = PyMapping_GetItemString(getSysModulesDict(), "threading");
if (threading == NULL) {
/* threading not imported */
PyErr_Clear();
return;
}
result = PyObject_CallMethod(threading, "_shutdown", "");
if (result == NULL)
PyErr_WriteUnraisable(threading);
else
Py_DECREF(result);
Py_DECREF(threading);
#endif
}
void finishMainThread() {
assert(current_internal_thread_state);
current_internal_thread_state->assertNoGenerators();
// TODO maybe this is the place to wait for non-daemon threads?
wait_for_thread_shutdown();
}
bool isMainThread() {
......@@ -601,6 +624,22 @@ extern "C" void PyEval_ReInitThreads() noexcept {
threads_waiting_on_gil = 0;
PerThreadSetBase::runAllForkHandlers();
/* Update the threading module with the new state.
*/
Box* threading = PyMapping_GetItemString(getSysModulesDict(), "threading");
if (threading == NULL) {
/* threading not imported */
PyErr_Clear();
return;
}
Box* result = PyObject_CallMethod(threading, "_after_fork", NULL);
if (result == NULL)
PyErr_WriteUnraisable(threading);
else
Py_DECREF(result);
Py_DECREF(threading);
}
void acquireGLWrite() {
......
......@@ -2540,7 +2540,7 @@ void setupBuiltins() {
{ "reload", builtin_reload, METH_O, reload_doc },
};
for (auto& md : builtin_methods) {
builtins_module->giveAttr(md.ml_name, new BoxedCApiFunction(&md, builtins_module, boxString("__builtin__")));
builtins_module->giveAttr(md.ml_name, new BoxedCApiFunction(&md, NULL, boxString("__builtin__")));
}
}
}
......@@ -67,9 +67,6 @@ Box* sysExcClear() {
static Box* sysExit(Box* arg) {
assert(arg);
Box* exc = runtimeCall(SystemExit, ArgPassSpec(1), arg, NULL, NULL, NULL, NULL);
// TODO this should be handled by the SystemExit constructor
exc->giveAttr("code", arg);
raiseExc(exc);
}
......@@ -728,7 +725,7 @@ void setupSys() {
sys_flags_cls->freeze();
for (auto& md : sys_methods) {
sys_module->giveAttr(md.ml_name, new BoxedCApiFunction(&md, sys_module, boxString("sys")));
sys_module->giveAttr(md.ml_name, new BoxedCApiFunction(&md, NULL, boxString("sys")));
}
sys_module->giveAttr("__displayhook__", sys_module->getattr(internStringMortal("displayhook")));
......
......@@ -90,7 +90,7 @@ static void* thread_start(Box* target, Box* varargs, Box* kwargs) {
// TODO this should take kwargs, which defaults to empty
Box* startNewThread(Box* target, Box* args, Box* kw) {
intptr_t thread_id = start_thread(&thread_start, target, args, kw);
return boxInt(thread_id ^ 0x12345678901L);
return boxInt(thread_id);
}
#define CHECK_STATUS(name) \
......
print compile
c = compile("a", "test.py", "eval")
print type(c), c.co_filename, c.co_name
......
import sys
import os.path
print sys.excepthook, sys.displayhook
print sys.version[:3]
print os.path.exists(sys.executable)
print sys.copyright[-200:]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment