By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

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

P: n/a
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-tel...alid-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
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 29, 9:26 am, たか <takau...@gmail.comwrote:
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
Jun 27 '08 #2

P: n/a
thanks for your reply.

I tried below python codes with "Py_single_input" instead of
"Py_file_input".

sample 1 : result is INCOMPLETE
for i in range(3):\n

sample 2 : result is COMPLETE(I want to get INCOMPLETE or something)
for i in range(3):\n\tprint i
or
for i in range(3):\n\tprint i\n

"Py_single_input" and "Py_file_input" seem the same. How come?
Plz check details in Grammar/Grammar from python source distribution
thanks. I challenge it.

--urai
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.