@@ -116,21 +116,45 @@ def __init__(self, args, as_module=False):
116
116
self .package = self .modulename = self .pathname = self .loader = self .spec = None
117
117
118
118
def prepare (self ):
119
- """Do initial preparation to run Python code.
120
-
121
- Includes finding the module to run, adjusting sys.argv[0], and changing
122
- sys.path to match what Python does.
119
+ """Set sys.path properly.
123
120
121
+ This needs to happen before any importing, and without importing anything.
124
122
"""
125
123
should_update_sys_path = True
126
-
127
124
if self .as_module :
128
125
if env .PYBEHAVIOR .actual_syspath0_dash_m :
129
126
path0 = os .getcwd ()
130
127
else :
131
128
path0 = ""
132
129
sys .path [0 ] = path0
133
130
should_update_sys_path = False
131
+ elif os .path .isdir (self .arg0 ):
132
+ # Running a directory means running the __main__.py file in that
133
+ # directory.
134
+ path0 = self .arg0
135
+ else :
136
+ path0 = os .path .abspath (os .path .dirname (self .arg0 ))
137
+
138
+
139
+ if should_update_sys_path :
140
+ # sys.path fakery. If we are being run as a command, then sys.path[0]
141
+ # is the directory of the "coverage" script. If this is so, replace
142
+ # sys.path[0] with the directory of the file we're running, or the
143
+ # current directory when running modules. If it isn't so, then we
144
+ # don't know what's going on, and just leave it alone.
145
+ top_file = inspect .stack ()[- 1 ][0 ].f_code .co_filename
146
+ if os .path .abspath (sys .path [0 ]) == os .path .abspath (os .path .dirname (top_file )):
147
+ # Set sys.path correctly.
148
+ sys .path [0 ] = python_reported_file (path0 )
149
+
150
+ def _prepare2 (self ):
151
+ """Do more preparation to run Python code.
152
+
153
+ Includes finding the module to run and adjusting sys.argv[0].
154
+ This method is allowed to import code.
155
+
156
+ """
157
+ if self .as_module :
134
158
self .modulename = self .arg0
135
159
pathname , self .package , self .spec = find_module (self .modulename )
136
160
if self .spec is not None :
@@ -141,7 +165,6 @@ def prepare(self):
141
165
elif os .path .isdir (self .arg0 ):
142
166
# Running a directory means running the __main__.py file in that
143
167
# directory.
144
- path0 = self .arg0
145
168
for ext in [".py" , ".pyc" , ".pyo" ]:
146
169
try_filename = os .path .join (self .arg0 , "__main__" + ext )
147
170
if os .path .exists (try_filename ):
@@ -165,29 +188,16 @@ def prepare(self):
165
188
self .package = ""
166
189
self .loader = DummyLoader ("__main__" )
167
190
else :
168
- path0 = os .path .abspath (os .path .dirname (self .arg0 ))
169
191
if env .PY3 :
170
192
self .loader = DummyLoader ("__main__" )
171
193
172
194
self .arg0 = python_reported_file (self .arg0 )
173
195
174
- if self .modulename is None :
175
- self .modulename = '__main__'
176
-
177
- if should_update_sys_path :
178
- # sys.path fakery. If we are being run as a command, then sys.path[0]
179
- # is the directory of the "coverage" script. If this is so, replace
180
- # sys.path[0] with the directory of the file we're running, or the
181
- # current directory when running modules. If it isn't so, then we
182
- # don't know what's going on, and just leave it alone.
183
- top_file = inspect .stack ()[- 1 ][0 ].f_code .co_filename
184
- if os .path .abspath (sys .path [0 ]) == os .path .abspath (os .path .dirname (top_file )):
185
- # Set sys.path correctly.
186
- sys .path [0 ] = python_reported_file (path0 )
187
-
188
196
def run (self ):
189
197
"""Run the Python code!"""
190
198
199
+ self ._prepare2 ()
200
+
191
201
# Create a module to serve as __main__
192
202
main_mod = types .ModuleType ('__main__' )
193
203
0 commit comments