ANNOUNCE: 'goto' for Python
Daniel Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Apr 1 16:41:34 EST 2004
Michael Hudson <mwh at python.net> wrote:
: Camilo Olarte <colarte at telesat.com.co> writes:
:> >> Entrian Solutions is pleased to announce version 1.0 of the 'goto'
:> >> module.
:> >>
:> >> This adds the 'goto' and 'comefrom' keywords to Python 2.3, adding
:> >> flexibility to Python's control flow mechanisms and allowing Python
:> >> programmers to use many common control flow idioms that were previously
:> >> denied to them.
:>
:> >>>>>> What is the day today ? Oh yes the first of April ! <<<<<<
:>
:> OOUCH ! I Almost believe it!
: There's nothing to believe: it works!
Hello,
It doesn't work from the interactive interpreter, though:
###
>>> from goto import goto, label
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/lib/python2.3/site-packages/goto.py", line 255, in _trace
_addToCaches(filename)
File "/usr/local/lib/python2.3/site-packages/goto.py", line 229, in _addToCaches
for tokenType, tokenString, (startRow, startCol), (endRow, endCol), line \
IOError: [Errno 2] No such file or directory: '<stdin>'
###
I haven't looked at the code closely enough to understand exactly how
to fix this. But in the meantime, here's a patch to at least give an
error message when one tries to use it interactively:
--- goto.py Tue Mar 30 12:24:28 2004
+++ goto-fixed.py Thu Apr 1 13:37:10 2004
@@ -208,6 +208,15 @@
# Source filenames -> comefrom label names -> line numbers of those comefroms.
_comefromNameCache = {}
+
+def _openModuleFilename(moduleFilename):
+ """Opens up the file named by moduleFilename. In the special case where
+ the file is <stdin>, we raise an IOError."""
+ if moduleFilename == '<stdin>':
+ raise IOError, "module cannot be used on sys.stdin."
+ return open(moduleFilename, 'r')
+
+
def _addToCaches(moduleFilename):
"""Finds the labels and gotos in a module and adds them to the caches."""
@@ -227,7 +236,7 @@
# Tokenize the module; 'window' is the last two (type, string) pairs.
window = [(None, ''), (None, '')]
for tokenType, tokenString, (startRow, startCol), (endRow, endCol), line \
- in tokenize.generate_tokens(open(moduleFilename, 'r').readline):
+ in tokenize.generate_tokens(_openModuleFilename(moduleFilename).readline):
# Plain goto: "goto .x"
if window == plainGotoPattern:
_plainGotoCache[moduleFilename][startRow] = tokenString
Hope this helps!
More information about the Python-list
mailing list