From 00e41defe8801ef37548fb60abacb3be13156d2a Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Fri, 23 Feb 2007 19:56:57 +0000 Subject: Bytes literal. --- Lib/compiler/ast.py | 14 ++++++++++++++ Lib/compiler/pyassem.py | 1 + Lib/compiler/pycodegen.py | 4 ++++ Lib/compiler/transformer.py | 6 ++++-- 4 files changed, 23 insertions(+), 2 deletions(-) (limited to 'Lib/compiler') diff --git a/Lib/compiler/ast.py b/Lib/compiler/ast.py index bc283c08bd..4794d66da6 100644 --- a/Lib/compiler/ast.py +++ b/Lib/compiler/ast.py @@ -267,6 +267,20 @@ class Break(Node): def __repr__(self): return "Break()" +class Bytes(Node): + def __init__(self, value, lineno=None): + self.value = value + self.lineno = lineno + + def getChildren(self): + return self.value, + + def getChildNodes(self): + return () + + def __repr__(self): + return "Bytes(%s)" % (repr(self.value),) + class CallFunc(Node): def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None): self.node = node diff --git a/Lib/compiler/pyassem.py b/Lib/compiler/pyassem.py index cac899d239..f665c543b0 100644 --- a/Lib/compiler/pyassem.py +++ b/Lib/compiler/pyassem.py @@ -792,6 +792,7 @@ class StackDepthTracker: 'DELETE_ATTR': -1, 'STORE_GLOBAL': -1, 'BUILD_MAP': 1, + 'MAKE_BYTES': 0, 'COMPARE_OP': -1, 'STORE_FAST': -1, 'IMPORT_STAR': -1, diff --git a/Lib/compiler/pycodegen.py b/Lib/compiler/pycodegen.py index 8db4e0de72..83fbc173ca 100644 --- a/Lib/compiler/pycodegen.py +++ b/Lib/compiler/pycodegen.py @@ -930,6 +930,10 @@ class CodeGenerator: def visitConst(self, node): self.emit('LOAD_CONST', node.value) + + def visitBytes(self, node): + self.emit('LOAD_CONST', node.value) + self.emit('MAKE_BYTES') def visitKeyword(self, node): self.emit('LOAD_CONST', node.name) diff --git a/Lib/compiler/transformer.py b/Lib/compiler/transformer.py index 5f2face4ab..79b702ce30 100644 --- a/Lib/compiler/transformer.py +++ b/Lib/compiler/transformer.py @@ -745,9 +745,11 @@ class Transformer: return eval(lit) def atom_string(self, nodelist): - k = '' - for node in nodelist: + k = self.decode_literal(nodelist[0][1]) + for node in nodelist[1:]: k += self.decode_literal(node[1]) + if isinstance(k, bytes): + return Bytes(str(k), lineno=nodelist[0][2]) return Const(k, lineno=nodelist[0][2]) def atom_ellipsis(self, nodelist): -- cgit v1.2.1