bpo-43916: _md5.md5 uses Py_TPFLAGS_DISALLOW_INSTANTIATION (GH-25753) · python/cpython@665c774 (original) (raw)

`@@ -901,8 +901,39 @@ def test_get_fips_mode(self):

`

901

901

`if fips_mode is not None:

`

902

902

`self.assertIsInstance(fips_mode, int)

`

903

903

``

``

904

`+

def test_disallow_instanciation(self):

`

``

905

`+

constructors = []

`

``

906

`+

try:

`

``

907

`+

import _md5

`

``

908

`+

constructors.append(_md5.md5)

`

``

909

`+

except ImportError:

`

``

910

`+

pass

`

``

911

`+

try:

`

``

912

`+

import _sha1

`

``

913

`+

constructors.append(_sha1.sha1)

`

``

914

`+

except ImportError:

`

``

915

`+

pass

`

``

916

`+

try:

`

``

917

`+

import _sha256

`

``

918

`+

constructors.append(_sha256.sha224)

`

``

919

`+

constructors.append(_sha256.sha256)

`

``

920

`+

except ImportError:

`

``

921

`+

pass

`

``

922

`+

try:

`

``

923

`+

import _sha512

`

``

924

`+

constructors.append(_sha512.sha384)

`

``

925

`+

constructors.append(_sha512.sha512)

`

``

926

`+

except ImportError:

`

``

927

`+

pass

`

``

928

+

``

929

`+

for constructor in constructors:

`

``

930

`+

h = constructor()

`

``

931

`+

with self.subTest(constructor=constructor):

`

``

932

`+

hash_type = type(h)

`

``

933

`+

self.assertRaises(TypeError, hash_type)

`

``

934

+

904

935

`@unittest.skipUnless(HASH is not None, 'need _hashlib')

`

905

``

`-

def test_internal_types(self):

`

``

936

`+

def test_hash_disallow_instanciation(self):

`

906

937

`# internal types like _hashlib.HASH are not constructable

`

907

938

`with self.assertRaisesRegex(

`

908

939

`TypeError, "cannot create '_hashlib.HASH' instance"

`