/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */ /* This file is adapted from the mechanism used in JEP. and shows the subinterpreter/thread problem If the Py_NewInterpreter() is commented out the program runs without error. Running under 2.3.4 we see: 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] Creating file from main thread... Done Creating file from sub thread... Done Running unser 2.3.5 we see: 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] Creating file from main thread... Done Creating file from sub thread... Exception in thread Thread-1:Traceback (most recent call last): File "C:\Python23\Lib\threading.py", line 442, in __bootstrap File "", line 3, in run IOError: file() constructor not accessible in restricted mode Done ***************************************************************************** */ #ifdef WIN32 # include "winconfig.h" #endif #if HAVE_CONFIG_H # include #endif #if HAVE_UNISTD_H # include # include #endif #if STDC_HEADERS # include #endif #include int main(int argc, char *argv[]) { Py_Initialize(); Py_NewInterpreter(); // Comment this out if you don't run in sub-interpreter PyRun_SimpleString("import sys"); PyRun_SimpleString("print sys.version"); PyRun_SimpleString("import threading"); PyRun_SimpleString("import thread"); PyRun_SimpleString("class FileCreationThread(threading.Thread):\n\tdef run(self):\n\t\taFile = file('c:\\\\file2.txt','w')\n\t\taFile.write('Second file')\n"); PyRun_SimpleString("def createFromThread():\n\tmyThread = FileCreationThread()\n\tmyThread.start()\n\tmyThread.join()\n\n"); PyRun_SimpleString("print 'Creating file from main thread...'"); PyRun_SimpleString("aFile = file('C:\\\\file1.txt','w')"); PyRun_SimpleString("aFile.write('First file')"); PyRun_SimpleString("print 'Done'"); PyRun_SimpleString("print 'Creating file from sub thread...'"); PyRun_SimpleString("createFromThread()"); PyRun_SimpleString("print 'Done'"); Py_Finalize(); return 0; }