Commit a399b1ae authored by Shane Hathaway's avatar Shane Hathaway

Corrected a formerly obscure bug in the compiler module; Python Scripts

revealed it.  "print" with output to a stream and a trailing comma needs to
end with a POP_TOP instruction.
parent b3041d62
......@@ -795,7 +795,7 @@ class CodeGenerator:
opcode = callfunc_opcode_info[have_star, have_dstar]
self.emit(opcode, kw << 8 | pos)
def visitPrint(self, node):
def visitPrint(self, node, newline=0):
self.set_lineno(node)
if node.dest:
self.visit(node.dest)
......@@ -806,11 +806,13 @@ class CodeGenerator:
if node.dest:
self.emit('ROT_TWO')
self.emit('PRINT_ITEM_TO')
if not newline:
self.emit('POP_TOP')
else:
self.emit('PRINT_ITEM')
def visitPrintnl(self, node):
self.visitPrint(node)
self.visitPrint(node, 1)
if node.dest:
self.emit('PRINT_NEWLINE_TO')
else:
......
......@@ -13,6 +13,15 @@ def print2():
print >>x, 'Hello, world!',
return printed
def printLines():
# This failed before Zope 2.4.0a2
r = range(3)
for n in r:
for m in r:
print m + n * len(r),
print
return printed
def primes():
# Somewhat obfuscated code on purpose
print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
......
......@@ -169,6 +169,10 @@ class RestrictionTests(unittest.TestCase):
res = self.execFunc('print%s' % i)
assert res == 'Hello, world!', res
def checkPrintLines(self):
res = self.execFunc('printLines')
assert res == '0 1 2\n3 4 5\n6 7 8\n', res
def checkPrimes(self):
res = self.execFunc('primes')
assert res == '[2, 3, 5, 7, 11, 13, 17, 19]', res
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment