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

Discussion in 'Python' started by たか, May 29, 2008.

  1. たか

    たか Guest

    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
     
    たか, May 29, 2008
    #1
    1. Advertising

  2. たか

    Guest

    On May 29, 9:26 am, たか <> 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
     
    , May 29, 2008
    #2
    1. Advertising

  3. たか

    たか Guest

    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
     
    たか, May 29, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ivan
    Replies:
    2
    Views:
    439
    msnews.microsoft.com
    Apr 23, 2004
  2. Darren
    Replies:
    0
    Views:
    512
    Darren
    Oct 11, 2004
  3. JR
    Replies:
    7
    Views:
    479
  4. yogesh
    Replies:
    1
    Views:
    379
    Victor Bazarov
    Mar 14, 2007
  5. Mike A
    Replies:
    17
    Views:
    272
    Dr John Stockton
    Nov 19, 2003
Loading...

Share This Page