How to make compile work for strings that contain ^M's?

Alex cut_me_out at hotmail.com
Sun Apr 30 18:19:37 EDT 2000


Hi.  I just noticed that the command 

compile(file, 'test', 'exec')

can fail if 'file' contains '^M^J' at the end of each line.
Specifically, if file is WDhandler.py, in the webdebug package
(http://www.cyberclip.com/webdebug/webdebug-1.15.tar.gz), then I get the
error 

compile(file, 'test', 'exec')
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "<string>", line 29
    """
       ^
SyntaxError: invalid syntax

... and sys.exc_info()[1].offset == 1043.

Is there any way that I can get compile to work properly in this case?
Neither python nor emacs seem to have a problem with '^M^J' line-endings
otherwise, but I use the compile(file, 'test', 'exec') command in a
script called by emacs to systematically test my scripts for syntax
errors before I save them.

It just occurred to me to strip out the ^M's in the script that does the
syntax checking.  I guess that'll do, but I'd still be interested in
whether there's a nicer way.

Alex.



More information about the Python-list mailing list