Skip to content

Commit d583f55

Browse files
Commit
1 parent 8c30b2c commit d583f55

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

Lib/_pydecimal.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6402,3 +6402,11 @@ def _format_number(is_negative, intpart, fracpart, exp, spec):
64026402
# _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
64036403
_PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
64046404
del sys
6405+
6406+
def __getattr__(name):
6407+
if name == "__version__":
6408+
from warnings import _deprecated
6409+
6410+
_deprecated("__version__", remove=(3, 20))
6411+
return "1.70" # Do not change
6412+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

Lib/decimal.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,12 @@
9797
1
9898
>>>
9999
"""
100-
101100
try:
102101
from _decimal import *
103102
from _decimal import __libmpdec_version__ # noqa: F401
103+
from _decimal import __getattr__ # noqa: F401
104104
except ImportError:
105105
import _pydecimal
106106
import sys
107107
_pydecimal.__doc__ = __doc__
108108
sys.modules[__name__] = _pydecimal
109-
110-
def __getattr__(name):
111-
if name == "__version__":
112-
from warnings import _deprecated
113-
114-
_deprecated("__version__", remove=(3, 20))
115-
return "1.70" # Do not change
116-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

Lib/test/test_decimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4474,7 +4474,7 @@ def test_module_attributes(self):
44744474
self.assertTrue(C.HAVE_THREADS is True or C.HAVE_THREADS is False)
44754475
self.assertTrue(P.HAVE_THREADS is True or P.HAVE_THREADS is False)
44764476

4477-
self.assertEqual(C.__version__, P.__version__)
4477+
self.assertEqual(C.SPEC_VERSION, P.SPEC_VERSION)
44784478

44794479
self.assertLessEqual(set(dir(C)), set(dir(P)))
44804480
self.assertEqual([n for n in dir(C) if n[:2] != '__'], sorted(P.__all__))

Modules/_decimal/_decimal.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7566,12 +7566,35 @@ static PyType_Spec context_spec = {
75667566
};
75677567

75687568

7569+
static PyObject *
7570+
decimal_getattr(PyObject *self, PyObject *args)
7571+
{
7572+
PyObject *name;
7573+
if (!PyArg_UnpackTuple(args, "__getattr__", 1, 1, &name)) {
7574+
return NULL;
7575+
}
7576+
7577+
if (PyUnicode_Check(name) && PyUnicode_EqualToUTF8(name, "__version__")) {
7578+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
7579+
"'__version__' is deprecated and slated for removal in Python 3.20",
7580+
1) < 0) {
7581+
return NULL;
7582+
}
7583+
return PyUnicode_FromString("1.7");
7584+
}
7585+
7586+
PyErr_Format(PyExc_AttributeError, "module 'decimal' has no attribute %R", name);
7587+
return NULL;
7588+
}
7589+
7590+
75697591
static PyMethodDef _decimal_methods [] =
75707592
{
75717593
_DECIMAL_GETCONTEXT_METHODDEF
75727594
_DECIMAL_SETCONTEXT_METHODDEF
75737595
_DECIMAL_LOCALCONTEXT_METHODDEF
75747596
_DECIMAL_IEEECONTEXT_METHODDEF
7597+
{"__getattr__", decimal_getattr, METH_VARARGS, "Module __getattr__"},
75757598
{ NULL, NULL, 1, NULL }
75767599
};
75777600

0 commit comments

Comments
 (0)