annotate Lib/runpy.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 1b8f08c4efd5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
1 """runpy.py - locating and running Python code using the module namespace
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
2
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
3 Provides support for locating and running Python scripts using the Python
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
4 module namespace instead of the native filesystem.
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
5
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
6 This allows Python code to play nicely with non-filesystem based PEP 302
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
7 importers when locating support scripts as well as when importing modules.
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
8 """
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
9 # Written by Nick Coghlan <ncoghlan at gmail.com>
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
10 # to implement PEP 338 (Executing Modules as Scripts)
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
11
76582
7025ee00dbf6 Issue #14605: Use None in sys.path_importer_cache to represent no
Brett Cannon <brett@python.org>
parents: 76566
diff changeset
12
76566
57d558f1904d Issue #14605: Make explicit the entries on sys.path_hooks that used to
Brett Cannon <brett@python.org>
parents: 71165
diff changeset
13 import os
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
14 import sys
78195
4431dc4bb770 Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents: 78109
diff changeset
15 import importlib.machinery # importlib first so we can test #15386 via -m
84149
8fff5554125d Issue #17177: switch from imp.new_module to types.ModuleType for runpy
Brett Cannon <brett@python.org>
parents: 80929
diff changeset
16 import types
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
17 from pkgutil import read_code, get_loader, get_importer
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
18
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
19 __all__ = [
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
20 "run_module", "run_path",
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
21 ]
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
22
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
23 class _TempModule(object):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
24 """Temporarily replace a module in sys.modules with an empty namespace"""
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
25 def __init__(self, mod_name):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
26 self.mod_name = mod_name
84149
8fff5554125d Issue #17177: switch from imp.new_module to types.ModuleType for runpy
Brett Cannon <brett@python.org>
parents: 80929
diff changeset
27 self.module = types.ModuleType(mod_name)
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
28 self._saved_module = []
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
29
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
30 def __enter__(self):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
31 mod_name = self.mod_name
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
32 try:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
33 self._saved_module.append(sys.modules[mod_name])
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
34 except KeyError:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
35 pass
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
36 sys.modules[mod_name] = self.module
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
37 return self
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
38
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
39 def __exit__(self, *args):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
40 if self._saved_module:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
41 sys.modules[self.mod_name] = self._saved_module[0]
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
42 else:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
43 del sys.modules[self.mod_name]
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
44 self._saved_module = []
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
45
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
46 class _ModifiedArgv0(object):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
47 def __init__(self, value):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
48 self.value = value
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
49 self._saved_value = self._sentinel = object()
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
50
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
51 def __enter__(self):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
52 if self._saved_value is not self._sentinel:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
53 raise RuntimeError("Already preserving saved value")
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
54 self._saved_value = sys.argv[0]
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
55 sys.argv[0] = self.value
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
56
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
57 def __exit__(self, *args):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
58 self.value = self._sentinel
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
59 sys.argv[0] = self._saved_value
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
60
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
61 def _run_code(code, run_globals, init_globals=None,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
62 mod_name=None, mod_fname=None,
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
63 mod_loader=None, pkg_name=None):
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
64 """Helper to run code in nominated namespace"""
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
65 if init_globals is not None:
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
66 run_globals.update(init_globals)
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
67 run_globals.update(__name__ = mod_name,
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
68 __file__ = mod_fname,
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 57828
diff changeset
69 __cached__ = None,
78094
3b05cf877124 Close #15230: runpy.run_path now sets __package__ correctly. Also refactored the runpy tests to use a more systematic approach
Nick Coghlan <ncoghlan@gmail.com>
parents: 71164
diff changeset
70 __doc__ = None,
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
71 __loader__ = mod_loader,
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
72 __package__ = pkg_name)
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 37879
diff changeset
73 exec(code, run_globals)
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
74 return run_globals
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
75
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
76 def _run_module_code(code, init_globals=None,
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
77 mod_name=None, mod_fname=None,
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
78 mod_loader=None, pkg_name=None):
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
79 """Helper to run code in new namespace with sys modified"""
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
80 with _TempModule(mod_name) as temp_module, _ModifiedArgv0(mod_fname):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
81 mod_globals = temp_module.module.__dict__
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
82 _run_code(code, mod_globals, init_globals,
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
83 mod_name, mod_fname, mod_loader, pkg_name)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
84 # Copy the globals of the temporary module, as they
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
85 # may be cleared when the temporary module goes away
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
86 return mod_globals.copy()
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
87
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
88
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37875
diff changeset
89 # This helper is needed due to a missing component in the PEP 302
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37875
diff changeset
90 # loader protocol (specifically, "get_filename" is non-standard)
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45435
diff changeset
91 # Since we can't introduce new features in maintenance releases,
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45435
diff changeset
92 # support was added to zipimporter under the name '_get_filename'
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37875
diff changeset
93 def _get_filename(loader, mod_name):
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45435
diff changeset
94 for attr in ("get_filename", "_get_filename"):
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45435
diff changeset
95 meth = getattr(loader, attr, None)
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45435
diff changeset
96 if meth is not None:
76566
57d558f1904d Issue #14605: Make explicit the entries on sys.path_hooks that used to
Brett Cannon <brett@python.org>
parents: 71165
diff changeset
97 return os.path.abspath(meth(mod_name))
50164
252927a2f32d Merged revisions 67753 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45435
diff changeset
98 return None
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37875
diff changeset
99
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
100 # Helper to get the loader, code and filename for a module
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
101 def _get_module_details(mod_name):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37875
diff changeset
102 loader = get_loader(mod_name)
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
103 if loader is None:
42466
3fc3cedc49ca Merged revisions 56753-56781 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
104 raise ImportError("No module named %s" % mod_name)
3fc3cedc49ca Merged revisions 56753-56781 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
105 if loader.is_package(mod_name):
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
106 if mod_name == "__main__" or mod_name.endswith(".__main__"):
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
107 raise ImportError("Cannot use package as __main__ module")
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
108 try:
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
109 pkg_main_name = mod_name + ".__main__"
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
110 return _get_module_details(pkg_main_name)
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
111 except ImportError as e:
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
112 raise ImportError(("%s; %r is a package and cannot " +
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
113 "be directly executed") %(e, mod_name))
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
114 code = loader.get_code(mod_name)
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
115 if code is None:
42466
3fc3cedc49ca Merged revisions 56753-56781 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
116 raise ImportError("No code object available for %s" % mod_name)
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
117 filename = _get_filename(loader, mod_name)
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
118 return mod_name, loader, code, filename
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
119
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
120 # XXX ncoghlan: Should this be documented and made public?
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
121 # (Current thoughts: don't repeat the mistake that lead to its
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
122 # creation when run_module() no longer met the needs of
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
123 # mainmodule.c, but couldn't be changed because it was public)
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
124 def _run_module_as_main(mod_name, alter_argv=True):
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
125 """Runs the designated module in the __main__ namespace
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
126
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
127 Note that the executed module will have full access to the
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
128 __main__ namespace. If this is not desirable, the run_module()
57828
cb05ccd75996 Merged revisions 76934-76935 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 57455
diff changeset
129 function should be used to run the module code in a fresh namespace.
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
130
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
131 At the very least, these variables in __main__ will be overwritten:
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
132 __name__
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
133 __file__
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 57828
diff changeset
134 __cached__
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
135 __loader__
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
136 __package__
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
137 """
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44129
diff changeset
138 try:
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
139 if alter_argv or mod_name != "__main__": # i.e. -m switch
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
140 mod_name, loader, code, fname = _get_module_details(mod_name)
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
141 else: # i.e. directory or zipfile execution
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
142 mod_name, loader, code, fname = _get_main_module_details()
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44129
diff changeset
143 except ImportError as exc:
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
144 # Try to provide a good error message
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
145 # for directories, zip files and the -m switch
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
146 if alter_argv:
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
147 # For -m switch, just display the exception
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
148 info = str(exc)
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
149 else:
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
150 # For directories/zipfiles, let the user
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
151 # know what the code was looking for
57455
e6133d154f98 fix test checking for error message
Benjamin Peterson <benjamin@python.org>
parents: 57450
diff changeset
152 info = "can't find '__main__' module in %r" % sys.argv[0]
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
153 msg = "%s: %s" % (sys.executable, info)
45435
42b3b117391d Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900-60931,60933-60958 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44129
diff changeset
154 sys.exit(msg)
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
155 pkg_name = mod_name.rpartition('.')[0]
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
156 main_globals = sys.modules["__main__"].__dict__
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
157 if alter_argv:
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
158 sys.argv[0] = fname
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
159 return _run_code(code, main_globals, None,
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
160 "__main__", fname, loader, pkg_name)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
161
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
162 def run_module(mod_name, init_globals=None,
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
163 run_name=None, alter_sys=False):
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
164 """Execute a module's code without importing it
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
165
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
166 Returns the resulting top level namespace dictionary
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
167 """
51452
2d354f4d77bc Merged revisions 69419-69420 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50164
diff changeset
168 mod_name, loader, code, fname = _get_module_details(mod_name)
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
169 if run_name is None:
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
170 run_name = mod_name
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
171 pkg_name = mod_name.rpartition('.')[0]
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
172 if alter_sys:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
173 return _run_module_code(code, init_globals, run_name,
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
174 fname, loader, pkg_name)
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
175 else:
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
176 # Leave the sys module alone
44129
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
177 return _run_code(code, {}, init_globals, run_name,
b637349336fd Merged revisions 59275-59303 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 42954
diff changeset
178 fname, loader, pkg_name)
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
179
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
180 def _get_main_module_details():
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
181 # Helper that gives a nicer error message when attempting to
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
182 # execute a zipfile or directory by invoking __main__.py
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
183 # Also moves the standard __main__ out of the way so that the
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
184 # preexisting __loader__ entry doesn't cause issues
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
185 main_name = "__main__"
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
186 saved_main = sys.modules[main_name]
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
187 del sys.modules[main_name]
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
188 try:
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
189 return _get_module_details(main_name)
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
190 except ImportError as exc:
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
191 if main_name in str(exc):
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
192 raise ImportError("can't find %r module in %r" %
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
193 (main_name, sys.path[0])) from exc
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
194 raise
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
195 finally:
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
196 sys.modules[main_name] = saved_main
57450
0db67153d25f Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 57291
diff changeset
197
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
198
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
199 def _get_code_from_file(run_name, fname):
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
200 # Check for a compiled file first
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
201 with open(fname, "rb") as f:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
202 code = read_code(f)
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
203 if code is None:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
204 # That didn't work, so try it as normal source code
71164
cd1759711357 Issue #12451: runpy: run_path() now opens the Python script in binary mode,
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65237
diff changeset
205 with open(fname, "rb") as f:
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
206 code = compile(f.read(), fname, 'exec')
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
207 loader = importlib.machinery.SourceFileLoader(run_name, fname)
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
208 else:
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
209 loader = importlib.machinery.SourcelessFileLoader(run_name, fname)
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
210 return code, loader
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
211
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
212 def run_path(path_name, init_globals=None, run_name=None):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
213 """Execute code located at the specified filesystem location
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
214
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
215 Returns the resulting top level namespace dictionary
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
216
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
217 The file path may refer directly to a Python script (i.e.
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
218 one that could be directly executed with execfile) or else
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
219 it may refer to a zipfile or directory containing a top
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
220 level __main__.py script.
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
221 """
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
222 if run_name is None:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
223 run_name = "<run_path>"
78094
3b05cf877124 Close #15230: runpy.run_path now sets __package__ correctly. Also refactored the runpy tests to use a more systematic approach
Nick Coghlan <ncoghlan@gmail.com>
parents: 71164
diff changeset
224 pkg_name = run_name.rpartition(".")[0]
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
225 importer = get_importer(path_name)
84167
1b8f08c4efd5 Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents: 84149
diff changeset
226 # Trying to avoid importing imp so as to not consume the deprecation warning.
1b8f08c4efd5 Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents: 84149
diff changeset
227 is_NullImporter = False
1b8f08c4efd5 Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents: 84149
diff changeset
228 if type(importer).__module__ == 'imp':
1b8f08c4efd5 Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents: 84149
diff changeset
229 if type(importer).__name__ == 'NullImporter':
1b8f08c4efd5 Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents: 84149
diff changeset
230 is_NullImporter = True
1b8f08c4efd5 Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents: 84149
diff changeset
231 if isinstance(importer, type(None)) or is_NullImporter:
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
232 # Not a valid sys.path entry, so run the code directly
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
233 # execfile() doesn't help as we want to allow compiled files
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
234 code, mod_loader = _get_code_from_file(run_name, path_name)
78094
3b05cf877124 Close #15230: runpy.run_path now sets __package__ correctly. Also refactored the runpy tests to use a more systematic approach
Nick Coghlan <ncoghlan@gmail.com>
parents: 71164
diff changeset
235 return _run_module_code(code, init_globals, run_name, path_name,
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
236 mod_loader, pkg_name)
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
237 else:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
238 # Importer is defined for path, so add it to
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
239 # the start of sys.path
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
240 sys.path.insert(0, path_name)
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
241 try:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
242 # Here's where things are a little different from the run_module
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
243 # case. There, we only had to replace the module in sys while the
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
244 # code was running and doing so was somewhat optional. Here, we
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
245 # have no choice and we have to remove it even while we read the
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
246 # code. If we don't do this, a __loader__ attribute in the
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
247 # existing __main__ module may prevent location of the new module.
78107
3987667bf98f Take the first step in resolving the messy pkgutil vs importlib edge cases by basing pkgutil explicitly on importlib, deprecating its internal import emulation and setting __main__.__loader__ correctly so that runpy still works (Affects #15343, #15314, #15357)
Nick Coghlan <ncoghlan@gmail.com>
parents: 78095
diff changeset
248 mod_name, loader, code, fname = _get_main_module_details()
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
249 with _TempModule(run_name) as temp_module, \
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
250 _ModifiedArgv0(path_name):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
251 mod_globals = temp_module.module.__dict__
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
252 return _run_code(code, mod_globals, init_globals,
65237
75beb6762e3b account for the module __dict__ being cleared when its module is
Benjamin Peterson <benjamin@python.org>
parents: 60648
diff changeset
253 run_name, fname, loader, pkg_name).copy()
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
254 finally:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
255 try:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
256 sys.path.remove(path_name)
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
257 except ValueError:
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
258 pass
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
259
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 51452
diff changeset
260
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
261 if __name__ == "__main__":
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
262 # Run the module specified as the next command line argument
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
263 if len(sys.argv) < 2:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 39782
diff changeset
264 print("No module specified for execution", file=sys.stderr)
37875
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
265 else:
a4c70f3968be Merge part of the trunk changes into the p3yk branch. This merges from 43030
Thomas Wouters <thomas@python.org>
parents:
diff changeset
266 del sys.argv[0] # Make the requested module sys.argv[0]
42954
27f0289b6835 Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents: 42466
diff changeset
267 _run_module_as_main(sys.argv[0])