为澄清这个问题,我编写了以下脚本:
#!/usr/bin/python3
import os,sys
import time
try:
while True:
user = input("User: ")
if user == "root":
os.system("sudo -u root /bin/bash")
else:
time.sleep(2)
except:
print("Exiting...")
sys.exit(0)
当我仅使用python3
运行此脚本时,一切都会按预期进行。例如,如果我在程序(KeyboardInterrupt)之外按Ctrl C,则不会有详细的错误消息(来自追溯)。我只想输出“ Exiting ...”,这就是为什么我没有指定KeyboardInterrupt的原因,但是可能会发生任何异常。但是,当我使用pyinstaller
(pyinstaller --onefile test.py
)构建ELF文件(对于Linux)时,会得到回溯输出:
$ dist/test
User: ^CTraceback (most recent call last):
File "test.py", line 7, in <module>
test = input("User: ")
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 15, in <module>
print("Exiting...")
奇怪的是,我无法每次在input
行退出程序时都重现此内容,但是当程序在time.sleep(2)
处休眠时,我可以100%重现以上内容:
$ dist/test
User: ^CExiting...
$ dist/test
User: ^CExiting...
$ dist/test
User: ^CExiting...
Traceback (most recent call last):
File "test.py", line 7, in <module>
test = input("User: ")
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 15, in <module>
print("Exiting...")
KeyboardInterrupt
[18825] Failed to execute script test
$ dist/test
User: asd
^CTraceback (most recent call last):
File "test.py", line 12, in <module>
time.sleep(2)
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 12, in <module>
time.sleep(2)
KeyboardInterrupt
[18833] Failed to execute script test
是否有一种方法可以告诉pyinstaller
不要“泄漏”脚本名称(例如:File "test.py"
)和实际代码行(例如:test = input("User: ")
)?