Anyone understand this syntax error?

Discussion in 'Python' started by Sean Hammond, Dec 2, 2006.

  1. Sean Hammond

    Sean Hammond Guest

    Anyone understand this?

    Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
    [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def markdown_perl(input):

    .... """Send 'input' (string) to the markdown perl script, and return
    the
    .... output from markdown (string).
    ....
    .... input: a string of markdown-formatted text, including \n's at
    the end
    .... of lines, that will be sent to the markdown process.
    ....
    .... returns: a string of valid XHTML from markdown
    .... """
    .... import tempfile
    .... import commands
    .... file = tempfile.NamedTemporaryFile()
    .... file.write(input)
    .... file.flush()
    .... return commands.getoutput('./markdown.pl '+file.name)
    File "<stdin>", line 15
    return commands.getoutput('./markdown.pl '+file.name)
    ^
    SyntaxError: invalid syntax
    >>>


    I don't get it. Syntax seems fine to me, just a normal string
    concatenation.

    --
     
    Sean Hammond, Dec 2, 2006
    #1
    1. Advertisements

  2. Sean Hammond

    John Machin Guest

    Sean Hammond wrote:
    > Anyone understand this?
    >
    > Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
    > [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> def markdown_perl(input):

    > ... """Send 'input' (string) to the markdown perl script, and return
    > the
    > ... output from markdown (string).
    > ...
    > ... input: a string of markdown-formatted text, including \n's at
    > the end
    > ... of lines, that will be sent to the markdown process.
    > ...
    > ... returns: a string of valid XHTML from markdown
    > ... """
    > ... import tempfile
    > ... import commands
    > ... file = tempfile.NamedTemporaryFile()
    > ... file.write(input)
    > ... file.flush()
    > ... return commands.getoutput('./markdown.pl '+file.name)
    > File "<stdin>", line 15
    > return commands.getoutput('./markdown.pl '+file.name)
    > ^
    > SyntaxError: invalid syntax
    > >>>

    >
    > I don't get it. Syntax seems fine to me, just a normal string
    > concatenation.


    Superficially there is nothing wrong with it. After saving that to a
    file and doing the editing necessary to change it from a screen dump to
    a script, it loads OK on windows with Python 2.4.3 and 2.5.

    It is probably not complaining about the string concatenation. The
    caret is positioned at the start of the statement; this would indicate
    that the problem is on an earlier line. There was a while back a
    strange error that only manifested itself in very large source files
    (e.g. those generated by the pywin32 package's makepy routine) and
    could be cured by adding trailing spaces to the line before the
    allegedly offending line. However that seems to have gone away.

    My guess is that the problem is something to do with tabs/spaces or you
    have some other invisible character at the start of the return
    statement. As the text has been through two mail or news clients, the
    problem may have been munged away and I'm not seeing it.

    Two sugggestions:
    (1) at an interactive prompt:

    print repr(open("yourfile.py").readlines())

    and eyeball the last few lines for monkey business.

    (2) e-mail somebody (like me) a zipped (i.e. e-mail-munging-proof) copy
    of your file.

    Two other comments on your code:
    (1) It's not wise to use "file" as a variable name; it shadows the
    built-in file(). Not a problem in this code but it's a bad habit that
    could bite you later.
    (2) Any good reason for flushing the file instead of closing it?

    HTH,
    John
     
    John Machin, Dec 2, 2006
    #2
    1. Advertisements

  3. Sean Hammond

    Peter Otten Guest

    Sean Hammond wrote:

    >
    > Anyone understand this?
    >
    > Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
    > [GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> def markdown_perl(input):

    > ... """Send 'input' (string) to the markdown perl script, and return
    > the
    > ... output from markdown (string).
    > ...
    > ... input: a string of markdown-formatted text, including \n's at
    > the end
    > ... of lines, that will be sent to the markdown process.
    > ...
    > ... returns: a string of valid XHTML from markdown
    > ... """
    > ... import tempfile
    > ... import commands
    > ... file = tempfile.NamedTemporaryFile()
    > ... file.write(input)
    > ... file.flush()
    > ... return commands.getoutput('./markdown.pl '+file.name)
    > File "<stdin>", line 15
    > return commands.getoutput('./markdown.pl '+file.name)
    > ^
    > SyntaxError: invalid syntax
    >>>>

    >
    > I don't get it. Syntax seems fine to me, just a normal string
    > concatenation.
    >
    > --


    Are you perhaps mixing tabs and spaces?

    >>> def f():

    .... print "hello" # four spaces before 'print'
    .... return 42 # one tab before 'return'
    File "<stdin>", line 3
    return 42
    ^
    SyntaxError: invalid syntax

    Peter
     
    Peter Otten, Dec 3, 2006
    #3
    1. Advertisements

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. Wes Harrison
    Replies:
    3
    Views:
    2,630
    horahora.geo
    Jan 23, 2008
  2. Chris Saunders

    Don't understand syntax error

    Chris Saunders, May 16, 2005, in forum: C Programming
    Replies:
    9
    Views:
    725
    CBFalconer
    May 17, 2005
  3. Stef Mientki
    Replies:
    0
    Views:
    856
    Stef Mientki
    Oct 20, 2008
  4. Antoon Pardon

    I don't understand this syntax error

    Antoon Pardon, Jan 19, 2009, in forum: Python
    Replies:
    1
    Views:
    376
    Peter Otten
    Jan 19, 2009
  5. Good Night Moon
    Replies:
    9
    Views:
    526
    Rick DeNatale
    Jul 25, 2007
  6. Albert Schlef

    Syntax error I don't understand

    Albert Schlef, Dec 28, 2008, in forum: Ruby
    Replies:
    9
    Views:
    350
    Brian Candler
    Dec 28, 2008
  7. Mark Richards
    Replies:
    3
    Views:
    484
    Tad McClellan
    Nov 18, 2007
  8. someone
    Replies:
    4
    Views:
    379
    someone
    Apr 18, 2013
Loading...