File tree Expand file tree Collapse file tree 2 files changed +37
-2
lines changed
Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -223,9 +223,9 @@ def exec_code(interpreter, args):
223223 source = sourcefile .read ()
224224 old_argv , sys .argv = sys .argv , args
225225 sys .path .insert (0 , os .path .abspath (os .path .dirname (args [0 ])))
226- spec = importlib .util .spec_from_loader ("__console__ " , loader = None )
226+ spec = importlib .util .spec_from_loader ("__main__ " , loader = None )
227227 mod = importlib .util .module_from_spec (spec )
228- sys .modules ["__console__ " ] = mod
228+ sys .modules ["__main__ " ] = mod
229229 interpreter .locals .update (mod .__dict__ )
230230 interpreter .locals ["__file__" ] = args [0 ]
231231 interpreter .runsource (source , args [0 ], "exec" )
Original file line number Diff line number Diff line change @@ -80,6 +80,41 @@ def test_exec_dunder_file(self):
8080 )
8181 self .assertEqual (stderr .strip (), f .name )
8282
83+ def test_exec_dunder_name (self ):
84+ with tempfile .NamedTemporaryFile (mode = "w" ) as f :
85+ f .write (
86+ dedent (
87+ """\
88+ import sys
89+ sys.stderr.write(__name__)
90+ sys.stderr.flush()"""
91+ )
92+ )
93+ f .flush ()
94+ _ , stderr = run_with_tty (
95+ [sys .executable ] + ["-m" , "bpython.curtsies" , f .name ]
96+ )
97+ self .assertEqual (stderr .strip (), '__main__' )
98+
99+ def test_exec_dunder_name_in_imported_module (self ):
100+ with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".py" ) as module_file , tempfile .NamedTemporaryFile (mode = "w" ) as main_file :
101+ module_file .write (
102+ dedent (
103+ """\
104+ import sys
105+ sys.stderr.write(__name__)
106+ sys.stderr.flush()"""
107+ )
108+ )
109+ module_file .flush ()
110+ module_name = os .path .basename (module_file .name )[:- 3 ]
111+ main_file .write (f"import { module_name } " )
112+ main_file .flush ()
113+ _ , stderr = run_with_tty (
114+ [sys .executable ] + ["-m" , "bpython.curtsies" , main_file .name ]
115+ )
116+ self .assertEqual (stderr .strip (), module_name )
117+
83118 def test_exec_nonascii_file (self ):
84119 with tempfile .NamedTemporaryFile (mode = "w" ) as f :
85120 f .write (
You can’t perform that action at this time.
0 commit comments