summaryrefslogtreecommitdiffstats
path: root/Modules/mathmodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2018-09-29 21:30:38 (GMT)
committerGitHub <noreply@github.com>2018-09-29 21:30:38 (GMT)
commitdf8101517aa1c917fdf8aeb466e480c26d4e878c (patch)
tree5ad880fe895f2fbb0669678fcde28cd2fe8e4072 /Modules/mathmodule.c
parente45473e3ca31e5b78dc85cab575f5bb60d5b7f8f (diff)
downloadcpython-df8101517aa1c917fdf8aeb466e480c26d4e878c.zip
cpython-df8101517aa1c917fdf8aeb466e480c26d4e878c.tar.gz
cpython-df8101517aa1c917fdf8aeb466e480c26d4e878c.tar.bz2
Speed-up math.dist() by 30% (GH-9628)
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r--Modules/mathmodule.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index e872e47..e956314 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2101,8 +2101,8 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
/*[clinic input]
math.dist
- p: object(subclass_of='&PyTuple_Type')
- q: object(subclass_of='&PyTuple_Type')
+ p: object
+ q: object
/
Return the Euclidean distance between two points p and q.
@@ -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=937122eaa5f19272]*/
+/*[clinic end generated code: output=56bd9538d06bbcfe input=8c83c07c7a524664]*/
{
PyObject *item;
double max = 0.0;
@@ -2126,6 +2126,11 @@ 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) {