[Python-checkins] bpo-33604: Remove deprecated HMAC default value marked for removal in 3.8 (GH-7063)

Gregory P. Smith webhook-mailer at python.org
Mon Sep 10 14:10:05 EDT 2018


https://github.com/python/cpython/commit/51a4743d19abd016f0772a57fb31df7af9220e18
commit: 51a4743d19abd016f0772a57fb31df7af9220e18
branch: master
author: Matthias Bussonnier <bussonniermatthias at gmail.com>
committer: Gregory P. Smith <greg at krypto.org>
date: 2018-09-10T11:10:01-07:00
summary:

bpo-33604: Remove deprecated HMAC default value marked for removal in 3.8 (GH-7063)

HMAC's digestmod was deprecated marked for removal, this removes it as planned.

files:
A Misc/NEWS.d/next/Library/2018-05-22-11-55-33.bpo-33604.6V4JcO.rst
M Doc/library/hmac.rst
M Lib/hmac.py
M Lib/test/test_hmac.py

diff --git a/Doc/library/hmac.rst b/Doc/library/hmac.rst
index 731624ba94e3..dc994b07c35c 100644
--- a/Doc/library/hmac.rst
+++ b/Doc/library/hmac.rst
@@ -19,8 +19,7 @@ This module implements the HMAC algorithm as described by :rfc:`2104`.
    Return a new hmac object.  *key* is a bytes or bytearray object giving the
    secret key.  If *msg* is present, the method call ``update(msg)`` is made.
    *digestmod* is the digest name, digest constructor or module for the HMAC
-   object to use. It supports any name suitable to :func:`hashlib.new` and
-   defaults to the :data:`hashlib.md5` constructor.
+   object to use. It supports any name suitable to :func:`hashlib.new`.
 
    .. versionchanged:: 3.4
       Parameter *key* can be a bytes or bytearray object.
diff --git a/Lib/hmac.py b/Lib/hmac.py
index 43b721297637..890eaba08e8f 100644
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -35,12 +35,9 @@ def __init__(self, key, msg = None, digestmod = None):
 
         key:       key for the keyed hash object.
         msg:       Initial input for the hash, if provided.
-        digestmod: A module supporting PEP 247.  *OR*
-                   A hashlib constructor returning a new hash object. *OR*
+        digestmod: Required.  A module supporting PEP 247.  *OR*
+                   A hashlib constructor returning a new hash object.  *OR*
                    A hash name suitable for hashlib.new().
-                   Defaults to hashlib.md5.
-                   Implicit default to hashlib.md5 is deprecated since Python
-                   3.4 and will be removed in Python 3.8.
 
         Note: key and msg must be a bytes or bytearray objects.
         """
@@ -49,11 +46,7 @@ def __init__(self, key, msg = None, digestmod = None):
             raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
 
         if digestmod is None:
-            _warnings.warn("HMAC() without an explicit digestmod argument "
-                           "is deprecated since Python 3.4, and will be removed "
-                           "in 3.8",
-                           DeprecationWarning, 2)
-            digestmod = _hashlib.md5
+            raise ValueError('`digestmod` is required.')
 
         if callable(digestmod):
             self.digest_cons = digestmod
diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py
index 7f4901307630..896bbe9ab798 100644
--- a/Lib/test/test_hmac.py
+++ b/Lib/test/test_hmac.py
@@ -302,45 +302,38 @@ def digest(self):
                 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
                 self.fail('Expected warning about small block_size')
 
-    def test_with_digestmod_warning(self):
-        with self.assertWarns(DeprecationWarning):
+    def test_with_digestmod_no_default(self):
+        with self.assertRaises(ValueError):
             key = b"\x0b" * 16
             data = b"Hi There"
-            digest = "9294727A3638BB1C13F48EF8158BFC9D"
-            h = hmac.HMAC(key, data)
-            self.assertEqual(h.hexdigest().upper(), digest)
-
+            hmac.HMAC(key, data, digestmod=None)
 
 class ConstructorTestCase(unittest.TestCase):
 
-    @ignore_warning
     def test_normal(self):
         # Standard constructor call.
         failed = 0
         try:
-            h = hmac.HMAC(b"key")
+            h = hmac.HMAC(b"key", digestmod='md5')
         except Exception:
             self.fail("Standard constructor call raised exception.")
 
-    @ignore_warning
     def test_with_str_key(self):
         # Pass a key of type str, which is an error, because it expects a key
         # of type bytes
         with self.assertRaises(TypeError):
-            h = hmac.HMAC("key")
+            h = hmac.HMAC("key", digestmod='md5')
 
-    @ignore_warning
     def test_dot_new_with_str_key(self):
         # Pass a key of type str, which is an error, because it expects a key
         # of type bytes
         with self.assertRaises(TypeError):
-            h = hmac.new("key")
+            h = hmac.new("key", digestmod='md5')
 
-    @ignore_warning
     def test_withtext(self):
         # Constructor call with text.
         try:
-            h = hmac.HMAC(b"key", b"hash this!")
+            h = hmac.HMAC(b"key", b"hash this!", digestmod='md5')
         except Exception:
             self.fail("Constructor call with text argument raised exception.")
         self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864')
@@ -369,13 +362,6 @@ def test_withmodule(self):
 
 class SanityTestCase(unittest.TestCase):
 
-    @ignore_warning
-    def test_default_is_md5(self):
-        # Testing if HMAC defaults to MD5 algorithm.
-        # NOTE: this whitebox test depends on the hmac class internals
-        h = hmac.HMAC(b"key")
-        self.assertEqual(h.digest_cons, hashlib.md5)
-
     def test_exercise_all_methods(self):
         # Exercising all methods once.
         # This must not raise any exceptions
diff --git a/Misc/NEWS.d/next/Library/2018-05-22-11-55-33.bpo-33604.6V4JcO.rst b/Misc/NEWS.d/next/Library/2018-05-22-11-55-33.bpo-33604.6V4JcO.rst
new file mode 100644
index 000000000000..200a9d5f661a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-05-22-11-55-33.bpo-33604.6V4JcO.rst
@@ -0,0 +1,2 @@
+Remove HMAC default to md5 marked for removal in 3.8 (removal originally
+planned in 3.6, bump to 3.8 in gh-7062).



More information about the Python-checkins mailing list