Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 1 | # |
| 2 | # Module for starting a process object using os.fork() or CreateProcess() |
| 3 | # |
| 4 | # multiprocessing/forking.py |
| 5 | # |
R. David Murray | 3fc969a | 2010-12-14 01:38:16 +0000 | [diff] [blame] | 6 | # Copyright (c) 2006-2008, R Oudkerk |
Richard Oudkerk | 3e268aa | 2012-04-30 12:13:55 +0100 | [diff] [blame] | 7 | # Licensed to PSF under a Contributor Agreement. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 8 | # |
| 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | import signal |
Richard Oudkerk | 7aaa1ef | 2013-02-26 12:39:57 +0000 | [diff] [blame] | 13 | import errno |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 14 | |
| 15 | from multiprocessing import util, process |
| 16 | |
Richard Oudkerk | 73d9a29 | 2012-06-14 15:30:10 +0100 | [diff] [blame] | 17 | __all__ = ['Popen', 'assert_spawning', 'duplicate', 'close', 'ForkingPickler'] |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 18 | |
| 19 | # |
| 20 | # Check that the current thread is spawning a child process |
| 21 | # |
| 22 | |
| 23 | def assert_spawning(self): |
| 24 | if not Popen.thread_is_spawning(): |
| 25 | raise RuntimeError( |
| 26 | '%s objects should only be shared between processes' |
| 27 | ' through inheritance' % type(self).__name__ |
| 28 | ) |
| 29 | |
| 30 | # |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 31 | # Try making some callable types picklable |
| 32 | # |
| 33 | |
Antoine Pitrou | 846fd30 | 2012-03-17 00:23:04 +0100 | [diff] [blame] | 34 | from pickle import Pickler |
| 35 | from copyreg import dispatch_table |
| 36 | |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 37 | class ForkingPickler(Pickler): |
Antoine Pitrou | 846fd30 | 2012-03-17 00:23:04 +0100 | [diff] [blame] | 38 | _extra_reducers = {} |
| 39 | def __init__(self, *args): |
| 40 | Pickler.__init__(self, *args) |
| 41 | self.dispatch_table = dispatch_table.copy() |
| 42 | self.dispatch_table.update(self._extra_reducers) |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 43 | @classmethod |
| 44 | def register(cls, type, reduce): |
Antoine Pitrou | 846fd30 | 2012-03-17 00:23:04 +0100 | [diff] [blame] | 45 | cls._extra_reducers[type] = reduce |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 46 | |
| 47 | def _reduce_method(m): |
| 48 | if m.__self__ is None: |
| 49 | return getattr, (m.__class__, m.__func__.__name__) |
| 50 | else: |
| 51 | return getattr, (m.__self__, m.__func__.__name__) |
| 52 | class _C: |
| 53 | def f(self): |
| 54 | pass |
| 55 | ForkingPickler.register(type(_C().f), _reduce_method) |
| 56 | |
| 57 | |
| 58 | def _reduce_method_descriptor(m): |
| 59 | return getattr, (m.__objclass__, m.__name__) |
| 60 | ForkingPickler.register(type(list.append), _reduce_method_descriptor) |
| 61 | ForkingPickler.register(type(int.__add__), _reduce_method_descriptor) |
| 62 | |
| 63 | try: |
| 64 | from functools import partial |
| 65 | except ImportError: |
| 66 | pass |
| 67 | else: |
| 68 | def _reduce_partial(p): |
| 69 | return _rebuild_partial, (p.func, p.args, p.keywords or {}) |
| 70 | def _rebuild_partial(func, args, keywords): |
| 71 | return partial(func, *args, **keywords) |
| 72 | ForkingPickler.register(partial, _reduce_partial) |
| 73 | |
| 74 | # |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 75 | # Unix |
| 76 | # |
| 77 | |
| 78 | if sys.platform != 'win32': |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 79 | duplicate = os.dup |
| 80 | close = os.close |
| 81 | |
| 82 | # |
| 83 | # We define a Popen class similar to the one from subprocess, but |
| 84 | # whose constructor takes a process object as its argument. |
| 85 | # |
| 86 | |
| 87 | class Popen(object): |
| 88 | |
| 89 | def __init__(self, process_obj): |
| 90 | sys.stdout.flush() |
| 91 | sys.stderr.flush() |
| 92 | self.returncode = None |
| 93 | |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 94 | r, w = os.pipe() |
| 95 | self.sentinel = r |
| 96 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 97 | self.pid = os.fork() |
| 98 | if self.pid == 0: |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 99 | os.close(r) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 100 | if 'random' in sys.modules: |
| 101 | import random |
| 102 | random.seed() |
| 103 | code = process_obj._bootstrap() |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 104 | os._exit(code) |
| 105 | |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 106 | # `w` will be closed when the child exits, at which point `r` |
| 107 | # will become ready for reading (using e.g. select()). |
| 108 | os.close(w) |
| 109 | util.Finalize(self, os.close, (r,)) |
| 110 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 111 | def poll(self, flag=os.WNOHANG): |
| 112 | if self.returncode is None: |
Richard Oudkerk | 7aaa1ef | 2013-02-26 12:39:57 +0000 | [diff] [blame] | 113 | while True: |
| 114 | try: |
| 115 | pid, sts = os.waitpid(self.pid, flag) |
| 116 | except os.error as e: |
| 117 | if e.errno == errno.EINTR: |
| 118 | continue |
| 119 | # Child process not yet created. See #1731717 |
| 120 | # e.errno == errno.ECHILD == 10 |
| 121 | return None |
| 122 | else: |
| 123 | break |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 124 | if pid == self.pid: |
| 125 | if os.WIFSIGNALED(sts): |
| 126 | self.returncode = -os.WTERMSIG(sts) |
| 127 | else: |
| 128 | assert os.WIFEXITED(sts) |
| 129 | self.returncode = os.WEXITSTATUS(sts) |
| 130 | return self.returncode |
| 131 | |
| 132 | def wait(self, timeout=None): |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 133 | if self.returncode is None: |
| 134 | if timeout is not None: |
Richard Oudkerk | 59d5404 | 2012-05-10 16:11:12 +0100 | [diff] [blame] | 135 | from .connection import wait |
| 136 | if not wait([self.sentinel], timeout): |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 137 | return None |
Richard Oudkerk | 59d5404 | 2012-05-10 16:11:12 +0100 | [diff] [blame] | 138 | # This shouldn't block if wait() returned successfully. |
Antoine Pitrou | 176f07d | 2011-06-06 19:35:31 +0200 | [diff] [blame] | 139 | return self.poll(os.WNOHANG if timeout == 0.0 else 0) |
| 140 | return self.returncode |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 141 | |
| 142 | def terminate(self): |
| 143 | if self.returncode is None: |
| 144 | try: |
| 145 | os.kill(self.pid, signal.SIGTERM) |
Florent Xicluna | 04842a8 | 2011-11-11 20:05:50 +0100 | [diff] [blame] | 146 | except OSError: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 147 | if self.wait(timeout=0.1) is None: |
| 148 | raise |
| 149 | |
| 150 | @staticmethod |
| 151 | def thread_is_spawning(): |
| 152 | return False |
| 153 | |
| 154 | # |
| 155 | # Windows |
| 156 | # |
| 157 | |
| 158 | else: |
| 159 | import _thread |
| 160 | import msvcrt |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 161 | import _winapi |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 162 | |
Florent Xicluna | 04842a8 | 2011-11-11 20:05:50 +0100 | [diff] [blame] | 163 | from pickle import load, HIGHEST_PROTOCOL |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 164 | |
Amaury Forgeot d'Arc | 949d47d | 2008-08-19 21:30:55 +0000 | [diff] [blame] | 165 | def dump(obj, file, protocol=None): |
| 166 | ForkingPickler(file, protocol).dump(obj) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 167 | |
| 168 | # |
| 169 | # |
| 170 | # |
| 171 | |
| 172 | TERMINATE = 0x10000 |
| 173 | WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False)) |
brian.curtin | e2f2998 | 2011-04-11 17:56:23 -0500 | [diff] [blame] | 174 | WINSERVICE = sys.executable.lower().endswith("pythonservice.exe") |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 176 | close = _winapi.CloseHandle |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 177 | |
| 178 | # |
| 179 | # _python_exe is the assumed path to the python executable. |
| 180 | # People embedding Python want to modify it. |
| 181 | # |
| 182 | |
brian.curtin | e2f2998 | 2011-04-11 17:56:23 -0500 | [diff] [blame] | 183 | if WINSERVICE: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 184 | _python_exe = os.path.join(sys.exec_prefix, 'python.exe') |
| 185 | else: |
| 186 | _python_exe = sys.executable |
| 187 | |
| 188 | def set_executable(exe): |
| 189 | global _python_exe |
| 190 | _python_exe = exe |
| 191 | |
| 192 | # |
| 193 | # |
| 194 | # |
| 195 | |
| 196 | def duplicate(handle, target_process=None, inheritable=False): |
| 197 | if target_process is None: |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 198 | target_process = _winapi.GetCurrentProcess() |
| 199 | return _winapi.DuplicateHandle( |
| 200 | _winapi.GetCurrentProcess(), handle, target_process, |
| 201 | 0, inheritable, _winapi.DUPLICATE_SAME_ACCESS |
| 202 | ) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 203 | |
| 204 | # |
| 205 | # We define a Popen class similar to the one from subprocess, but |
| 206 | # whose constructor takes a process object as its argument. |
| 207 | # |
| 208 | |
| 209 | class Popen(object): |
| 210 | ''' |
| 211 | Start a subprocess to run the code of a process object |
| 212 | ''' |
| 213 | _tls = _thread._local() |
| 214 | |
| 215 | def __init__(self, process_obj): |
Richard Oudkerk | bd7b5dd | 2012-06-04 18:59:10 +0100 | [diff] [blame] | 216 | cmd = ' '.join('"%s"' % x for x in get_command_line()) |
| 217 | prep_data = get_preparation_data(process_obj._name) |
| 218 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 219 | # create pipe for communication with child |
| 220 | rfd, wfd = os.pipe() |
| 221 | |
| 222 | # get handle for read end of the pipe and make it inheritable |
| 223 | rhandle = duplicate(msvcrt.get_osfhandle(rfd), inheritable=True) |
| 224 | os.close(rfd) |
| 225 | |
Richard Oudkerk | bd7b5dd | 2012-06-04 18:59:10 +0100 | [diff] [blame] | 226 | with open(wfd, 'wb', closefd=True) as to_child: |
| 227 | # start process |
| 228 | try: |
| 229 | hp, ht, pid, tid = _winapi.CreateProcess( |
| 230 | _python_exe, cmd + (' %s' % rhandle), |
| 231 | None, None, 1, 0, None, None, None |
| 232 | ) |
| 233 | _winapi.CloseHandle(ht) |
| 234 | finally: |
| 235 | close(rhandle) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 236 | |
Richard Oudkerk | bd7b5dd | 2012-06-04 18:59:10 +0100 | [diff] [blame] | 237 | # set attributes of self |
| 238 | self.pid = pid |
| 239 | self.returncode = None |
| 240 | self._handle = hp |
| 241 | self.sentinel = int(hp) |
Richard Oudkerk | 021f4c2 | 2012-11-15 18:16:35 +0000 | [diff] [blame] | 242 | util.Finalize(self, _winapi.CloseHandle, (self.sentinel,)) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 243 | |
Richard Oudkerk | bd7b5dd | 2012-06-04 18:59:10 +0100 | [diff] [blame] | 244 | # send information to child |
| 245 | Popen._tls.process_handle = int(hp) |
| 246 | try: |
| 247 | dump(prep_data, to_child, HIGHEST_PROTOCOL) |
| 248 | dump(process_obj, to_child, HIGHEST_PROTOCOL) |
| 249 | finally: |
| 250 | del Popen._tls.process_handle |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 251 | |
| 252 | @staticmethod |
| 253 | def thread_is_spawning(): |
| 254 | return getattr(Popen._tls, 'process_handle', None) is not None |
| 255 | |
| 256 | @staticmethod |
| 257 | def duplicate_for_child(handle): |
| 258 | return duplicate(handle, Popen._tls.process_handle) |
| 259 | |
| 260 | def wait(self, timeout=None): |
| 261 | if self.returncode is None: |
| 262 | if timeout is None: |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 263 | msecs = _winapi.INFINITE |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 264 | else: |
| 265 | msecs = max(0, int(timeout * 1000 + 0.5)) |
| 266 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 267 | res = _winapi.WaitForSingleObject(int(self._handle), msecs) |
| 268 | if res == _winapi.WAIT_OBJECT_0: |
| 269 | code = _winapi.GetExitCodeProcess(self._handle) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 270 | if code == TERMINATE: |
| 271 | code = -signal.SIGTERM |
| 272 | self.returncode = code |
| 273 | |
| 274 | return self.returncode |
| 275 | |
| 276 | def poll(self): |
| 277 | return self.wait(timeout=0) |
| 278 | |
| 279 | def terminate(self): |
| 280 | if self.returncode is None: |
| 281 | try: |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 282 | _winapi.TerminateProcess(int(self._handle), TERMINATE) |
Richard Oudkerk | b303580 | 2012-08-01 17:44:18 +0100 | [diff] [blame] | 283 | except OSError: |
| 284 | if self.wait(timeout=1.0) is None: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 285 | raise |
| 286 | |
| 287 | # |
| 288 | # |
| 289 | # |
| 290 | |
| 291 | def is_forking(argv): |
| 292 | ''' |
| 293 | Return whether commandline indicates we are forking |
| 294 | ''' |
| 295 | if len(argv) >= 2 and argv[1] == '--multiprocessing-fork': |
| 296 | assert len(argv) == 3 |
| 297 | return True |
| 298 | else: |
| 299 | return False |
| 300 | |
| 301 | |
| 302 | def freeze_support(): |
| 303 | ''' |
| 304 | Run code for process object if this in not the main process |
| 305 | ''' |
| 306 | if is_forking(sys.argv): |
| 307 | main() |
| 308 | sys.exit() |
| 309 | |
| 310 | |
| 311 | def get_command_line(): |
| 312 | ''' |
| 313 | Returns prefix of command line used for spawning a child process |
| 314 | ''' |
Richard Oudkerk | e88a244 | 2012-08-14 11:41:32 +0100 | [diff] [blame] | 315 | if getattr(process.current_process(), '_inheriting', False): |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 316 | raise RuntimeError(''' |
| 317 | Attempt to start a new process before the current process |
| 318 | has finished its bootstrapping phase. |
| 319 | |
| 320 | This probably means that you are on Windows and you have |
| 321 | forgotten to use the proper idiom in the main module: |
| 322 | |
| 323 | if __name__ == '__main__': |
| 324 | freeze_support() |
| 325 | ... |
| 326 | |
| 327 | The "freeze_support()" line can be omitted if the program |
| 328 | is not going to be frozen to produce a Windows executable.''') |
| 329 | |
| 330 | if getattr(sys, 'frozen', False): |
| 331 | return [sys.executable, '--multiprocessing-fork'] |
| 332 | else: |
| 333 | prog = 'from multiprocessing.forking import main; main()' |
Richard Oudkerk | 77c84f2 | 2012-05-18 14:28:02 +0100 | [diff] [blame] | 334 | opts = util._args_from_interpreter_flags() |
| 335 | return [_python_exe] + opts + ['-c', prog, '--multiprocessing-fork'] |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | def main(): |
| 339 | ''' |
| 340 | Run code specifed by data received over pipe |
| 341 | ''' |
| 342 | assert is_forking(sys.argv) |
| 343 | |
| 344 | handle = int(sys.argv[-1]) |
| 345 | fd = msvcrt.open_osfhandle(handle, os.O_RDONLY) |
| 346 | from_parent = os.fdopen(fd, 'rb') |
| 347 | |
| 348 | process.current_process()._inheriting = True |
| 349 | preparation_data = load(from_parent) |
| 350 | prepare(preparation_data) |
| 351 | self = load(from_parent) |
| 352 | process.current_process()._inheriting = False |
| 353 | |
| 354 | from_parent.close() |
| 355 | |
| 356 | exitcode = self._bootstrap() |
Richard Oudkerk | 73d9a29 | 2012-06-14 15:30:10 +0100 | [diff] [blame] | 357 | sys.exit(exitcode) |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | def get_preparation_data(name): |
| 361 | ''' |
| 362 | Return info about parent needed by child to unpickle process object |
| 363 | ''' |
| 364 | from .util import _logger, _log_to_stderr |
| 365 | |
| 366 | d = dict( |
| 367 | name=name, |
| 368 | sys_path=sys.path, |
| 369 | sys_argv=sys.argv, |
| 370 | log_to_stderr=_log_to_stderr, |
| 371 | orig_dir=process.ORIGINAL_DIR, |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 372 | authkey=process.current_process().authkey, |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 373 | ) |
| 374 | |
| 375 | if _logger is not None: |
| 376 | d['log_level'] = _logger.getEffectiveLevel() |
| 377 | |
brian.curtin | e2f2998 | 2011-04-11 17:56:23 -0500 | [diff] [blame] | 378 | if not WINEXE and not WINSERVICE: |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 379 | main_path = getattr(sys.modules['__main__'], '__file__', None) |
| 380 | if not main_path and sys.argv[0] not in ('', '-c'): |
| 381 | main_path = sys.argv[0] |
| 382 | if main_path is not None: |
| 383 | if not os.path.isabs(main_path) and \ |
| 384 | process.ORIGINAL_DIR is not None: |
| 385 | main_path = os.path.join(process.ORIGINAL_DIR, main_path) |
| 386 | d['main_path'] = os.path.normpath(main_path) |
| 387 | |
| 388 | return d |
| 389 | |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 390 | # |
| 391 | # Prepare current process |
| 392 | # |
| 393 | |
| 394 | old_main_modules = [] |
| 395 | |
| 396 | def prepare(data): |
| 397 | ''' |
| 398 | Try to get current process ready to unpickle process object |
| 399 | ''' |
| 400 | old_main_modules.append(sys.modules['__main__']) |
| 401 | |
| 402 | if 'name' in data: |
Benjamin Peterson | 58ea9fe | 2008-08-19 19:17:39 +0000 | [diff] [blame] | 403 | process.current_process().name = data['name'] |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 404 | |
| 405 | if 'authkey' in data: |
| 406 | process.current_process()._authkey = data['authkey'] |
| 407 | |
| 408 | if 'log_to_stderr' in data and data['log_to_stderr']: |
| 409 | util.log_to_stderr() |
| 410 | |
| 411 | if 'log_level' in data: |
| 412 | util.get_logger().setLevel(data['log_level']) |
| 413 | |
| 414 | if 'sys_path' in data: |
| 415 | sys.path = data['sys_path'] |
| 416 | |
| 417 | if 'sys_argv' in data: |
| 418 | sys.argv = data['sys_argv'] |
| 419 | |
| 420 | if 'dir' in data: |
| 421 | os.chdir(data['dir']) |
| 422 | |
| 423 | if 'orig_dir' in data: |
| 424 | process.ORIGINAL_DIR = data['orig_dir'] |
| 425 | |
| 426 | if 'main_path' in data: |
Nick Coghlan | 793ee1f | 2011-01-30 01:24:08 +0000 | [diff] [blame] | 427 | # XXX (ncoghlan): The following code makes several bogus |
| 428 | # assumptions regarding the relationship between __file__ |
| 429 | # and a module's real name. See PEP 302 and issue #10845 |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 430 | main_path = data['main_path'] |
| 431 | main_name = os.path.splitext(os.path.basename(main_path))[0] |
| 432 | if main_name == '__init__': |
| 433 | main_name = os.path.basename(os.path.dirname(main_path)) |
| 434 | |
Nick Coghlan | 793ee1f | 2011-01-30 01:24:08 +0000 | [diff] [blame] | 435 | if main_name == '__main__': |
| 436 | main_module = sys.modules['__main__'] |
| 437 | main_module.__file__ = main_path |
| 438 | elif main_name != 'ipython': |
| 439 | # Main modules not actually called __main__.py may |
| 440 | # contain additional code that should still be executed |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 441 | import imp |
| 442 | |
| 443 | if main_path is None: |
| 444 | dirs = None |
| 445 | elif os.path.basename(main_path).startswith('__init__.py'): |
| 446 | dirs = [os.path.dirname(os.path.dirname(main_path))] |
| 447 | else: |
| 448 | dirs = [os.path.dirname(main_path)] |
| 449 | |
| 450 | assert main_name not in sys.modules, main_name |
| 451 | file, path_name, etc = imp.find_module(main_name, dirs) |
| 452 | try: |
| 453 | # We would like to do "imp.load_module('__main__', ...)" |
| 454 | # here. However, that would cause 'if __name__ == |
| 455 | # "__main__"' clauses to be executed. |
| 456 | main_module = imp.load_module( |
| 457 | '__parents_main__', file, path_name, etc |
| 458 | ) |
| 459 | finally: |
| 460 | if file: |
| 461 | file.close() |
| 462 | |
| 463 | sys.modules['__main__'] = main_module |
| 464 | main_module.__name__ = '__main__' |
| 465 | |
| 466 | # Try to make the potentially picklable objects in |
| 467 | # sys.modules['__main__'] realize they are in the main |
| 468 | # module -- somewhat ugly. |
| 469 | for obj in list(main_module.__dict__.values()): |
| 470 | try: |
| 471 | if obj.__module__ == '__parents_main__': |
| 472 | obj.__module__ = '__main__' |
| 473 | except Exception: |
| 474 | pass |