gh-76007: Deprecate __version__ attribute in http.server (#142658)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
This commit is contained in:
Hugo van Kemenade 2025-12-13 17:32:13 +02:00 committed by GitHub
parent e02a35c365
commit 170dac291e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 26 additions and 5 deletions

View file

@ -9,6 +9,7 @@ Pending removal in Python 3.20
- :mod:`csv`
- :mod:`!ctypes.macholib`
- :mod:`decimal` (use :data:`decimal.SPEC_VERSION` instead)
- :mod:`http.server`
- :mod:`imaplib`
- :mod:`ipaddress`
- :mod:`json`

View file

@ -1026,6 +1026,7 @@ New deprecations
- :mod:`csv`
- :mod:`!ctypes.macholib`
- :mod:`decimal` (use :data:`decimal.SPEC_VERSION` instead)
- :mod:`http.server`
- :mod:`imaplib`
- :mod:`ipaddress`
- :mod:`json`

View file

@ -61,8 +61,6 @@ XXX To do:
# (Actually, the latter is only true if you know the server configuration
# at the time the request was made!)
__version__ = "0.6"
__all__ = [
"HTTPServer", "ThreadingHTTPServer",
"HTTPSServer", "ThreadingHTTPSServer",
@ -280,7 +278,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
# The server software version. You may want to override this.
# The format is multiple whitespace-separated strings,
# where each string is of the form name[/version].
server_version = "BaseHTTP/" + __version__
server_version = "BaseHTTP"
error_message_format = DEFAULT_ERROR_MESSAGE
error_content_type = DEFAULT_ERROR_CONTENT_TYPE
@ -690,7 +688,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
"""
server_version = "SimpleHTTP/" + __version__
server_version = "SimpleHTTP"
index_pages = ("index.html", "index.htm")
extensions_map = _encodings_map_default = {
'.gz': 'application/gzip',
@ -1080,5 +1078,14 @@ def _main(args=None):
)
def __getattr__(name):
if name == "__version__":
from warnings import _deprecated
_deprecated("__version__", remove=(3, 20))
return "0.6" # Do not change
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
if __name__ == '__main__':
_main()

View file

@ -1560,6 +1560,16 @@ class CommandLineRunTimeTestCase(unittest.TestCase):
self.assertEqual(res, self.served_data)
class TestModule(unittest.TestCase):
def test_deprecated__version__(self):
with self.assertWarnsRegex(
DeprecationWarning,
"'__version__' is deprecated and slated for removal in Python 3.20",
) as cm:
getattr(http.server, "__version__")
self.assertEqual(cm.filename, __file__)
def setUpModule():
unittest.addModuleCleanup(os.chdir, os.getcwd())

View file

@ -583,7 +583,7 @@ would raise an error.
.. nonce: peEgcr
.. section: Library
Deprecate ``__version__`` from a :mod:`imaplib`. Patch by Hugo van Kemenade.
Deprecate ``__version__`` from :mod:`imaplib`. Patch by Hugo van Kemenade.
..

View file

@ -0,0 +1,2 @@
Deprecate ``__version__`` from :mod:`http.server`. Patch by Hugo van
Kemenade.