mirror of
https://github.com/python/cpython.git
synced 2025-09-18 22:50:26 +00:00
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.
This commit is contained in:
parent
fdf282d609
commit
cb08a71c5c
3 changed files with 14 additions and 9 deletions
|
@ -867,6 +867,8 @@ class MathTests(unittest.TestCase):
|
||||||
dist((1, 2, 3, 4), (5, 6, 7))
|
dist((1, 2, 3, 4), (5, 6, 7))
|
||||||
with self.assertRaises(ValueError): # Check dimension agree
|
with self.assertRaises(ValueError): # Check dimension agree
|
||||||
dist((1, 2, 3), (4, 5, 6, 7))
|
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()
|
# Verify that the one dimensional case is equivalent to abs()
|
||||||
for i in range(20):
|
for i in range(20):
|
||||||
|
|
10
Modules/clinic/mathmodule.c.h
generated
10
Modules/clinic/mathmodule.c.h
generated
|
@ -310,7 +310,15 @@ math_dist(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
||||||
if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) {
|
if (!_PyArg_CheckPositional("dist", nargs, 2, 2)) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
if (!PyTuple_Check(args[0])) {
|
||||||
|
_PyArg_BadArgument("dist", 1, "tuple", args[0]);
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
p = args[0];
|
p = args[0];
|
||||||
|
if (!PyTuple_Check(args[1])) {
|
||||||
|
_PyArg_BadArgument("dist", 2, "tuple", args[1]);
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
q = args[1];
|
q = args[1];
|
||||||
return_value = math_dist_impl(module, p, q);
|
return_value = math_dist_impl(module, p, q);
|
||||||
|
|
||||||
|
@ -548,4 +556,4 @@ math_isclose(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=f3264ab0ef57ba0a input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=0664f30046da09fe input=a9049054013a1b77]*/
|
||||||
|
|
|
@ -2101,8 +2101,8 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
math.dist
|
math.dist
|
||||||
|
|
||||||
p: object
|
p: object(subclass_of='&PyTuple_Type')
|
||||||
q: object
|
q: object(subclass_of='&PyTuple_Type')
|
||||||
/
|
/
|
||||||
|
|
||||||
Return the Euclidean distance between two points p and q.
|
Return the Euclidean distance between two points p and q.
|
||||||
|
@ -2116,7 +2116,7 @@ Roughly equivalent to:
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
|
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;
|
PyObject *item;
|
||||||
double max = 0.0;
|
double max = 0.0;
|
||||||
|
@ -2126,11 +2126,6 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
|
||||||
double diffs_on_stack[NUM_STACK_ELEMS];
|
double diffs_on_stack[NUM_STACK_ELEMS];
|
||||||
double *diffs = diffs_on_stack;
|
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);
|
m = PyTuple_GET_SIZE(p);
|
||||||
n = PyTuple_GET_SIZE(q);
|
n = PyTuple_GET_SIZE(q);
|
||||||
if (m != n) {
|
if (m != n) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue