473,287 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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
2 1532
On May 29, 9:26 am, $B$?$+(B <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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: b83503104 | last post by:
Hi, I want to accept the user's answer yes or no. If I do this: answer = input('y or n?') and type y on the keyboard, python complains Traceback (most recent call last):
11
by: Pete Wilson | last post by:
Hi folks -- The page at http://www.pwilson.net/submit-demo.html will not validate. The validator at http://validator.w3.org tells me I can't have an input inside a form. Would some kind...
2
by: Tom | last post by:
I'm making a database input form that has many text boxes bound to DB fields. I've written a validatedTextBox class that inherits from textBox but validates the input data. It does this with...
1
by: k386 | last post by:
I am having trouble creating a hidden form field, or least having it accessible from getElementById. Rough overview: I am reading an XML file through XMLhttpRequest and outputting each item...
13
by: Jen | last post by:
One user of my application is experiencing an exception "input string not in correct format". But it makes no sense where it is occurring. It is occurring when a string from a textbox ("172") is...
12
by: Pao | last post by:
Hi all For all NEW sites (virtual directories) that I create, I receive always the same error: (I translate so may be a little different) Impossible to visualize the XML page Impossible to...
9
by: Krumble Bunk | last post by:
Hi all, I am trying my hands at writing a shell for unix. A very rubbish shell, but nonetheless, I come to a point where I am confused. I would like to have something like shellstop xyz ...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
9
muaddubby
by: muaddubby | last post by:
Hi I'm running into a scenario with a DataSet that has a schema loaded into it, tries to then load data (that matches the schema), and fails with a "Input string was not in a correct format"...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.