@@ -98,22 +98,11 @@ def run(self, cmd):
98
98
return self .runctx (cmd , dict , dict )
99
99
100
100
def runctx (self , cmd , globals , locals ):
101
- # cmd has to run in __main__ namespace (or imports from __main__ will
102
- # break). Clear __main__ and replace with the globals provided.
103
- import __main__
104
- # Save a reference to the current __main__ namespace so that we can
105
- # restore it after cmd completes.
106
- original_main = __main__ .__dict__ .copy ()
107
- __main__ .__dict__ .clear ()
108
- __main__ .__dict__ .update (globals )
109
-
110
101
self .enable ()
111
102
try :
112
- exec (cmd , __main__ . __dict__ , locals )
103
+ exec (cmd , globals , locals )
113
104
finally :
114
105
self .disable ()
115
- __main__ .__dict__ .clear ()
116
- __main__ .__dict__ .update (original_main )
117
106
return self
118
107
119
108
@@ -192,10 +181,19 @@ def main():
192
181
'__cached__' : None ,
193
182
'__builtins__' : __builtins__ ,
194
183
}
184
+ # cmd has to run in __main__ namespace (or imports from __main__ will
185
+ # break). Clear __main__ and replace with the globals provided.
186
+ import __main__
187
+ # Save a reference to the current __main__ namespace so that we can
188
+ # restore it after cmd completes.
189
+ original_main = __main__ .__dict__ .copy ()
190
+ __main__ .__dict__ .update (globs )
195
191
196
192
try :
197
- runctx (code , globs , None , options .outfile , options .sort )
193
+ runctx (code , __main__ . __dict__ , None , options .outfile , options .sort )
198
194
except BrokenPipeError as exc :
195
+ __main__ .__dict__ .clear ()
196
+ __main__ .__dict__ .update (original_main )
199
197
# Prevent "Exception ignored" during interpreter shutdown.
200
198
sys .stdout = None
201
199
sys .exit (exc .errno )
0 commit comments