|
19 | 19 | from six import binary_type, string_types, text_type, BytesIO |
20 | 20 | from string import * |
21 | 21 | from types import FunctionType, ModuleType |
22 | | -try: # Python 2 |
| 22 | +try: # Python2 |
23 | 23 | import __builtin__ as builtins |
24 | | - from inspect import getargspec as getfullargspec |
25 | | - from string import maketrans |
26 | | -except ImportError: # Python 3 |
| 24 | +except ImportError: |
27 | 25 | import builtins |
28 | | - from importlib import reload |
| 26 | +try: # Python2 |
29 | 27 | from inspect import getfullargspec |
| 28 | +except ImportError: |
| 29 | + from inspect import getargspec as getfullargspec |
| 30 | +try: # Python2 |
| 31 | + from string import maketrans |
| 32 | +except ImportError: |
30 | 33 | maketrans = str.maketrans |
| 34 | +try: # Python3 |
| 35 | + from importlib import reload |
| 36 | +except ImportError: |
| 37 | + pass |
31 | 38 |
|
32 | 39 |
|
33 | 40 | __all__ = ["add", "add_macro", "add_map", "b", "clear", "codecs", "decode", "encode", "ensure_str", "examples", "guess", |
34 | | - "isb", "generate_strings_from_regex", "get_alphabet_from_mask", "handle_error", "is_native", |
| 41 | + "isb", "generate_strings_from_regex", "get_alphabet_from_mask", "handle_error", "i2s", "is_native", |
35 | 42 | "list_categories", "list_encodings", "list_macros", "lookup", "maketrans", "os", "rank", "re", "register", |
36 | 43 | "remove", "reset", "s2i", "search", "stopfunc", "BytesIO", "_input", "_stripl", "CodecMacro", |
37 | 44 | "DARWIN", "LANG", "LINUX", "MASKS", "PY3", "UNIX", "WINDOWS"] |
|
75 | 82 | exc_name = lambda e: "".join(t.capitalize() for t in re.split(r"[-_+]", e)) |
76 | 83 |
|
77 | 84 |
|
| 85 | +def i2s(input): |
| 86 | + h = hex(input)[2:].rstrip("eL") |
| 87 | + return codecs.decode(h.zfill(len(h) + len(h) % 2), "hex") |
| 88 | + |
| 89 | + |
78 | 90 | class CodecMacro(tuple): |
79 | 91 | """Macro details when looking up the codec registry. """ |
80 | 92 | def __new__(cls, name): |
@@ -170,11 +182,11 @@ def __stdin_pipe(): |
170 | 182 |
|
171 | 183 | def _input(infile): |
172 | 184 | # handle input file or stdin |
| 185 | + c = b("") |
173 | 186 | if infile: |
174 | 187 | with open(infile, 'rb') as f: |
175 | 188 | c = f.read() |
176 | 189 | else: |
177 | | - c = b("") |
178 | 190 | for line in __stdin_pipe(): |
179 | 191 | c += line |
180 | 192 | return c |
|
0 commit comments