Skip to content

Commit f0a6481

Browse files
authored
bpo-46502: Remove "How do I tell incomplete input" from FAQ (pythonGH-30925)
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.
1 parent 1319408 commit f0a6481

File tree

1 file changed

+0
-122
lines changed

1 file changed

+0
-122
lines changed

Doc/faq/extending.rst

Lines changed: 0 additions & 122 deletions
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,127 +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-
294-
int main (int argc, char* argv[])
295-
{
296-
int i, j, done = 0; /* lengths of line, code */
297-
char ps1[] = ">>> ";
298-
char ps2[] = "... ";
299-
char *prompt = ps1;
300-
char *msg, *line, *code = NULL;
301-
PyObject *src, *glb, *loc;
302-
PyObject *exc, *val, *trb, *obj, *dum;
303-
304-
Py_Initialize ();
305-
loc = PyDict_New ();
306-
glb = PyDict_New ();
307-
PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ());
308-
309-
while (!done)
310-
{
311-
line = readline (prompt);
312-
313-
if (NULL == line) /* Ctrl-D pressed */
314-
{
315-
done = 1;
316-
}
317-
else
318-
{
319-
i = strlen (line);
320-
321-
if (i > 0)
322-
add_history (line); /* save non-empty lines */
323-
324-
if (NULL == code) /* nothing in code yet */
325-
j = 0;
326-
else
327-
j = strlen (code);
328-
329-
code = realloc (code, i + j + 2);
330-
if (NULL == code) /* out of memory */
331-
exit (1);
332-
333-
if (0 == j) /* code was empty, so */
334-
code[0] = '\0'; /* keep strncat happy */
335-
336-
strncat (code, line, i); /* append line to code */
337-
code[i + j] = '\n'; /* append '\n' to code */
338-
code[i + j + 1] = '\0';
339-
340-
src = Py_CompileString (code, "<stdin>", Py_single_input);
341-
342-
if (NULL != src) /* compiled just fine - */
343-
{
344-
if (ps1 == prompt || /* ">>> " or */
345-
'\n' == code[i + j - 1]) /* "... " and double '\n' */
346-
{ /* so execute it */
347-
dum = PyEval_EvalCode (src, glb, loc);
348-
Py_XDECREF (dum);
349-
Py_XDECREF (src);
350-
free (code);
351-
code = NULL;
352-
if (PyErr_Occurred ())
353-
PyErr_Print ();
354-
prompt = ps1;
355-
}
356-
} /* syntax error or E_EOF? */
357-
else if (PyErr_ExceptionMatches (PyExc_SyntaxError))
358-
{
359-
PyErr_Fetch (&exc, &val, &trb); /* clears exception! */
360-
361-
if (PyArg_ParseTuple (val, "sO", &msg, &obj) &&
362-
!strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */
363-
{
364-
Py_XDECREF (exc);
365-
Py_XDECREF (val);
366-
Py_XDECREF (trb);
367-
prompt = ps2;
368-
}
369-
else /* some other syntax error */
370-
{
371-
PyErr_Restore (exc, val, trb);
372-
PyErr_Print ();
373-
free (code);
374-
code = NULL;
375-
prompt = ps1;
376-
}
377-
}
378-
else /* some non-syntax error */
379-
{
380-
PyErr_Print ();
381-
free (code);
382-
code = NULL;
383-
prompt = ps1;
384-
}
385-
386-
free (line);
387-
}
388-
}
389-
390-
Py_XDECREF(glb);
391-
Py_XDECREF(loc);
392-
Py_Finalize();
393-
exit(0);
394-
}
395-
396-
397275
How do I find undefined g++ symbols __builtin_new or __pure_virtual?
398276
--------------------------------------------------------------------
399277

0 commit comments

Comments
 (0)