← Back to DAGDA | Support | Source: https://github.com/python/cpython/commit/cb08a71c5c534f33d9486677534dafb087c30e8c
Skip to content

Commit cb08a71

Browse files
ammaraskarserhiy-storchaka
authored andcommitted
bpo-34838: Use subclass_of for math.dist. (GH-9659)
Argument clinic now generates fast inline code for positional parsing, so the manually implemented type check in math.dist can be removed.
1 parent fdf282d commit cb08a71

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎Lib/test/test_math.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ class T(tuple):
867867
dist((1, 2, 3, 4), (5, 6, 7))
868868
with self.assertRaises(ValueError): # Check dimension agree
869869
dist((1, 2, 3), (4, 5, 6, 7))
870+
with self.assertRaises(TypeError): # Rejects invalid types
871+
dist("abc", "xyz")
870872

871873
# Verify that the one dimensional case is equivalent to abs()
872874
for i in range(20):

‎Modules/clinic/mathmodule.c.h‎

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/mathmodule.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,8 +2101,8 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
21012101
/*[clinic input]
21022102
math.dist
21032103
2104-
p: object
2105-
q: object
2104+
p: object(subclass_of='&PyTuple_Type')
2105+
q: object(subclass_of='&PyTuple_Type')
21062106
/
21072107
21082108
Return the Euclidean distance between two points p and q.
@@ -2116,7 +2116,7 @@ Roughly equivalent to:
21162116

21172117
static PyObject *
21182118
math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
2119-
/*[clinic end generated code: output=56bd9538d06bbcfe input=8c83c07c7a524664]*/
2119+
/*[clinic end generated code: output=56bd9538d06bbcfe input=937122eaa5f19272]*/
21202120
{
21212121
PyObject *item;
21222122
double max = 0.0;
@@ -2126,11 +2126,6 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
21262126
double diffs_on_stack[NUM_STACK_ELEMS];
21272127
double *diffs = diffs_on_stack;
21282128

2129-
if (!PyTuple_Check(p) || !PyTuple_Check(q)) {
2130-
PyErr_SetString(PyExc_TypeError, "dist argument must be a tuple");
2131-
return NULL;
2132-
}
2133-
21342129
m = PyTuple_GET_SIZE(p);
21352130
n = PyTuple_GET_SIZE(q);
21362131
if (m != n) {

0 commit comments

Comments
 (0)