Skip to content

Commit

Permalink
Corrected a formerly obscure bug in the compiler module; Python Scripts
Browse files Browse the repository at this point in the history
revealed it.  "print" with output to a stream and a trailing comma needs to
end with a POP_TOP instruction.
  • Loading branch information
hathawsh committed Jun 8, 2001
1 parent 4abcd22 commit 08c2711
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler/pycodegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ def visitCallFunc(self, node):
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)
Expand All @@ -806,11 +806,13 @@ def visitPrint(self, node):
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:
Expand Down
9 changes: 9 additions & 0 deletions tests/restricted_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tests/testRestrictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ def checkPrint(self):
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
Expand Down

0 comments on commit 08c2711

Please sign in to comment.