Skip to content

Commit c7af838

Browse files
pablogsalmloskot
andauthored
[3.10] bpo-46502: Remove "How do I tell incomplete input" from FAQ (GH-30925) (GH-30933)
Since, - Py_CompileString no longer allows to distinguish "incomplete input" from "invalid input" - there is no alternative solution available from the Python C API due to how the new parser works (rewritten in 3.9) - the only supported way is to manually import the codeop module from C and use its API as IDLE does, and accept its own complications it is desirable to remove this Q&A from the official FAQ.. (cherry picked from commit f0a6481) Co-authored-by: Mateusz Łoskot <[email protected]> Co-authored-by: Mateusz Łoskot <[email protected]>
1 parent 171fdf2 commit c7af838

File tree

1 file changed

+0
-125
lines changed

1 file changed

+0
-125
lines changed

Doc/faq/extending.rst

-125
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ For Red Hat, install the python-devel RPM to get the necessary files.
254254

255255
For Debian, run ``apt-get install python-dev``.
256256

257-
258257
How do I tell "incomplete input" from "invalid input"?
259258
------------------------------------------------------
260259

@@ -273,130 +272,6 @@ you. You can also set the :c:func:`PyOS_ReadlineFunctionPointer` to point at you
273272
custom input function. See ``Modules/readline.c`` and ``Parser/myreadline.c``
274273
for more hints.
275274

276-
However sometimes you have to run the embedded Python interpreter in the same
277-
thread as your rest application and you can't allow the
278-
:c:func:`PyRun_InteractiveLoop` to stop while waiting for user input.
279-
A solution is trying to compile the received string with
280-
:c:func:`Py_CompileString`. If it compiles without errors, try to execute the
281-
returned code object by calling :c:func:`PyEval_EvalCode`. Otherwise save the
282-
input for later. If the compilation fails, find out if it's an error or just
283-
more input is required - by extracting the message string from the exception
284-
tuple and comparing it to the string "unexpected EOF while parsing". Here is a
285-
complete example using the GNU readline library (you may want to ignore
286-
**SIGINT** while calling readline())::
287-
288-
#include <stdio.h>
289-
#include <readline.h>
290-
291-
#define PY_SSIZE_T_CLEAN
292-
#include <Python.h>
293-
#include <object.h>
294-
#include <compile.h>
295-
#include <eval.h>
296-
297-
int main (int argc, char* argv[])
298-
{
299-
int i, j, done = 0; /* lengths of line, code */
300-
char ps1[] = ">>> ";
301-
char ps2[] = "... ";
302-
char *prompt = ps1;
303-
char *msg, *line, *code = NULL;
304-
PyObject *src, *glb, *loc;
305-
PyObject *exc, *val, *trb, *obj, *dum;
306-
307-
Py_Initialize ();
308-
loc = PyDict_New ();
309-
glb = PyDict_New ();
310-
PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ());
311-
312-
while (!done)
313-
{
314-
line = readline (prompt);
315-
316-
if (NULL == line) /* Ctrl-D pressed */
317-
{
318-
done = 1;
319-
}
320-
else
321-
{
322-
i = strlen (line);
323-
324-
if (i > 0)
325-
add_history (line); /* save non-empty lines */
326-
327-
if (NULL == code) /* nothing in code yet */
328-
j = 0;
329-
else
330-
j = strlen (code);
331-
332-
code = realloc (code, i + j + 2);
333-
if (NULL == code) /* out of memory */
334-
exit (1);
335-
336-
if (0 == j) /* code was empty, so */
337-
code[0] = '\0'; /* keep strncat happy */
338-
339-
strncat (code, line, i); /* append line to code */
340-
code[i + j] = '\n'; /* append '\n' to code */
341-
code[i + j + 1] = '\0';
342-
343-
src = Py_CompileString (code, "<stdin>", Py_single_input);
344-
345-
if (NULL != src) /* compiled just fine - */
346-
{
347-
if (ps1 == prompt || /* ">>> " or */
348-
'\n' == code[i + j - 1]) /* "... " and double '\n' */
349-
{ /* so execute it */
350-
dum = PyEval_EvalCode (src, glb, loc);
351-
Py_XDECREF (dum);
352-
Py_XDECREF (src);
353-
free (code);
354-
code = NULL;
355-
if (PyErr_Occurred ())
356-
PyErr_Print ();
357-
prompt = ps1;
358-
}
359-
} /* syntax error or E_EOF? */
360-
else if (PyErr_ExceptionMatches (PyExc_SyntaxError))
361-
{
362-
PyErr_Fetch (&exc, &val, &trb); /* clears exception! */
363-
364-
if (PyArg_ParseTuple (val, "sO", &msg, &obj) &&
365-
!strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */
366-
{
367-
Py_XDECREF (exc);
368-
Py_XDECREF (val);
369-
Py_XDECREF (trb);
370-
prompt = ps2;
371-
}
372-
else /* some other syntax error */
373-
{
374-
PyErr_Restore (exc, val, trb);
375-
PyErr_Print ();
376-
free (code);
377-
code = NULL;
378-
prompt = ps1;
379-
}
380-
}
381-
else /* some non-syntax error */
382-
{
383-
PyErr_Print ();
384-
free (code);
385-
code = NULL;
386-
prompt = ps1;
387-
}
388-
389-
free (line);
390-
}
391-
}
392-
393-
Py_XDECREF(glb);
394-
Py_XDECREF(loc);
395-
Py_Finalize();
396-
exit(0);
397-
}
398-
399-
400275
How do I find undefined g++ symbols __builtin_new or __pure_virtual?
401276
--------------------------------------------------------------------
402277

0 commit comments

Comments
 (0)