diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 081941dafbe..0aaae8feb5d 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -81,6 +81,13 @@ dis_bug1333982 = """\ bug1333982.func_code.co_firstlineno + 2, bug1333982.func_code.co_firstlineno + 3) +_BIG_LINENO_FORMAT = """\ +%3d 0 LOAD_GLOBAL 0 (spam) + 3 POP_TOP + 4 LOAD_CONST 0 (None) + 7 RETURN_VALUE +""" + class DisTests(unittest.TestCase): def do_disassembly_test(self, func, expected): s = StringIO.StringIO() @@ -124,6 +131,23 @@ class DisTests(unittest.TestCase): if __debug__: self.do_disassembly_test(bug1333982, dis_bug1333982) + def test_big_linenos(self): + def func(count): + namespace = {} + func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"]) + exec func in namespace + return namespace['foo'] + + # Test all small ranges + for i in xrange(1, 300): + expected = _BIG_LINENO_FORMAT % (i + 2) + self.do_disassembly_test(func(i), expected) + + # Test some larger ranges too + for i in xrange(300, 5000, 10): + expected = _BIG_LINENO_FORMAT % (i + 2) + self.do_disassembly_test(func(i), expected) + def test_main(): run_unittest(DisTests) diff --git a/Misc/NEWS b/Misc/NEWS index 928c1b82df9..f3834bbf1d5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ What's New in Python 2.5 release candidate 1? Core and builtins ----------------- +- Bug #1512814, Fix incorrect lineno's when code within a function + had more than 255 blank lines. + - Patch #1521179: Python now accepts the standard options ``--help`` and ``--version`` as well as ``/?`` on Windows. diff --git a/Python/compile.c b/Python/compile.c index a96a503723f..8341cc93bbb 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4098,9 +4098,10 @@ corresponding to a bytecode address A should do something like this In order for this to work, when the addr field increments by more than 255, the line # increment in each pair generated must be 0 until the remaining addr -increment is < 256. So, in the example above, com_set_lineno should not (as -was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to -255, 0, 45, 255, 0, 45. +increment is < 256. So, in the example above, assemble_lnotab (it used +to be called com_set_lineno) should not (as was actually done until 2.2) +expand 300, 300 to 255, 255, 45, 45, + but to 255, 0, 45, 255, 0, 45. */ static int @@ -4155,12 +4156,12 @@ assemble_lnotab(struct assembler *a, struct instr *i) } lnotab = (unsigned char *) PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off; - *lnotab++ = 255; *lnotab++ = d_bytecode; + *lnotab++ = 255; d_bytecode = 0; for (j = 1; j < ncodes; j++) { - *lnotab++ = 255; *lnotab++ = 0; + *lnotab++ = 255; } d_lineno -= ncodes * 255; a->a_lnotab_off += ncodes * 2;