From bacf23f3edfa2ce880b7cd740d63f10058732fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20Jenei?= <35337337+SlaushVunter@users.noreply.github.com> Date: Mon, 13 Sep 2021 11:08:34 +0200 Subject: [PATCH] _ModuleWithDeprecations should inherit from types.ModuleType (#6267) (#6268) * _ModuleWithDeprecations should inherit from types.ModuleType (#6267) Update utils.py * fix typos reported by black * flake8 fix * Test should fail when int_from_bytes will be removed. Because this test would become pointless then. --- src/cryptography/utils.py | 4 +++- tests/test_utils.py | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py index 3cfd32e98..2ce68f7ce 100644 --- a/src/cryptography/utils.py +++ b/src/cryptography/utils.py @@ -7,6 +7,7 @@ import abc import enum import inspect import sys +import types import typing import warnings @@ -113,8 +114,9 @@ class _DeprecatedValue(object): self.warning_class = warning_class -class _ModuleWithDeprecations(object): +class _ModuleWithDeprecations(types.ModuleType): def __init__(self, module): + super().__init__(module.__name__) self.__dict__["_module"] = module def __getattr__(self, attr): diff --git a/tests/test_utils.py b/tests/test_utils.py index 389638f15..ee411c367 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,6 +4,7 @@ import binascii +import inspect import os import textwrap @@ -12,6 +13,7 @@ import pretend import pytest import cryptography +import cryptography.utils from cryptography.exceptions import UnsupportedAlgorithm, _Reasons import cryptography_vectors @@ -4444,3 +4446,13 @@ def test_raises_unsupported_algorithm(): "An error.", _Reasons.BACKEND_MISSING_INTERFACE ) assert exc_info.type is UnsupportedAlgorithm + + +def test_inspect_deprecated_module(): + # Check if inspection is supported by _ModuleWithDeprecations. + assert isinstance( + cryptography.utils, cryptography.utils._ModuleWithDeprecations + ) + source_file = inspect.getsourcefile(cryptography.utils) + assert isinstance(source_file, str) + assert source_file.endswith("utils.py")