Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Syntax checking python...

18 views
Skip to first unread message

Afanasiy

unread,
Jan 31, 2003, 5:05:48 PM1/31/03
to

In other languages, I can receive syntax errors without the script having
to execute. Please do not let this hurt your feelings. I merely bring up
the other languages as an example of what I mean and what I am used to.

In Python, I will not receive a syntax error until the interpreter runs
right into it. This is not very efficient for me during my development.

Is there a way I can be notified of my typos without having to run the
interpreter up to those mistakes? That is my question. No smartasses.

Chad Netzer

unread,
Jan 31, 2003, 5:50:31 PM1/31/03
to
On Fri, 2003-01-31 at 14:05, Afanasiy wrote:
> In other languages, I can receive syntax errors without the script having
> to execute.

This is (presumably) an IDE issue. It is not a feature of the language,
but of the development tools. Am I correct?

I suppose such a feature could be made to work in IDLE (to some
extent). Maybe it already exists. But it amounts to the computer
running your script in the background and looking for errors. I don't
use IDLE, but perhaps someone else who does knows more about this kind
of feature.

Unless you are just talking about keyword typos, in which case emacs
(and probably Vim) will highlight the keywords for you.

--
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)

holger krekel

unread,
Jan 31, 2003, 5:36:39 PM1/31/03
to

There really isn't much difference regarding SyntaxErrors between
most languages: you have to process your source code with some
program (usually a compiler) and then you get any complaints.
Well, in python this is 'python' itself. If you want a more convenient
tool then you can easily write some lines of code to have that.
There is a 'parser' module among others stuff that you can
use for this.

hope that helps,

holger

Mike C. Fletcher

unread,
Jan 31, 2003, 5:56:49 PM1/31/03
to
Most practical approach (IMO):
Use a Python-aware editor (PythonWin, for instance), which
syntax-colours your source-code as you go. Sure, it doesn't catch
everything, but it'll highlight matching braces/brackets/parens as you
move about, check for string-literal endings, highlight keywords, and
generally attempt to show the code as understood by the interpreter.

If for some particular reason you really do need to do robust
pre-checking of files, see pychecker, or the compile module.

HTH,
Mike

Afanasiy wrote:

--
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


John Roth

unread,
Jan 31, 2003, 6:48:01 PM1/31/03
to

"Afanasiy" <abeli...@hotmail.com> wrote in message
news:jpsl3vcbb4tlv97l7...@4ax.com...

>
> In other languages, I can receive syntax errors without the script
having
> to execute. Please do not let this hurt your feelings. I merely bring
up
> the other languages as an example of what I mean and what I am used
to.
>
> In Python, I will not receive a syntax error until the interpreter
runs
> right into it. This is not very efficient for me during my
development.

As far as development efficiency, I proceed in very small increments,
and generally write unit tests for everything as I go along. This
flushes
out syntax errors rapidly. With this style of programming, I don't find
the lack of a separate syntax checker to be significant; in fact I
wouldn't
use it if it was present because it would slow me down.

> Is there a way I can be notified of my typos without having to run the
> interpreter up to those mistakes? That is my question. No smartasses.

John Roth


Andrew Bennetts

unread,
Jan 31, 2003, 7:14:09 PM1/31/03
to
On Fri, Jan 31, 2003 at 10:05:48PM +0000, Afanasiy wrote:
>
> In other languages, I can receive syntax errors without the script having
> to execute. Please do not let this hurt your feelings. I merely bring up
> the other languages as an example of what I mean and what I am used to.
>
> In Python, I will not receive a syntax error until the interpreter runs
> right into it. This is not very efficient for me during my development.

Actually, you'll receive a SyntaxError as soon as you import a module with
invalid syntax, which is generally very early in the running of your
program.

> Is there a way I can be notified of my typos without having to run the
> interpreter up to those mistakes? That is my question. No smartasses.

Of course, typos cause more than just SyntaxErrors....

def foo(arg1):
print agr1

That's syntactically correct, but obviously wrong, and I suspect this is
more the sort of typo you have in mind (SyntaxErrors are rarely a problem if
you use an editor with syntax highlighting). In this case, I've found
PyChecker <http://pychecker.sf.net/> to be extremely effective (and it'll
tell you about plain SyntaxErrors as well).

PyChecker is remarkably good at finding most of the typos that I make. Its
biggest drawbacks for me are:
- it still throws a fair few false positives
- it can't cope with Zope's extension classes, or something, meaning I
can't use it to check my Zope products that I develop at work.

