From 25d6bff59c2abb7b2f8aa75c56a711c20d828703 Mon Sep 17 00:00:00 2001 From: Filipe Rosset Date: Wed, 2 Dec 2026 21:26:35 -0300 Subject: [PATCH] Add Python 3.16 support Metadata/CI: - pyproject.toml: add 'Programming Language :: Python :: 3.16' classifier - .github/workflows/ci.yml: add 3.16 to linux matrix; bump other-matrix max from 3.15 to 3.16 Source fixes for two 3.16 stdlib behavior changes (verified on 3.16.0a2): 1. urllib.parse now validates ports eagerly at urlsplit()/urlparse() time, raising ValueError for non-integer ports (e.g. ':bad') instead of deferring to the .port property. gh-88037, listed under 'Porting to Python 3.16' in the 3.16 What's New. Three call sites in _urllib2_fork.py are wrapped in try/except ValueError: - _url_origin(): return the URL as its own origin - normalize_url(): return the URL unchanged - request_host(): fall back to the Host header 2. http.cookiejar.MozillaCookieJar.save() now writes '0' (not '') in the expires field for session cookies, matching curl/Wget convention. gh-61366. _clientcookie.py _really_load() now treats expires == '0' the same as the empty string: session cookie (expires=None, discard=True). All 346 tests pass on 3.10-3.16. ruff clean. Signed-off-by: Filipe Rosset --- .github/workflows/ci.yml | 4 ++-- mechanize/_clientcookie.py | 3 ++- mechanize/_urllib2_fork.py | 18 +++++++++++++++--- pyproject.toml | 1 + 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1777e4..84e714d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ['3.10', '3.11', '3.12', '3.13', '3.14', '3.15'] + python-version: ['3.10', '3.11', '3.12', '3.13', '3.14', '3.15', '3.16'] steps: - uses: actions/checkout@v4 @@ -48,7 +48,7 @@ jobs: fail-fast: false matrix: os: [macos-latest, windows-latest] - python-version: ['3.10', '3.15'] + python-version: ['3.10', '3.16'] steps: - uses: actions/checkout@v4 diff --git a/mechanize/_clientcookie.py b/mechanize/_clientcookie.py index 6c89cbf..9c49323 100644 --- a/mechanize/_clientcookie.py +++ b/mechanize/_clientcookie.py @@ -240,7 +240,8 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires): assert domain_specified == initial_dot discard = False - if expires == "": + if expires in ("", "0"): + # 3.16+ stdlib save() writes "0" (not "") for session cookies expires = None discard = True diff --git a/mechanize/_urllib2_fork.py b/mechanize/_urllib2_fork.py index 6a10292..0041d2d 100644 --- a/mechanize/_urllib2_fork.py +++ b/mechanize/_urllib2_fork.py @@ -130,7 +130,11 @@ def request_host(request): """ url = request.get_full_url() - host = urlparse(url)[1] + try: + host = urlparse(url)[1] + except ValueError: + # 3.16+ urlparse() rejects non-integer ports + host = "" if host == "": host = request.get_header("Host", "") @@ -173,7 +177,11 @@ def fix_invalid_bytes_in_url_component(component, allowed_chars=PATH_CHARS): def normalize_url(url): - parsed = urlparse(url) + try: + parsed = urlparse(url) + except ValueError: + # 3.16+ urlparse() rejects non-integer ports; keep the URL as-is + return url netloc = parsed.netloc if not isinstance(netloc, bytes) and netloc: def safe_encode(label): @@ -569,7 +577,11 @@ def http_error_default(self, req, fp, code, msg, hdrs): def _url_origin(url): - parts = urlsplit(url) + try: + parts = urlsplit(url) + except ValueError: + # 3.16+ urlsplit() rejects non-integer ports; treat as its own origin + return url scheme = (parts.scheme or '').lower() try: port = parts.port diff --git a/pyproject.toml b/pyproject.toml index 5928401..f30034a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ classifiers = [ "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3.14", "Programming Language :: Python :: 3.15", + "Programming Language :: Python :: 3.16", "Topic :: Internet", "Topic :: Internet :: File Transfer Protocol (FTP)", "Topic :: Internet :: WWW/HTTP",