Skip to content

Commit

Permalink
bpo-34838: Use subclass_of for math.dist. (GH-9659)
Browse files Browse the repository at this point in the history
Argument clinic now generates fast inline code for
positional parsing, so the manually implemented type
check in math.dist can be removed.
  • Loading branch information
ammaraskar authored and serhiy-storchaka committed Jan 12, 2019
1 parent fdf282d commit cb08a71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Lib/test/test_math.py
Expand Up @@ -867,6 +867,8 @@ class T(tuple):
dist((1, 2, 3, 4), (5, 6, 7))
with self.assertRaises(ValueError): # Check dimension agree
dist((1, 2, 3), (4, 5, 6, 7))
with self.assertRaises(TypeError): # Rejects invalid types
dist("abc", "xyz")

# Verify that the one dimensional case is equivalent to abs()
for i in range(20):
Expand Down
10 changes: 9 additions & 1 deletion Modules/clinic/mathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions Modules/mathmodule.c
Expand Up @@ -2101,8 +2101,8 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
/*[clinic input]
math.dist
p: object
q: object
p: object(subclass_of='&PyTuple_Type')
q: object(subclass_of='&PyTuple_Type')
/
Return the Euclidean distance between two points p and q.
Expand All @@ -2116,7 +2116,7 @@ Roughly equivalent to:

static PyObject *
math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
/*[clinic end generated code: output=56bd9538d06bbcfe input=8c83c07c7a524664]*/
/*[clinic end generated code: output=56bd9538d06bbcfe input=937122eaa5f19272]*/
{
PyObject *item;
double max = 0.0;
Expand All @@ -2126,11 +2126,6 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
double diffs_on_stack[NUM_STACK_ELEMS];
double *diffs = diffs_on_stack;

if (!PyTuple_Check(p) || !PyTuple_Check(q)) {
PyErr_SetString(PyExc_TypeError, "dist argument must be a tuple");
return NULL;
}

m = PyTuple_GET_SIZE(p);
n = PyTuple_GET_SIZE(q);
if (m != n) {
Expand Down

0 comments on commit cb08a71

Please sign in to comment.