27 import sys |
27 import sys |
28 import tempfile |
28 import tempfile |
29 import xmlrpc.client as xmlrpclib |
29 import xmlrpc.client as xmlrpclib |
30 |
30 |
31 from typing import ( |
31 from typing import ( |
|
32 Iterable, |
|
33 Iterator, |
32 List, |
34 List, |
33 Optional, |
35 Optional, |
|
36 Type, |
|
37 TypeVar, |
34 ) |
38 ) |
35 |
39 |
36 ispy3 = sys.version_info[0] >= 3 |
40 ispy3 = sys.version_info[0] >= 3 |
37 ispypy = '__pypy__' in sys.builtin_module_names |
41 ispypy = '__pypy__' in sys.builtin_module_names |
38 TYPE_CHECKING = False |
42 TYPE_CHECKING = False |
39 |
43 |
40 if not globals(): # hide this from non-pytype users |
44 if not globals(): # hide this from non-pytype users |
41 import typing |
45 import typing |
42 |
46 |
43 TYPE_CHECKING = typing.TYPE_CHECKING |
47 TYPE_CHECKING = typing.TYPE_CHECKING |
|
48 |
|
49 _Tbytestr = TypeVar('_Tbytestr', bound='bytestr') |
44 |
50 |
45 |
51 |
46 def future_set_exception_info(f, exc_info): |
52 def future_set_exception_info(f, exc_info): |
47 f.set_exception(exc_info[0]) |
53 f.set_exception(exc_info[0]) |
48 |
54 |
210 # since the appropriate bytes format is done internally. |
216 # since the appropriate bytes format is done internally. |
211 # |
217 # |
212 # https://github.com/google/pytype/issues/500 |
218 # https://github.com/google/pytype/issues/500 |
213 if TYPE_CHECKING: |
219 if TYPE_CHECKING: |
214 |
220 |
215 def __init__(self, s=b''): |
221 def __init__(self, s: object = b'') -> None: |
216 pass |
222 pass |
217 |
223 |
218 def __new__(cls, s=b''): |
224 def __new__(cls: Type[_Tbytestr], s: object = b'') -> _Tbytestr: |
219 if isinstance(s, bytestr): |
225 if isinstance(s, bytestr): |
220 return s |
226 return s |
221 if not isinstance( |
227 if not isinstance( |
222 s, (bytes, bytearray) |
228 s, (bytes, bytearray) |
223 ) and not builtins.hasattr( # hasattr-py3-only |
229 ) and not builtins.hasattr( # hasattr-py3-only |
224 s, u'__bytes__' |
230 s, u'__bytes__' |
225 ): |
231 ): |
226 s = str(s).encode('ascii') |
232 s = str(s).encode('ascii') |
227 return bytes.__new__(cls, s) |
233 return bytes.__new__(cls, s) |
228 |
234 |
229 def __getitem__(self, key): |
235 def __getitem__(self, key) -> bytes: |
230 s = bytes.__getitem__(self, key) |
236 s = bytes.__getitem__(self, key) |
231 if not isinstance(s, bytes): |
237 if not isinstance(s, bytes): |
232 s = bytechr(s) |
238 s = bytechr(s) |
233 return s |
239 return s |
234 |
240 |
235 def __iter__(self): |
241 def __iter__(self) -> Iterator[bytes]: |
236 return iterbytestr(bytes.__iter__(self)) |
242 return iterbytestr(bytes.__iter__(self)) |
237 |
243 |
238 def __repr__(self): |
244 def __repr__(self) -> str: |
239 return bytes.__repr__(self)[1:] # drop b'' |
245 return bytes.__repr__(self)[1:] # drop b'' |
240 |
246 |
241 |
247 |
242 def iterbytestr(s): |
248 def iterbytestr(s: Iterable[int]) -> Iterator[bytes]: |
243 """Iterate bytes as if it were a str object of Python 2""" |
249 """Iterate bytes as if it were a str object of Python 2""" |
244 return map(bytechr, s) |
250 return map(bytechr, s) |
245 |
251 |
246 |
252 |
247 def maybebytestr(s): |
253 def maybebytestr(s): |