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



$B$?$+(B

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
 
I

Inyeol.Lee

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
 


$B$?$+(B

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top