Despite that, I still use it regularly, because it is so handy. Hopefully
it solves your problem too.

Regards,

-Andrew.


Laura Creighton

unread,
Jan 31, 2003, 11:42:48 PM1/31/03
to
> --
> http://mail.python.org/mailman/listinfo/python-list

Does PyChecker http://pychecker.sourceforge.net/ accomplish the same
goals as you want?

Laura Creighton

Alex Coventry

unread,
Feb 1, 2003, 12:27:56 AM2/1/03
to

hi. i use emacs with the following script, which i've arranged to
execute whenever i execute or save a python buffer:

http://web.mit.edu/alex_c/Public/find_syntax_error.py

the elisp function i use in conjunction with this is below. it warps
the cursor to the script's best guess as to the line on which the syntax
error occurred.

hope this helps.

alex.

(defun check-syntax () (interactive)

"Check whether there are any errors in the current buffer."

(let (

;; Name of the file the buffer will be saved to.
(file (expand-file-name (make-temp-name "python-")
py-temp-directory))

;; Store the results of calling out to find_syntax_error.py
compile-result row column msg)

;; Add some whitespace at the end, to make sure the entire file
;; gets compiled.
(save-excursion
(goto-char (point-max))
(if (not (looking-at "^\\s-*$")) (insert "\n")))

;; Save to the temp file
(write-region (point-min) (point-max) file nil 'nomsg)

;; Get the result from find_syntax_error.py
(setq compile-result
(shell-command-to-string
(concat py-python-command " "
MY_CVS_PATH "tools/admin/find_syntax_error.py "
file)))

;; Get rid of the temp file.
(delete-file file)


(if (string-match "No error found\\.\n" compile-result)
(message "No syntax errors")
(goto-char (point-min))
(if (not (string-match "^\\([0-9]+\\) \\([0-9]+\\) \\(.*\\)"
compile-result))
(progn
(message compile-result)
(error (concat
"Could not find error coordinates. "
"Error message was: " compile-result
))))
(setq row (string-to-int (match-string 1 compile-result))
column (string-to-int (match-string 2 compile-result))
msg (match-string 3 compile-result)
)
(goto-char (point-min))
(next-line row)

(forward-char column)
(error (concat "Syntax Error found: " msg))
)))

Ganesan R

unread,
Feb 1, 2003, 12:48:01 AM2/1/03
to
>>>>> "Laura" == Laura Creighton <l...@strakt.com> writes:

> Does PyChecker http://pychecker.sourceforge.net/ accomplish the same
> goals as you want?

PyChecker is very helpful, but I find it lacking in one respect. It doesn't
catch mispelled method names in dynamic code paths. Consider this
(contrived) example:

------------

#!/usr/bin/python

import sys

if len(sys.argv) > 2:
"hello".replce("h", "g")
# ^^
# note the typo here
-----------

PyChecker can't catch that. Considering the dynamic nature of python, I
don't know how difficult it is to catch such problems. This is not just
about typos; I am a relative newbie and tend to get method names wrong some
times. For example, when I started with python I used to make a mistake
of saying sys.argv.len() instead of len(sys.argv). If PyChecker can do
more exhaustive checks in all code paths, it would be a godsend.

Ganesan

--
Ganesan R

Trent Mick

unread,
Jan 31, 2003, 11:58:41 PM1/31/03
to
[Afanasiy wrote]

Python's py_compile module can do what you are asking.

>>> import py_compile
>>> py_compile.compile("t.py")
File "t.py", line 1
print "asdf" +
^
SyntaxError: invalid syntax

There is a compileall.py script that takes a directory and compiles all
the Python files in it. This will report any syntax errors.

In neither of these cases are your Python scripts executed. What you
probably want to do is write a small Python script that takes a
filename (your script) and compiles it with the py_compile module.
You will get a SyntaxError traceback (like above) for the first syntax
error in your module.

If you want other warnings you should look at PyChecker, as others have
suggested here. The current PyChecker *does* execute the top-level of
your code so it is not ideal. A version is in the works to not have to
execute code, but I don't know when that is due. Note that this is
often not considered a problem because it is usually considered
unnecessary and bad form to have top-level Python code actually execute
anything. If you use the:
if __name__ == "__main__":
# do stuff here
mechanism then you will always be fine.

Trent

--
Trent Mick
Tre...@ActiveState.com

0 new messages