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

たか takaurai at gmail.com
Thu May 29 03:26:45 EDT 2008


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-input.htm
//
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



More information about the Python-list mailing list