You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In theory, running pip("code", ["arglist"]) from within Python code should work nicely; and indeed, it does work nicely from within IDLE. But in any other context (Python's built-in REPL, other Python code), the fact that the function calls sys.exit(1) when it runs into a fatal error is unexpected and a bit tricky to deal with. A better approach would be to raise a normal exception (maybe FatalError) and let the caller handle it. When running pip.py as the main program, one approach might be to catch the error in the if __name__ == "__main__" section and do sys.exit(1) there.
In a similar vein, sys.exit(0) could instead be return, which would play a lot nicer with anyone trying to call pip() from their own code.
Finally, there are some parts of the current pip() function that ought to be refactored and called something else. The argparse code could probably go in its own function, for starters; and any code that assumes we're coming from a command-line invocation probably belongs outside of pip(), which might be called from some other context.
The text was updated successfully, but these errors were encountered:
- pip() now raises FatalError instead of using sys.exit(1), making it play much nicer with third-party code that calls it
- Moved main-program code into main() function
- Added run() function for running specific code
In theory, running
pip("code", ["arglist"])
from within Python code should work nicely; and indeed, it does work nicely from within IDLE. But in any other context (Python's built-in REPL, other Python code), the fact that the function callssys.exit(1)
when it runs into a fatal error is unexpected and a bit tricky to deal with. A better approach would be to raise a normal exception (maybeFatalError
) and let the caller handle it. When runningpip.py
as the main program, one approach might be to catch the error in theif __name__ == "__main__"
section and dosys.exit(1)
there.In a similar vein,
sys.exit(0)
could instead bereturn
, which would play a lot nicer with anyone trying to callpip()
from their own code.Finally, there are some parts of the current
pip()
function that ought to be refactored and called something else. The argparse code could probably go in its own function, for starters; and any code that assumes we're coming from a command-line invocation probably belongs outside ofpip()
, which might be called from some other context.The text was updated successfully, but these errors were encountered: