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

AttributeError: module 'email.errors' has no attribute 'InvalidHeaderError'. #93010

Closed
The-Compiler opened this issue May 20, 2022 · 5 comments
Closed
Labels
topic-email type-bug An unexpected behavior, bug, or error

Comments

@The-Compiler
Copy link
Contributor

Bug report

email._header_value_parser tries to raise errors.InvalidHeaderError, but that does not exist (and as far as I can tell, never did):

>>> from email.headerregistry import HeaderRegistry
>>> reg = HeaderRegistry()
>>> reg('Content-Disposition', 'attachment; 0*00="foo"')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/email/headerregistry.py", line 604, in __call__
    return self[name](name, value)
  File "/usr/lib/python3.10/email/headerregistry.py", line 192, in __new__
    cls.parse(value, kwds)
  File "/usr/lib/python3.10/email/headerregistry.py", line 448, in parse
    kwds['parse_tree'] = parse_tree = cls.value_parser(value)
  File "/usr/lib/python3.10/email/_header_value_parser.py", line 2705, in parse_content_disposition_header
    disp_header.append(parse_mime_parameters(value[1:]))
  File "/usr/lib/python3.10/email/_header_value_parser.py", line 2569, in parse_mime_parameters
    token, value = get_parameter(value)
  File "/usr/lib/python3.10/email/_header_value_parser.py", line 2431, in get_parameter
    token, value = get_section(value)
  File "/usr/lib/python3.10/email/_header_value_parser.py", line 2382, in get_section
    section.defects.append(errors.InvalidHeaderError(
AttributeError: module 'email.errors' has no attribute 'InvalidHeaderError'. Did you mean: 'InvalidHeaderDefect'?

Code:

if digits[0] == '0' and digits != '0':
section.defects.append(errors.InvalidHeaderError(
"section number has an invalid leading 0"))

This was added in 97f43c0 by @bitdancer pretty much some 10 years ago, and I don't think it ever worked (but nor could I find anyone running into this so far): The git history doesn't know anything about an email.errors.InvalidHeaderError.

Found via Hypothesis while testing my own parsing code - cc @Zac-HD if you want to see another instance of "the curse of hypothesis" 😉

Your environment

  • CPython versions tested on: 3.7.13, 3.8.13, 3.9.13, 3.10.4, 3.11.0b1
  • Operating system and architecture: Archlinux, x86_64
@The-Compiler The-Compiler added the type-bug An unexpected behavior, bug, or error label May 20, 2022
@warsaw
Copy link
Member

warsaw commented May 20, 2022

Oh wow, nice find.

@Zac-HD
Copy link
Contributor

Zac-HD commented May 20, 2022

Nice find!

On a meta-level though this suggests that CPython could really benefit from adopting some static analysis tooling and/or coverage requirements which are table stakes for any critical project... maybe I can argue for that instead of PBT at the next summit 😅

@The-Compiler
Copy link
Contributor Author

The-Compiler commented May 20, 2022

On a meta-level though this suggests that CPython could really benefit from adopting some static analysis tooling and/or coverage requirements which are table stakes for any critical project... maybe I can argue for that instead of PBT at the next summit

Agreed! FWIW I tried to run flake8 with a lot of ignored errors and manual processing, and I believe I now have another handful of bugs/PRs to report... 😅

Lib/_pyio.py:2165:28: F821 undefined name 'locale'
Lib/enum.py:1643:28: F821 undefined name 'cls_name'
Lib/test/test_shutil.py:1597:78: F821 undefined name 'x'
Lib/test/test_shutil.py:1599:78: F821 undefined name 'x'
Lib/test/test_shutil.py:1602:48: F821 undefined name 'x'
Lib/test/test_urllib2net.py:60:23: F821 undefined name 'ResourceDenied'
Lib/test/test_importlib/test_threaded_import.py:275:5: F821 undefined name 'unittets'
Lib/test/test_sqlite3/test_dbapi.py:312:13: F821 undefined name 'const'
Lib/test/test_sqlite3/test_dbapi.py:646:39: F821 undefined name 'size'

Lib/enum.py:483:30: F523 '...'.format(...) has unused arguments at position(s): 0
Lib/test/test_concurrent_futures.py:480:47: F522 '...'.format(...) has unused named argument(s): executor_type

Lib/encodings/mac_arabic.py:474:5: F601 dictionary key 32 repeated with different values
[...]
Lib/encodings/mac_arabic.py:593:5: F601 dictionary key 125 repeated with different values
Lib/test/test_mailbox.py:2265:5: F601 dictionary key 'Delivered-To' repeated with different values
Lib/test/test_mailbox.py:2266:5: F601 dictionary key 'Received' repeated with different values
[...]

@The-Compiler
Copy link
Contributor Author

For anyone curious about the result of running flake8 and taking a closer look at the result: I spent hours today reporting 9 more similar bugs, see them linked above. Decided to not report the encodings.mac_arabic one despite it probably being a genuine issue too - it was added in 2005, seems to be autogenerated, and hasn't seen any significant changes since then. Given how irrelevant it probably is today, I'm guessing nobody will care (and that's probably okay too!).

warsaw pushed a commit that referenced this issue May 23, 2022
* fix issue 93010

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 23, 2022
* fix issue 93010

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
(cherry picked from commit 71abeb0)

Co-authored-by: oda-gitso <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 23, 2022
* fix issue 93010

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
(cherry picked from commit 71abeb0)

Co-authored-by: oda-gitso <[email protected]>
@warsaw
Copy link
Member

warsaw commented May 23, 2022

@The-Compiler Thanks for the above reports. This issue is now fix, thus closing.

@warsaw warsaw closed this as completed May 23, 2022
miss-islington added a commit that referenced this issue May 23, 2022
* fix issue 93010

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
(cherry picked from commit 71abeb0)

Co-authored-by: oda-gitso <[email protected]>
miss-islington added a commit that referenced this issue May 23, 2022
* fix issue 93010

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
(cherry picked from commit 71abeb0)

Co-authored-by: oda-gitso <[email protected]>
The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue May 24, 2022
The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue May 24, 2022
Also adds workaround for python/cpython#93010

(cherry picked from commit 9afcad1)
The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-email type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants