glb=PyDict_New (); PyDict_SetItemString (glb, "__builtins__ ", PyEval_GetBuiltins ()); while (!done) { line =readline (prompt); if (NULL ==line) /* Ctrl-D pressed */ { done =1; } else { i =strlen (line); if (i >0) add_history (line); /* save non-empty lines */ if (NULL ==code) /* nothing in code yet */ j =0; else j =strlen (code); code =realloc (code, i +j +2); if (NULL ==code) /* out of memory */ exit (1); if (0==j) /* code was empty, so */ code[0] ='\0'; /* keep strncat happy */ strncat (code, line, i); /* append line to code */ code[i +j] ='
'; /* append '
' to code */ code[i +j +1] ='\0'; src =Py_CompileString (code, "<stdin> ", Py_single_input); if (NULL !=src) /* compiled just fine - */ { if (ps1==prompt| | /* ">>> " or */ '
'==code[i +j -1]) /* "... " and double '
' */ { /* so execute it */ dum =PyEval_EvalCode (src, glb, loc); Py_XDECREF (dum); Py_XDECREF (src); free (code); code =NULL ; if (PyErr_Occurred ()) PyErr_Print (); prompt=ps1; } } /* syntax error or E_EOF? */ else if (PyErr_ExceptionMatches (PyExc_SyntaxError)) { PyErr_Fetch (&exc,&val,&trb); /* clears exception! */ if (PyArg_ParseTuple (val, "sO",&msg,&obj)&& !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */ { Py_XDECREF (exc); Py_XDECREF (val); Py_XDECREF (trb); (continued from previous page) (continues on next page) Python Frequently Asked Questions, Release 3.9.5 Chapter 5. Extending/Embedding FAQ 64