@@ -29,6 +29,13 @@ def __instancecheck__(cls, instance):
2929 return issubclass (instance .__class__ , cls )
3030
3131
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+
3239class newbytes (with_metaclass (BaseNewBytes , _builtin_bytes )):
3340 """
3441 A backport of the Python 3 bytes object to Py2
@@ -42,14 +49,14 @@ def __new__(cls, *args, **kwargs):
4249 bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
4350 bytes(int) -> bytes object of size given by the parameter initialized with null bytes
4451 bytes() -> empty bytes object
45-
52+
4653 Construct an immutable array of bytes from:
4754 - an iterable yielding integers in range(256)
4855 - a text string encoded using the specified encoding
4956 - any object implementing the buffer API.
5057 - an integer
5158 """
52-
59+
5360 encoding = None
5461 errors = None
5562
@@ -91,7 +98,7 @@ def __new__(cls, *args, **kwargs):
9198 if errors is not None :
9299 newargs .append (errors )
93100 value = args [0 ].encode (* newargs )
94- ###
101+ ###
95102 elif isinstance (args [0 ], Iterable ):
96103 if len (args [0 ]) == 0 :
97104 # This could be an empty list or tuple. Return b'' as on Py3.
@@ -102,8 +109,7 @@ def __new__(cls, *args, **kwargs):
102109 # But then we can't index into e.g. frozensets. Try to proceed
103110 # anyway.
104111 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 ]])
107113 except :
108114 raise ValueError ('bytes must be in range(0, 256)' )
109115 elif isinstance (args [0 ], Integral ):
@@ -113,7 +119,7 @@ def __new__(cls, *args, **kwargs):
113119 else :
114120 value = args [0 ]
115121 return super (newbytes , cls ).__new__ (cls , value )
116-
122+
117123 def __repr__ (self ):
118124 return 'b' + super (newbytes , self ).__repr__ ()
119125
@@ -140,15 +146,15 @@ def __contains__(self, key):
140146 else :
141147 newbyteskey = newbytes (key )
142148 return issubset (list (newbyteskey ), list (self ))
143-
149+
144150 @no (unicode )
145151 def __add__ (self , other ):
146152 return newbytes (super (newbytes , self ).__add__ (other ))
147153
148154 @no (unicode )
149155 def __radd__ (self , left ):
150156 return newbytes (left ) + self
151-
157+
152158 @no (unicode )
153159 def __mul__ (self , other ):
154160 return newbytes (super (newbytes , self ).__mul__ (other ))
@@ -371,32 +377,32 @@ def rstrip(self, bytes_to_strip=None):
371377 """
372378 Strip trailing bytes contained in the argument.
373379 If the argument is omitted, strip trailing ASCII whitespace.
374- """
380+ """
375381 return newbytes (super (newbytes , self ).rstrip (bytes_to_strip ))
376382
377383 @no (unicode )
378384 def strip (self , bytes_to_strip = None ):
379385 """
380386 Strip leading and trailing bytes contained in the argument.
381387 If the argument is omitted, strip trailing ASCII whitespace.
382- """
388+ """
383389 return newbytes (super (newbytes , self ).strip (bytes_to_strip ))
384390
385391 def lower (self ):
386392 """
387393 b.lower() -> copy of b
388-
394+
389395 Return a copy of b with all ASCII characters converted to lowercase.
390- """
396+ """
391397 return newbytes (super (newbytes , self ).lower ())
392398
393399 @no (unicode )
394400 def upper (self ):
395401 """
396402 b.upper() -> copy of b
397-
403+
398404 Return a copy of b with all ASCII characters converted to uppercase.
399- """
405+ """
400406 return newbytes (super (newbytes , self ).upper ())
401407
402408 @classmethod
0 commit comments