@@ -192,13 +192,8 @@ static PyObject *the_builtins;
192192static void
193193init_python ()
194194{
195- if (the_interp )
196- return ;
197- Py_Initialize (); /* Initialize the interpreter */
198- the_builtins = PyEval_GetBuiltins (); /* Get __builtins__ */
199195 PyEval_InitThreads (); /* Create and acquire the interpreter lock */
200- the_tstate = PyEval_SaveThread (); /* Release lock & get thread state */
201- the_interp = the_tstate -> interpreter_state ; /* Get interp state */
196+ PyEval_ReleaseLock (); /* Release the lock */
202197}
203198
204199static void *
@@ -250,25 +245,25 @@ run_interpreter(FILE *input, FILE *output)
250245{
251246 PyThreadState * tstate ;
252247 PyObject * new_stdin , * new_stdout ;
253- PyObject * old_stdin , * old_stdout , * old_stderr ;
254- PyObject * globals ;
248+ PyObject * mainmod , * globals ;
255249 char buffer [1000 ];
256250 char * p , * q ;
257251 int n , end ;
258252
259- tstate = PyThreadState_New (the_interp );
260- PyEval_AcquireThread (tstate );
253+ PyEval_AcquireLock ();
254+ tstate = Py_NewInterpreter ();
255+ if (tstate == NULL ) {
256+ fprintf (output , "Sorry -- can't create an interpreter\n" );
257+ return ;
258+ }
261259
262- globals = PyDict_New ();
263- PyDict_SetItemString (globals , "__builtins__" , the_builtins );
260+ mainmod = PyImport_AddModule ("__main__" );
261+ globals = PyModule_GetDict (mainmod );
262+ Py_INCREF (globals );
264263
265264 new_stdin = PyFile_FromFile (input , "<socket-in>" , "r" , NULL );
266265 new_stdout = PyFile_FromFile (output , "<socket-out>" , "w" , NULL );
267266
268- old_stdin = PySys_GetObject ("stdin" );
269- old_stdout = PySys_GetObject ("stdout" );
270- old_stderr = PySys_GetObject ("stderr" );
271-
272267 for (n = 1 ; !PyErr_Occurred (); n ++ ) {
273268 Py_BEGIN_ALLOW_THREADS
274269 fprintf (output , "%d > ", n );
@@ -299,10 +294,6 @@ run_interpreter(FILE *input, FILE *output)
299294 if (end < 0 )
300295 PyErr_Print ();
301296
302- PySys_SetObject ("stdin" , old_stdin );
303- PySys_SetObject ("stdout" , old_stdout );
304- PySys_SetObject ("stderr" , old_stderr );
305-
306297 if (end )
307298 break ;
308299 }
@@ -311,8 +302,8 @@ run_interpreter(FILE *input, FILE *output)
311302 Py_XDECREF (new_stdin );
312303 Py_XDECREF (new_stdout );
313304
314- PyEval_ReleaseThread (tstate );
315- PyThreadState_Delete ( tstate );
305+ Py_EndInterpreter (tstate );
306+ PyEval_ReleaseLock ( );
316307
317308 fprintf (output , "Goodbye!\n" );
318309}
@@ -321,6 +312,9 @@ static int
321312run_command (char * buffer , PyObject * globals )
322313{
323314 PyObject * m , * d , * v ;
315+ fprintf (stderr , "run_command: %s" , buffer );
316+ if (strchr (buffer , '\n' ) == NULL )
317+ fprintf (stderr , "\n" );
324318 v = PyRun_String (buffer , Py_single_input , globals , globals );
325319 if (v == NULL ) {
326320 if (PyErr_Occurred () == PyExc_SystemExit ) {
0 commit comments