Skip to content

Commit 95c8cd6

Browse files
committed
correct __name__ in the script launch mode
1 parent 791cf8d commit 95c8cd6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

instld/cli/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def push(self, line):
117117

118118
else:
119119
builtins.__import__ = import_wrapper
120-
spec = importlib.util.spec_from_file_location('kek', os.path.abspath(python_file))
120+
spec = importlib.util.spec_from_file_location('__main__', os.path.abspath(python_file))
121121
module = importlib.util.module_from_spec(spec)
122122
sys.modules['__main__'] = module
123123
if sys.platform.lower() in ('win32',):

tests/cli/test_cli.py

+19
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,22 @@ def test_install_package_from_another_repository_only_command():
159159

160160

161161
os.remove(script)
162+
163+
164+
def test_run_script_and_check_the___name__():
165+
strings = [
166+
'print(__name__)',
167+
]
168+
169+
script = os.path.join('tests', 'cli', 'data', 'main.py')
170+
with open(script, 'w') as file:
171+
file.write('\n'.join(strings))
172+
173+
for runner in (subprocess.run,):
174+
result = runner(['instld', script], stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=100, universal_newlines=True)
175+
176+
result.check_returncode()
177+
178+
assert result.stdout == '__main__\n'
179+
180+
os.remove(script)

0 commit comments

Comments
 (0)