Skip to content

Surround sys.argv call with a try-except block #754

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

Closed
wants to merge 1 commit into from

Conversation

insideClaw
Copy link

Fix for #753

Branched from master as the changes it's based on ( #739 ) have not gone through the development branch.

@methane
Copy link
Member

methane commented Dec 15, 2018

This code looks very ugly. except(AttributeError): doesn't follow code style widely used for Python.
And getattr(sys, "argv", None) is enough for fixing this.

But sys.argv has another problem. I'll remove automatically checking sys.argv.

@methane methane closed this Dec 15, 2018
@insideClaw
Copy link
Author

Good luck with your preferred approach.

For my information, @methane can you elaborate on what makes the code ugly for you - the lack of whitespace and/or using parenthesis for only one argument?

@methane
Copy link
Member

methane commented Dec 17, 2018

  • First of all, as I mentioned above, getattr(sys, "argv", None) is simplest.
  • except(AttributeError): -> except AttributeError:
  • try-except block must be small as possible; only getattr should be in try block.

@insideClaw
Copy link
Author

Thanks for explanation and the try-except block size optimization, I hadn't thought of that one.

@methane
Copy link
Member

methane commented Dec 17, 2018

Another better code is:

try:
    program_name = sys.argv[0]
except (AttributeError, IndexError):
    pass

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants