diff options
author | Petr Viktorin <encukou@gmail.com> | 2021-06-24 12:57:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-24 12:57:28 (GMT) |
commit | 29987f72650b7cccee4df216c8297e8484a44e6a (patch) | |
tree | ae457ca35b315de536ef0c42c52b49ef32bd1f08 /Doc/faq | |
parent | 6c76df2b86d742cc294beae7f9b6bafabb946ad5 (diff) | |
download | cpython-29987f72650b7cccee4df216c8297e8484a44e6a.zip cpython-29987f72650b7cccee4df216c8297e8484a44e6a.tar.gz cpython-29987f72650b7cccee4df216c8297e8484a44e6a.tar.bz2 |
bpo-40939: Remove documentation for `PyParser_*` & add porting notes (GH-26855)
I tried to be relatively thorough and give lots of links.
One reason is that this wasn't deprecated very long; also it seems people running into this tend to not be familiar with similar APIs.
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Diffstat (limited to 'Doc/faq')
-rw-r--r-- | Doc/faq/extending.rst | 35 |
1 files changed, 2 insertions, 33 deletions
diff --git a/Doc/faq/extending.rst b/Doc/faq/extending.rst index aecb56e..3379e41 100644 --- a/Doc/faq/extending.rst +++ b/Doc/faq/extending.rst @@ -275,39 +275,8 @@ for more hints. However sometimes you have to run the embedded Python interpreter in the same thread as your rest application and you can't allow the -:c:func:`PyRun_InteractiveLoop` to stop while waiting for user input. The one -solution then is to call :c:func:`PyParser_ParseString` and test for ``e.error`` -equal to ``E_EOF``, which means the input is incomplete. Here's a sample code -fragment, untested, inspired by code from Alex Farber:: - - #define PY_SSIZE_T_CLEAN - #include <Python.h> - #include <node.h> - #include <errcode.h> - #include <grammar.h> - #include <parsetok.h> - #include <compile.h> - - int testcomplete(char *code) - /* code should end in \n */ - /* return -1 for error, 0 for incomplete, 1 for complete */ - { - node *n; - perrdetail e; - - n = PyParser_ParseString(code, &_PyParser_Grammar, - Py_file_input, &e); - if (n == NULL) { - if (e.error == E_EOF) - return 0; - return -1; - } - - PyNode_Free(n); - return 1; - } - -Another solution is trying to compile the received string with +:c:func:`PyRun_InteractiveLoop` to stop while waiting for user input. +A solution is trying to compile the received string with :c:func:`Py_CompileString`. If it compiles without errors, try to execute the returned code object by calling :c:func:`PyEval_EvalCode`. Otherwise save the input for later. If the compilation fails, find out if it's an error or just |