|
8 | 8 |
|
9 | 9 | import os
|
10 | 10 | import sys
|
| 11 | +import tokenize |
11 | 12 |
|
12 | 13 | from distutils.debug import DEBUG
|
13 | 14 | from distutils.errors import *
|
@@ -144,29 +145,41 @@ class found in 'cmdclass' is used in place of the default, which is
|
144 | 145 |
|
145 | 146 | # And finally, run all the commands found on the command line.
|
146 | 147 | if ok:
|
147 |
| - try: |
148 |
| - dist.run_commands() |
149 |
| - except KeyboardInterrupt: |
150 |
| - raise SystemExit("interrupted") |
151 |
| - except OSError as exc: |
152 |
| - if DEBUG: |
153 |
| - sys.stderr.write("error: %s\n" % (exc,)) |
154 |
| - raise |
155 |
| - else: |
156 |
| - raise SystemExit("error: %s" % (exc,)) |
157 |
| - |
158 |
| - except (DistutilsError, |
159 |
| - CCompilerError) as msg: |
160 |
| - if DEBUG: |
161 |
| - raise |
162 |
| - else: |
163 |
| - raise SystemExit("error: " + str(msg)) |
| 148 | + return run_commands(dist) |
164 | 149 |
|
165 | 150 | return dist
|
166 | 151 |
|
167 | 152 | # setup ()
|
168 | 153 |
|
169 | 154 |
|
| 155 | +def run_commands (dist): |
| 156 | + """Given a Distribution object run all the commands, |
| 157 | + raising ``SystemExit`` errors in the case of failure. |
| 158 | +
|
| 159 | + This function assumes that either ``sys.argv`` or ``dist.script_args`` |
| 160 | + is already set accordingly. |
| 161 | + """ |
| 162 | + try: |
| 163 | + dist.run_commands() |
| 164 | + except KeyboardInterrupt: |
| 165 | + raise SystemExit("interrupted") |
| 166 | + except OSError as exc: |
| 167 | + if DEBUG: |
| 168 | + sys.stderr.write("error: %s\n" % (exc,)) |
| 169 | + raise |
| 170 | + else: |
| 171 | + raise SystemExit("error: %s" % (exc,)) |
| 172 | + |
| 173 | + except (DistutilsError, |
| 174 | + CCompilerError) as msg: |
| 175 | + if DEBUG: |
| 176 | + raise |
| 177 | + else: |
| 178 | + raise SystemExit("error: " + str(msg)) |
| 179 | + |
| 180 | + return dist |
| 181 | + |
| 182 | + |
170 | 183 | def run_setup (script_name, script_args=None, stop_after="run"):
|
171 | 184 | """Run a setup script in a somewhat controlled environment, and
|
172 | 185 | return the Distribution instance that drives things. This is useful
|
@@ -205,14 +218,16 @@ def run_setup (script_name, script_args=None, stop_after="run"):
|
205 | 218 | _setup_stop_after = stop_after
|
206 | 219 |
|
207 | 220 | save_argv = sys.argv.copy()
|
208 |
| - g = {'__file__': script_name} |
| 221 | + g = {'__file__': script_name, '__name__': '__main__'} |
209 | 222 | try:
|
210 | 223 | try:
|
211 | 224 | sys.argv[0] = script_name
|
212 | 225 | if script_args is not None:
|
213 | 226 | sys.argv[1:] = script_args
|
214 |
| - with open(script_name, 'rb') as f: |
215 |
| - exec(f.read(), g) |
| 227 | + # tokenize.open supports automatic encoding detection |
| 228 | + with tokenize.open(script_name) as f: |
| 229 | + code = f.read().replace(r'\r\n', r'\n') |
| 230 | + exec(code, g) |
216 | 231 | finally:
|
217 | 232 | sys.argv = save_argv
|
218 | 233 | _setup_stop_after = None
|
|
0 commit comments