@@ -29,6 +29,13 @@ def __instancecheck__(cls, instance):
29
29
return issubclass (instance .__class__ , cls )
30
30
31
31
32
+ def _newchr (x ):
33
+ if isinstance (x , str ): # this happens on pypy
34
+ return x .encode ('ascii' )
35
+ else :
36
+ return chr (x )
37
+
38
+
32
39
class newbytes (with_metaclass (BaseNewBytes , _builtin_bytes )):
33
40
"""
34
41
A backport of the Python 3 bytes object to Py2
@@ -42,14 +49,14 @@ def __new__(cls, *args, **kwargs):
42
49
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
43
50
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
44
51
bytes() -> empty bytes object
45
-
52
+
46
53
Construct an immutable array of bytes from:
47
54
- an iterable yielding integers in range(256)
48
55
- a text string encoded using the specified encoding
49
56
- any object implementing the buffer API.
50
57
- an integer
51
58
"""
52
-
59
+
53
60
encoding = None
54
61
errors = None
55
62
@@ -91,7 +98,7 @@ def __new__(cls, *args, **kwargs):
91
98
if errors is not None :
92
99
newargs .append (errors )
93
100
value = args [0 ].encode (* newargs )
94
- ###
101
+ ###
95
102
elif isinstance (args [0 ], Iterable ):
96
103
if len (args [0 ]) == 0 :
97
104
# This could be an empty list or tuple. Return b'' as on Py3.
@@ -102,8 +109,7 @@ def __new__(cls, *args, **kwargs):
102
109
# But then we can't index into e.g. frozensets. Try to proceed
103
110
# anyway.
104
111
try :
105
- values = [chr (x ) for x in args [0 ]]
106
- value = b'' .join (values )
112
+ value = bytearray ([_newchr (x ) for x in args [0 ]])
107
113
except :
108
114
raise ValueError ('bytes must be in range(0, 256)' )
109
115
elif isinstance (args [0 ], Integral ):
@@ -113,7 +119,7 @@ def __new__(cls, *args, **kwargs):
113
119
else :
114
120
value = args [0 ]
115
121
return super (newbytes , cls ).__new__ (cls , value )
116
-
122
+
117
123
def __repr__ (self ):
118
124
return 'b' + super (newbytes , self ).__repr__ ()
119
125
@@ -140,15 +146,15 @@ def __contains__(self, key):
140
146
else :
141
147
newbyteskey = newbytes (key )
142
148
return issubset (list (newbyteskey ), list (self ))
143
-
149
+
144
150
@no (unicode )
145
151
def __add__ (self , other ):
146
152
return newbytes (super (newbytes , self ).__add__ (other ))
147
153
148
154
@no (unicode )
149
155
def __radd__ (self , left ):
150
156
return newbytes (left ) + self
151
-
157
+
152
158
@no (unicode )
153
159
def __mul__ (self , other ):
154
160
return newbytes (super (newbytes , self ).__mul__ (other ))
@@ -371,32 +377,32 @@ def rstrip(self, bytes_to_strip=None):
371
377
"""
372
378
Strip trailing bytes contained in the argument.
373
379
If the argument is omitted, strip trailing ASCII whitespace.
374
- """
380
+ """
375
381
return newbytes (super (newbytes , self ).rstrip (bytes_to_strip ))
376
382
377
383
@no (unicode )
378
384
def strip (self , bytes_to_strip = None ):
379
385
"""
380
386
Strip leading and trailing bytes contained in the argument.
381
387
If the argument is omitted, strip trailing ASCII whitespace.
382
- """
388
+ """
383
389
return newbytes (super (newbytes , self ).strip (bytes_to_strip ))
384
390
385
391
def lower (self ):
386
392
"""
387
393
b.lower() -> copy of b
388
-
394
+
389
395
Return a copy of b with all ASCII characters converted to lowercase.
390
- """
396
+ """
391
397
return newbytes (super (newbytes , self ).lower ())
392
398
393
399
@no (unicode )
394
400
def upper (self ):
395
401
"""
396
402
b.upper() -> copy of b
397
-
403
+
398
404
Return a copy of b with all ASCII characters converted to uppercase.
399
- """
405
+ """
400
406
return newbytes (super (newbytes , self ).upper ())
401
407
402
408
@classmethod
0 commit comments