Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python - sys.exit #200

Open
2 tasks
junxnone opened this issue Apr 26, 2020 · 0 comments
Open
2 tasks

python - sys.exit #200

junxnone opened this issue Apr 26, 2020 · 0 comments

Comments

@junxnone
Copy link
Owner

junxnone commented Apr 26, 2020

Reference

Brief

sys.exit([arg])
  • 调用异常 SystemExit
  • arg - 默认 0, 可以是整数或者其他 object,
    • 具体数值和 shell 相关
    • 0 - 指 successful
  • arg - Unix
    • 整数
      • 0 - 指 successful
      • 2 - command line syntax errors
      • 1 - all other kind of errors
    • 其他 object
      • None = 0
      • other = 1 - stderr print
In [1]: import sys

In [2]: sys.exit?
Docstring:
exit([status])

Exit the interpreter by raising SystemExit(status).
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is an integer, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
Type:      builtin_function_or_method

exception SystemExit

sys.exit() vs os._exit() vs exit() vs quit()

  • os._exit() - 直接将python解释器退出,余下的语句不会执行
    • without calling cleanup handlers
    • 通常在 子进程 中使用
    • 调用 C 语言的 _exit() 函数
  • sys.exit() - 调用后会引发SystemExit异常,可以捕获此异常做清理工作
  • exit() - site.py 中定义, 不应在产品 code 中使用, 一般在交互式 Shell 中退出时使用
  • quit() - site module 中定义, 不应在产品 code 中使用, 一般在交互式 Shell 中退出时使用

site module

  • 在解释器(interpreter)启动时,site会被自动的导入(imported)
  • -S : don't imply 'import site' on initialization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant