diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py index a5b7966c7780e9e..d2786ae89b5e644 100644 --- a/Lib/test/test_urlparse.py +++ b/Lib/test/test_urlparse.py @@ -1344,6 +1344,11 @@ def check(result): check(func(burl)) check(func(burl, missing_as_none=True)) + for cls in (urllib.parse.DefragResult, urllib.parse.SplitResult, urllib.parse.ParseResult): + check(cls(*[''] * len(cls._fields))) + for cls in (urllib.parse.DefragResultBytes, urllib.parse.SplitResultBytes, urllib.parse.ParseResultBytes): + check(cls(*[b''] * len(cls._fields))) + def test_result_compat_unpickling(self): def check(result, pickles): for pickled in pickles: diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 4247b9a4b07fa3f..9e47234a21decb6 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -307,6 +307,8 @@ def __deepcopy__(self, memo): def __getstate__(self): state = super().__getstate__() + if state is None: + return None try: if state[1]['_keep_empty'] == _MISSING_AS_NONE_DEFAULT: del state[1]['_keep_empty'] diff --git a/Misc/NEWS.d/next/Library/2026-10-27-20-11-00.gh-issue-160761.aB3dEf.rst b/Misc/NEWS.d/next/Library/2026-10-27-20-11-00.gh-issue-160761.aB3dEf.rst new file mode 100644 index 000000000000000..840456e53d1cc4c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-27-20-11-00.gh-issue-160761.aB3dEf.rst @@ -0,0 +1 @@ +Fix :exc:`TypeError` when pickling directly instantiated :mod:`urllib.parse` result objects.