Anyone understand this syntax error?

John Machin sjmachin at lexicon.net
Sat Dec 2 18:13:54 EST 2006


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




More information about the Python-list mailing list