47 try: |
47 try: |
48 range = xrange |
48 range = xrange |
49 except NameError: |
49 except NameError: |
50 range = range |
50 range = range |
51 |
51 |
|
52 # shlex.quote is new, but there's an undocumented implementation in "pipes", |
|
53 # who knew!? |
|
54 try: |
|
55 from shlex import quote as shlex_quote |
|
56 except ImportError: |
|
57 # Useful function, available under a different (undocumented) name |
|
58 # in Python versions earlier than 3.3. |
|
59 from pipes import quote as shlex_quote |
|
60 |
52 # A function to iterate listlessly over a dict's items. |
61 # A function to iterate listlessly over a dict's items. |
53 try: |
62 try: |
54 {}.iteritems |
63 {}.iteritems |
55 except AttributeError: |
64 except AttributeError: |
56 def iitems(d): |
65 def iitems(d): |
82 |
91 |
83 def binary_bytes(byte_values): |
92 def binary_bytes(byte_values): |
84 """Produce a byte string with the ints from `byte_values`.""" |
93 """Produce a byte string with the ints from `byte_values`.""" |
85 return bytes(byte_values) |
94 return bytes(byte_values) |
86 |
95 |
87 def byte_to_int(byte_value): |
|
88 """Turn an element of a bytes object into an int.""" |
|
89 return byte_value |
|
90 |
|
91 def bytes_to_ints(bytes_value): |
96 def bytes_to_ints(bytes_value): |
92 """Turn a bytes object into a sequence of ints.""" |
97 """Turn a bytes object into a sequence of ints.""" |
93 # In Python 3, iterating bytes gives ints. |
98 # In Python 3, iterating bytes gives ints. |
94 return bytes_value |
99 return bytes_value |
95 |
100 |
99 return s |
104 return s |
100 |
105 |
101 def binary_bytes(byte_values): |
106 def binary_bytes(byte_values): |
102 """Produce a byte string with the ints from `byte_values`.""" |
107 """Produce a byte string with the ints from `byte_values`.""" |
103 return "".join(chr(b) for b in byte_values) |
108 return "".join(chr(b) for b in byte_values) |
104 |
|
105 def byte_to_int(byte_value): |
|
106 """Turn an element of a bytes object into an int.""" |
|
107 return ord(byte_value) |
|
108 |
109 |
109 def bytes_to_ints(bytes_value): |
110 def bytes_to_ints(bytes_value): |
110 """Turn a bytes object into a sequence of ints.""" |
111 """Turn a bytes object into a sequence of ints.""" |
111 for byte in bytes_value: |
112 for byte in bytes_value: |
112 yield ord(byte) |
113 yield ord(byte) |
140 PYC_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER |
141 PYC_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER |
141 except AttributeError: |
142 except AttributeError: |
142 PYC_MAGIC_NUMBER = imp.get_magic() |
143 PYC_MAGIC_NUMBER = imp.get_magic() |
143 |
144 |
144 |
145 |
145 def import_local_file(modname): |
146 def import_local_file(modname, modfile=None): |
146 """Import a local file as a module. |
147 """Import a local file as a module. |
147 |
148 |
148 Opens a file in the current directory named `modname`.py, imports it |
149 Opens a file in the current directory named `modname`.py, imports it |
149 as `modname`, and returns the module object. |
150 as `modname`, and returns the module object. `modfile` is the file to |
|
151 import if it isn't in the current directory. |
150 |
152 |
151 """ |
153 """ |
152 try: |
154 try: |
153 from importlib.machinery import SourceFileLoader |
155 from importlib.machinery import SourceFileLoader |
154 except ImportError: |
156 except ImportError: |
155 SourceFileLoader = None |
157 SourceFileLoader = None |
156 |
158 |
157 modfile = modname + '.py' |
159 if modfile is None: |
|
160 modfile = modname + '.py' |
158 if SourceFileLoader: |
161 if SourceFileLoader: |
159 mod = SourceFileLoader(modname, modfile).load_module() |
162 mod = SourceFileLoader(modname, modfile).load_module() |
160 else: |
163 else: |
161 for suff in imp.get_suffixes(): # pragma: part covered |
164 for suff in imp.get_suffixes(): # pragma: part covered |
162 if suff[0] == '.py': |
165 if suff[0] == '.py': |