Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

`http.client.HTTPSConnection._tunnel` raises `AttributeError: 'NoneType' object has no attribute 'encode'` when header value is `None`

未关闭 适合新手
#159,044 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python

调研方向

首先阅读 http.client.HTTPSConnection._tunnel 以及 Lib/http/client.py 中对 set_tunnel 的处理;将其行为与报告中指出的 3.10.20 版本进行比较。检查 Lib/test/test_httplib.py 中现有的隧道测试,并添加对 header 值为 None 的测试覆盖。完成标准是该回归问题已被测试覆盖,并且 _tunnel() 不再因该输入而崩溃。

由索引模型根据 Issue 内容生成。

描述

stdlib type-bug
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

主要语言
Python
星标
77.6k
派生
37.6k
平均合并
1 天 10 小时
30 天内合并 PR
573

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。