Deformed enough PYTHONWARNINGS makes pypy crash on startup

Issue #2917 resolved
Julian Berman
created an issue
⊙  /usr/local/bin/pypy --version && env -i PYTHONWARNINGS='ignore:*:*:foo.py' /usr/local/bin/pypy                                                                      julian@Air
Python 2.7.13 (ab0b9caf307db6592905a80b8faffd69b39005b8, Jun 24 2018, 08:19:27)
[PyPy 6.0.0 with GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
debug: OperationError:
debug:  operror-type: ValueError
debug:  operror-value: Empty module name

In CPython this also blows up, but only once you import the warnings module (presumably the same thing happening, just early enough in PyPy startup to make the interpreter freak out a bit?):

  /usr/bin/python --version && env -i PYTHONWARNINGS='ignore:*:*:foo.py' /usr/bin/python                                                                              julian@Air
Python 2.7.10
Python 2.7.10 (default, Aug 17 2018, 17:41:52)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import warnings
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py", line 388, in <module>
    _processoptions(sys.warnoptions)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py", line 110, in _processoptions
    _setoption(arg)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py", line 126, in _setoption
    category = _getcategory(category)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.py", line 166, in _getcategory
    m = __import__(module, None, None, [klass])
ValueError: Empty module name

ISTM that probably the best behavior here is to not fail at all and to just do the same thing that happens when you provide slightly less deformed PYTHONWARNINGS -- i.e., ignore it.

⊙  PYTHONWARNINGS=foo:bar:bar:bar pypy                                                                                                                                 julian@Air
Invalid -W option ignored: invalid action: 'foo'

```

Comments (5)

  1. Armin Rigo

    Following the CPython 2.7 source code, it seems that we need this in interpreter/app_main.py:

         if warnoptions:
             sys.warnoptions[:] = warnoptions
    -        from warnings import _processoptions
    -        _processoptions(sys.warnoptions)
    +        try:
    +            if 'warnings' in sys.modules:
    +                from warnings import _processoptions
    +                _processoptions(sys.warnoptions)
    +            else:
    +                import warnings
    +        except ImportError as e:
    +            pass   # CPython just eats any exception here
    

    Getting a saner complain than a crash involves changing the standard library module warnings.py, which we generally avoid to do without good reason. Now this is arguably a good reason.

  2. Armin Rigo

    Something like that, maybe?

    diff -r d5b5c118b132 lib-python/2.7/warnings.py
    --- a/lib-python/2.7/warnings.py        Sun Nov 25 09:35:46 2018 +0200
    +++ b/lib-python/2.7/warnings.py        Mon Nov 26 07:08:42 2018 +0200
    @@ -182,6 +182,8 @@
             module = category[:i]
             klass = category[i+1:]
             try:
    +            if not module:
    +                raise ImportError   # instead of the ValueError we'd get
                 m = __import__(module, None, None, [klass])
             except ImportError:
                 raise _OptionError("invalid module name: %r" % (module,))
    
  3. Log in to comment