bpo-34838: Use subclass_of for math.dist. (GH-9659) · python/cpython@cb08a71 (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

Appearance settings

Commit cb08a71

ammaraskarserhiy-storchaka

authored and

committed

Argument clinic now generates fast inline code for positional parsing, so the manually implemented type check in math.dist can be removed.

File tree

3 files changed

lines changed

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -867,6 +867,8 @@ class T(tuple):
867 867 dist((1, 2, 3, 4), (5, 6, 7))
868 868 with self.assertRaises(ValueError): # Check dimension agree
869 869 dist((1, 2, 3), (4, 5, 6, 7))
870 +with self.assertRaises(TypeError): # Rejects invalid types
871 +dist("abc", "xyz")
870 872
871 873 # Verify that the one dimensional case is equivalent to abs()
872 874 for i in range(20):
Original file line number Diff line number Diff line change
@@ -2101,8 +2101,8 @@ vector_norm(Py_ssize_t n, double *vec, double max, int found_nan)
2101 2101 /*[clinic input]
2102 2102 math.dist
2103 2103
2104 - p: object
2105 - q: object
2104 + p: object(subclass_of='&PyTuple_Type')
2105 + q: object(subclass_of='&PyTuple_Type')
2106 2106 /
2107 2107
2108 2108 Return the Euclidean distance between two points p and q.
@@ -2116,7 +2116,7 @@ Roughly equivalent to:
2116 2116
2117 2117 static PyObject *
2118 2118 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]*/
2120 2120 {
2121 2121 PyObject *item;
2122 2122 double max = 0.0;
@@ -2126,11 +2126,6 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
2126 2126 double diffs_on_stack[NUM_STACK_ELEMS];
2127 2127 double *diffs = diffs_on_stack;
2128 2128
2129 -if (!PyTuple_Check(p) |
2130 -PyErr_SetString(PyExc_TypeError, "dist argument must be a tuple");
2131 -return NULL;
2132 - }
2133 -
2134 2129 m = PyTuple_GET_SIZE(p);
2135 2130 n = PyTuple_GET_SIZE(q);
2136 2131 if (m != n) {