Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 00e41de

Browse files
committed
Bytes literal.
1 parent cf297e4 commit 00e41de

15 files changed

Lines changed: 179 additions & 19 deletions

File tree

Include/Python-ast.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ struct _stmt {
176176
enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4,
177177
IfExp_kind=5, Dict_kind=6, Set_kind=7, ListComp_kind=8,
178178
GeneratorExp_kind=9, Yield_kind=10, Compare_kind=11,
179-
Call_kind=12, Num_kind=13, Str_kind=14, Ellipsis_kind=15,
180-
Attribute_kind=16, Subscript_kind=17, Name_kind=18,
181-
List_kind=19, Tuple_kind=20};
179+
Call_kind=12, Num_kind=13, Str_kind=14, Bytes_kind=15,
180+
Ellipsis_kind=16, Attribute_kind=17, Subscript_kind=18,
181+
Name_kind=19, List_kind=20, Tuple_kind=21};
182182
struct _expr {
183183
enum _expr_kind kind;
184184
union {
@@ -254,6 +254,10 @@ struct _expr {
254254
string s;
255255
} Str;
256256

257+
struct {
258+
string s;
259+
} Bytes;
260+
257261
struct {
258262
expr_ty value;
259263
identifier attr;
@@ -465,6 +469,8 @@ expr_ty _Py_Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty
465469
expr_ty _Py_Num(object n, int lineno, int col_offset, PyArena *arena);
466470
#define Str(a0, a1, a2, a3) _Py_Str(a0, a1, a2, a3)
467471
expr_ty _Py_Str(string s, int lineno, int col_offset, PyArena *arena);
472+
#define Bytes(a0, a1, a2, a3) _Py_Bytes(a0, a1, a2, a3)
473+
expr_ty _Py_Bytes(string s, int lineno, int col_offset, PyArena *arena);
468474
#define Ellipsis(a0, a1, a2) _Py_Ellipsis(a0, a1, a2)
469475
expr_ty _Py_Ellipsis(int lineno, int col_offset, PyArena *arena);
470476
#define Attribute(a0, a1, a2, a3, a4, a5) _Py_Attribute(a0, a1, a2, a3, a4, a5)

Include/opcode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extern "C" {
7272
#define LOAD_LOCALS 82
7373
#define RETURN_VALUE 83
7474
#define IMPORT_STAR 84
75-
75+
#define MAKE_BYTES 85
7676
#define YIELD_VALUE 86
7777
#define POP_BLOCK 87
7878
#define END_FINALLY 88

Lib/compiler/ast.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ def getChildNodes(self):
267267
def __repr__(self):
268268
return "Break()"
269269

270+
class Bytes(Node):
271+
def __init__(self, value, lineno=None):
272+
self.value = value
273+
self.lineno = lineno
274+
275+
def getChildren(self):
276+
return self.value,
277+
278+
def getChildNodes(self):
279+
return ()
280+
281+
def __repr__(self):
282+
return "Bytes(%s)" % (repr(self.value),)
283+
270284
class CallFunc(Node):
271285
def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None):
272286
self.node = node

Lib/compiler/pyassem.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ def findDepth(self, insts, debug=0):
792792
'DELETE_ATTR': -1,
793793
'STORE_GLOBAL': -1,
794794
'BUILD_MAP': 1,
795+
'MAKE_BYTES': 0,
795796
'COMPARE_OP': -1,
796797
'STORE_FAST': -1,
797798
'IMPORT_STAR': -1,

Lib/compiler/pycodegen.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,10 @@ def visitDiscard(self, node):
930930

931931
def visitConst(self, node):
932932
self.emit('LOAD_CONST', node.value)
933+
934+
def visitBytes(self, node):
935+
self.emit('LOAD_CONST', node.value)
936+
self.emit('MAKE_BYTES')
933937

934938
def visitKeyword(self, node):
935939
self.emit('LOAD_CONST', node.name)

Lib/compiler/transformer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,11 @@ def decode_literal(self, lit):
745745
return eval(lit)
746746

747747
def atom_string(self, nodelist):
748-
k = ''
749-
for node in nodelist:
748+
k = self.decode_literal(nodelist[0][1])
749+
for node in nodelist[1:]:
750750
k += self.decode_literal(node[1])
751+
if isinstance(k, bytes):
752+
return Bytes(str(k), lineno=nodelist[0][2])
751753
return Const(k, lineno=nodelist[0][2])
752754

753755
def atom_ellipsis(self, nodelist):

Lib/opcode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def jabs_op(name, op):
111111
def_op('LOAD_LOCALS', 82)
112112
def_op('RETURN_VALUE', 83)
113113
def_op('IMPORT_STAR', 84)
114+
def_op('MAKE_BYTES', 85)
114115
def_op('YIELD_VALUE', 86)
115116
def_op('POP_BLOCK', 87)
116117
def_op('END_FINALLY', 88)

Lib/test/test_bytes.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,19 @@ def test_join(self):
403403
self.assertEqual(bytes.join(tuple(lst)), bytes("abc"))
404404
self.assertEqual(bytes.join(iter(lst)), bytes("abc"))
405405
# XXX more...
406-
406+
407+
def test_literal(self):
408+
tests = [
409+
(b"Wonderful spam", u"Wonderful spam"),
410+
(br"Wonderful spam too", u"Wonderful spam too"),
411+
(b"\xaa\x00\000\200", u"\xaa\x00\000\200"),
412+
(br"\xaa\x00\000\200", ur"\xaa\x00\000\200"),
413+
]
414+
for b, s in tests:
415+
self.assertEqual(b, bytes(s, 'latin-1'))
416+
for c in range(128, 256):
417+
self.assertRaises(SyntaxError, eval,
418+
'b"%s"' % chr(c))
407419

408420
# Optimizations:
409421
# __iter__? (optimization)

Lib/test/test_compiler.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,30 @@ def testWithAss(self):
187187
exec(c, dct)
188188
self.assertEquals(dct.get('result'), 1)
189189

190+
def testBytesLiteral(self):
191+
c = compiler.compile("b'foo'", '<string>', 'eval')
192+
b = eval(c)
193+
194+
c = compiler.compile('def f(b=b"foo"):\n'
195+
' b[0] += 1\n'
196+
' return b\n'
197+
'f(); f(); result = f()\n',
198+
'<string>',
199+
'exec')
200+
dct = {}
201+
exec(c, dct)
202+
self.assertEquals(dct.get('result'), b"ioo")
203+
204+
c = compiler.compile('def f():\n'
205+
' b = b"foo"\n'
206+
' b[0] += 1\n'
207+
' return b\n'
208+
'f(); f(); result = f()\n',
209+
'<string>',
210+
'exec')
211+
dct = {}
212+
exec(c, dct)
213+
self.assertEquals(dct.get('result'), b"goo")
190214

191215
NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
192216

Parser/Python.asdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module Python version "$Revision$"
6060
expr? starargs, expr? kwargs)
6161
| Num(object n) -- a number as a PyObject.
6262
| Str(string s) -- need to specify raw, unicode, etc?
63+
| Bytes(string s)
6364
| Ellipsis
6465
-- other literals? bools?
6566

0 commit comments

Comments
 (0)