]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
SF bug #1224621: tokenize module does not detect inconsistent dedents
authorRaymond Hettinger <python@rcn.com>
Tue, 21 Jun 2005 07:53:56 +0000 (07:53 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 21 Jun 2005 07:53:56 +0000 (07:53 +0000)
Lib/test/test_tokenize.py
Lib/tokenize.py
Misc/NEWS

index d21740468f1751afad91d4b535c5b450fab3ad4c..5dd20d36f9f0fe677ae1c006da30d38e4cc3a44e 100644 (file)
@@ -1,4 +1,4 @@
-from test.test_support import verbose, findfile
+from test.test_support import verbose, findfile, TestFailed
 import tokenize, os, sys
 
 if verbose:
@@ -10,3 +10,21 @@ f.close()
 
 if verbose:
     print 'finished'
+
+###### Test detecton of IndentationError ######################
+
+from cStringIO import StringIO
+
+sampleBadText = """
+def foo():
+    bar
+  baz
+"""
+
+try:
+    for tok in tokenize.generate_tokens(StringIO(sampleBadText).readline):
+        pass
+except IndentationError:
+    pass
+else:
+    raise TestFailed("Did not detect IndentationError:")
index 9087e84ca0e2a30277fe344482be8c43d7685412..fc97a2874e0e0497575364a964c264e060b5e12e 100644 (file)
@@ -225,6 +225,9 @@ def generate_tokens(readline):
                 indents.append(column)
                 yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
             while column < indents[-1]:
+                if column not in indents:
+                    raise IndentationError(
+                        "unindent does not match any outer indentation level")
                 indents = indents[:-1]
                 yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
 
index b4e626f59edf6926e1bbc529a1083b88814a28ef..76ec7c572ecfd81d44aabaf0f5d54c77da7c0e1e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,8 @@ Extension Modules
 Library
 -------
 
+- Bug #1224621: tokenize module does not detect inconsistent dedents
+
 - Bug #1196315: fix weakref.WeakValueDictionary constructor.
 
 - Bug #1213894: os.path.realpath didn't resolve symlinks that were the first