`http.client.HTTPSConnection._tunnel` raises `AttributeError: 'NoneType' object has no attribute 'encode'` when header value is `None`
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- python
- Domain
- backend, networking
Research direction
Start by reading http.client.HTTPSConnection._tunnel and the set_tunnel handling in Lib/http/client.py; compare the behavior with the 3.10.20 version identified in the report. Check existing tunnel tests in Lib/test/test_httplib.py and add coverage for a header value of None. Done means the regression is covered and _tunnel() no longer crashes for that input.
Written by the indexing model from the issue text.
Description
Bug report
Bug Description
Calling http.client.HTTPSConnection._tunnel() raises AttributeError: 'NoneType' object has no attribute 'encode' when _tunnel_headers contains a header key with a None value.
This regression affects third-party libraries (such as Apache Thrift and CodeChecker) that pass header dictionaries containing None values into set_tunnel(..., headers=...) or HTTP connection objects.
In Python 3.10.21, _tunnel() introduced strict encoding of tunnel headers (value.encode("latin-1")) without checking if the header value is None.
Target Python Version
Python 3.10.21 (Regression introduced between 3.10.20 and 3.10.21)
Operating System
Linux / Ubuntu
Code to Reproduce
import http.client
conn = http.client.HTTPSConnection("127.0.0.1", 8080)
# Set tunnel with a header value of None
conn.set_tunnel("example.com", 443, headers={"X-Test-Header": None})
# Call _tunnel() to trigger the issue
conn._tunnel()
Expected Behavior
_tunnel() should gracefully handle or filter out header keys with None values (or handle string conversions) rather than crashing with an AttributeError.
Actual Behavior
Traceback (most recent call last):
File "reproduce.py", line 9, in
conn._tunnel()
File "/usr/lib/python3.10/http/client.py", line 936, in _tunnel
value_bytes = value.encode("latin-1")
AttributeError: 'NoneType' object has no attribute 'encode'
CPython versions tested on:
3.10
Operating systems tested on:
Linux
- Dominant language
- Python
- Stars
- 77.6k
- Forks
- 37.6k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 573
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/cpython
-
A minor problem with the section "6.10.2. Membership test operations" from the language reference.Opendocs
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
docs
Difficulty 1/5 1-3 hours Newbie friendliness 78/100
python/cpython#159012 · 1 comment ·
Maintainers usually reply within 1 day
-
stdlib topic-profiling type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
stdlib type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
lazy import of `_sysconfig` in `Lib/ctypes/__init__.py` is eager on android and cygwinPossibly taken @sillyfellow21 claimed this 4 days ago. Opentests topic-lazy-imports
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
python/cpython#158684 · 4 comments ·
Maintainers usually reply within 1 day
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pyjanitor-devs/pyjanitor#1758 ·
Maintainers usually reply within 1 day
-
bug ready for review
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
odysseus-dev/odysseus#6641 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
happypawspillaro/happypaws#78 ·
Maintainers usually reply within 4 days
-
pydanty:is-working
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
pydantic/pydantic-ai#10020 ·
Maintainers usually reply within 1 day
-
Bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ansible-collections/ibm_zos_core#2650 ·