Terry J. Reedy <tjre...@udel.edu> added the comment:

The docs are normative as well as descriptive.  The specific doc for sys.argv 
is https://docs.python.org/3/library/sys.html#sys.argv.  It says that a proper 
Python stdlib sys module has an args attribute that is a non-empty list whose 
first item is a string.  (It implies, and to me should actually say, that all 
items are strings.)  Since it would seem that it is easy enough to make this 
true, I think that Python code should be able to depend on this as much as 
anything else in the docs.

The 2003 issue 839151 reported that warnings.warn exited with AttributeError 
when accessing a non-existent argv in an embedded interpreter.  I presume that 
the latter was embedded CPython.  AFAIK, the fix could have been to fix 
embedded interpreters to have sys.argv = ['']. (My preference.) Instead, the 
fix, for the warn access only, was to guard the access with try-except 
AttributeError.

The extended fix proposed here is to document that argv is at least optional 
and therefore imply that all access should be wrapped in try-except.

Guido, which idea comports with your idea of 'Python': sys has an argv with at 
least one string (the name, if any, of a 'program'); or sys has an optional 
argv, so that all bullet-proof code must guard access by catching at least 
AttributeError?
---

Notes on a couple of #839151 messages:

1. The fact that one can make argv be missing by deleting it was and is 
irrelevant.  Monkeypatching modules by deleting and changing attributes can 
create unlimited havoc and there is nothing special about sys.argv in this 
regard.

2. "this [patch] should also catch IndexError and TypeError in case sys.argv is 
an empty list or it is not even a list at all."  I consider this similarly 
irrelevant because both of these would, according to the doc, be bugs, whether 
originally present or introduced by monkeypatching.  

https://github.com/pypa/keyring/445 is 404 for me in multiple tries.

----------
nosy: +gvanrossum, terry.reedy
versions: +Python 3.10

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41615>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to