How do I tell "imconplete input" from "valid input"?

Inyeol.Lee at gmail.com Inyeol.Lee at gmail.com
Thu May 29 04:43:02 EDT 2008


On May 29, 9:26 am, たか <takau... at gmail.com> wrote:
> Hi everyone,
>
>  I am developing the console which has the embedded Python interactive
> interpreter. So, I want to judge whether current command is complete
> or not. Below is good example to solve this problem.
> //
> //http://effbot.org/pyfaq/how-do-i-tell-incomplete-input-from-invalid-i...
> //
> 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;
>
> }
>
>  But, I think this code has a problem. For example, when argument
> 'code' has below python command,
>
> if x % 2:
>    print "odd"
>
> at the current situation, this function returns COMPLETE!
> But, I think that sometimes users want to type "else" statement. So, I
> expected to get INCOMPLETE from this function, but I didn't.
>
> How should I do it?
>
> Thanks,
> urai

I guess you should use "Py_single_input" instead of "Py_file_input" in
your code, which requires extra NEWLINE to end complex statement.
Plz check details in Grammar/Grammar from python source distribution

--Inyeol



More information about the Python-list mailing list