Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-109802: removed inaccessible code from complexobject.c #109642

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
facb60b
gh-109802: Increase test coverage for complexobject.c
skirpichev Sep 20, 2023
deff888
* c_powu: L166
skirpichev Sep 20, 2023
8a7b344
* complex_hash: L411, L414, L423
skirpichev Sep 20, 2023
e383167
* to_complex: L443, L449, L453-454
skirpichev Sep 20, 2023
85fae1a
* complex_sub: L474, L475
skirpichev Sep 20, 2023
406c375
* complex_mul: L485, L486
skirpichev Sep 20, 2023
aba6fb4
* complex_div: L496, L497
skirpichev Sep 20, 2023
204e2ec
* complex_pow: L512, L513, L522
skirpichev Sep 20, 2023
b67e8d6
* complex_pos: L555-L559
skirpichev Sep 20, 2023
85f6994
* complex_richcompare: L595, L616, L621, L625
skirpichev Sep 20, 2023
ffd088a
* complex_from_string_inner: L786, L804
skirpichev Sep 20, 2023
72b349d
* complex_subtype_from_string: L860, L871-L874 (the function called i…
skirpichev Sep 20, 2023
c25563c
* complex_new_impl: L944, L951, L958, L965, L985, L994
skirpichev Sep 20, 2023
e1c247f
* complex_bool: L580
skirpichev Sep 21, 2023
0a8679f
Merge branch 'main' into complex-cov
skirpichev Sep 24, 2023
f96c0ce
Merge branch 'main' into complex-cov
skirpichev Sep 25, 2023
fbd035d
Merge branch 'main' into complex-cov
skirpichev Nov 11, 2023
64f2174
Apply suggestions from code review
skirpichev Nov 12, 2023
b99dcfc
Merge branch 'main' into complex-cov
skirpichev Nov 12, 2023
2ef7d25
address review: self.assertAlmostEqual -> self.assertEqual (except one)
skirpichev Nov 12, 2023
75fda9c
address review: spam support classes
skirpichev Nov 12, 2023
36ee551
address review: comment on assert in complex_new_impl()
skirpichev Nov 12, 2023
aafe16e
address review: revert _Py_c_pow()
skirpichev Nov 12, 2023
f88c4bc
address review: hash test
skirpichev Nov 12, 2023
443efd5
Merge branch 'complex-cov' of github.com:skirpichev/cpython into comp…
skirpichev Nov 12, 2023
a2b6e73
* _Py_c_pow: L135-136, also other tests for _Py_c_* C API
skirpichev Nov 13, 2023
32d9f7f
+1
skirpichev Nov 13, 2023
9e21cc1
address review:
skirpichev Nov 14, 2023
3494b0d
address review: comment in complex_hash()
skirpichev Nov 14, 2023
17236ee
address review: simplify _PY_C_FUNC2 macro
skirpichev Nov 14, 2023
f7c9b0b
Merge branch 'main' into complex-cov
skirpichev Nov 16, 2023
aea52ed
Drop pycore_complexobject.h
skirpichev Nov 16, 2023
589a2e4
+ test _Py_c_abs()
skirpichev Nov 16, 2023
2f63950
Oops, typo fixed
skirpichev Nov 27, 2023
f73c907
address review: check for failures in test code before PyTuple_Pack'ing
skirpichev Nov 27, 2023
86c3647
address review: use _testcapi.DBL_MAX instead of magic numbers
skirpichev Nov 27, 2023
e2bdcec
Merge branch 'main' into complex-cov
skirpichev Nov 27, 2023
deace0f
Amend f73c90731f
skirpichev Nov 27, 2023
f261392
+ use Py_BuildValue
skirpichev Nov 27, 2023
899c656
Merge branch 'main' into complex-cov
skirpichev Nov 28, 2023
0a725e8
Merge branch 'main' into complex-cov
skirpichev Dec 8, 2023
8649755
* complex_abs: L569
skirpichev Dec 9, 2023
c5f6e34
Merge branch 'main' into complex-cov
skirpichev Feb 1, 2024
dad56e8
Merge branch 'main' into complex-cov
skirpichev Feb 1, 2024
c4a3af2
Merge branch 'main' into complex-cov
skirpichev Feb 22, 2024
5be59f4
Merge branch 'main' into complex-cov
skirpichev Apr 5, 2024
2d9247e
Merge branch 'main' into complex-cov
skirpichev May 6, 2024
61a720e
Merge branch 'main' into complex-cov
skirpichev May 31, 2024
2a25b9a
Update wrt recent changes
skirpichev May 31, 2024
1817c3b
Merge branch 'main' into complex-cov
skirpichev May 31, 2024
1781c95
Merge branch 'main' into complex-cov
skirpichev Jul 24, 2024
d9f2c67
Apply suggestions from code review
skirpichev Oct 6, 2024
6cad37a
Update Objects/complexobject.c
skirpichev Oct 6, 2024
f1f3d97
Apply suggestions from code review
skirpichev Oct 6, 2024
7ef464d
Merge branch 'master' into complex-cov
skirpichev Oct 13, 2024
65c30f1
Add test for complex_from_number
skirpichev Oct 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ def __int__(self):
self.assertRaises(TypeError, complex, MyInt())
self.assertRaises(TypeError, complex, MyInt(), 1.5)
self.assertRaises(TypeError, complex, 1.5, MyInt())
self.assertRaises(TypeError, complex, object())
with self.assertRaises(TypeError):
complex(real=object())

class complex0(complex):
"""Test usage of __complex__() when inheriting from 'complex'"""
Expand Down
62 changes: 25 additions & 37 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,18 @@ _Py_c_pow(Py_complex a, Py_complex b)
return r;
}

#define C_EXP_CUTOFF 100
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

static Py_complex
c_powu(Py_complex x, long n)
{
Py_complex r, p;
long mask = 1;
r = c_1;
p = x;
while (mask > 0 && n >= mask) {
assert(0 <= n && n <= C_EXP_CUTOFF);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
while (n >= mask) {
assert(mask>0);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
if (n & mask)
r = _Py_c_prod(r,p);
mask <<= 1;
Expand Down Expand Up @@ -454,11 +458,10 @@ complex_hash(PyComplexObject *v)
{
Py_uhash_t hashreal, hashimag, combined;
hashreal = (Py_uhash_t)_Py_HashDouble((PyObject *) v, v->cval.real);
if (hashreal == (Py_uhash_t)-1)
return -1;
hashimag = (Py_uhash_t)_Py_HashDouble((PyObject *)v, v->cval.imag);
if (hashimag == (Py_uhash_t)-1)
return -1;
/* In current implementation of hashing for numberic types,
* -1 is reserved. */
assert(hashreal != (Py_uhash_t)-1 && hashimag != (Py_uhash_t)-1);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
/* Note: if the imaginary part is 0, hashimag is 0 now,
* so the following returns hashreal unchanged. This is
* important because numbers of different types that
Expand Down Expand Up @@ -565,7 +568,7 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
errno = 0;
// Check whether the exponent has a small integer value, and if so use
// a faster and more accurate algorithm.
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) {
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= C_EXP_CUTOFF) {
p = c_powi(a, (long)b.real);
}
else {
Expand Down Expand Up @@ -638,7 +641,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
}

assert(PyComplex_Check(v));
TO_COMPLEX(v, i);
i = ((PyComplexObject*)v)->cval;
skirpichev marked this conversation as resolved.
Show resolved Hide resolved

if (PyLong_Check(w)) {
/* Check for 0.0 imaginary part first to avoid the rich
Expand All @@ -664,7 +667,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
else if (PyComplex_Check(w)) {
Py_complex j;

TO_COMPLEX(w, j);
j = ((PyComplexObject*)w)->cval;
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
equal = (i.real == j.real && i.imag == j.imag);
}
else {
Expand Down Expand Up @@ -887,22 +890,15 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
PyObject *s_buffer = NULL, *result = NULL;
Py_ssize_t len;

if (PyUnicode_Check(v)) {
s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
if (s_buffer == NULL) {
return NULL;
}
assert(PyUnicode_IS_ASCII(s_buffer));
/* Simply get a pointer to existing ASCII characters. */
s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
assert(s != NULL);
}
else {
PyErr_Format(PyExc_TypeError,
"complex() argument must be a string or a number, not %T",
v);
assert(PyUnicode_Check(v));
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v);
if (s_buffer == NULL) {
return NULL;
}
assert(PyUnicode_IS_ASCII(s_buffer));
/* Simply get a pointer to existing ASCII characters. */
s = PyUnicode_AsUTF8AndSize(s_buffer, &len);
assert(s != NULL);

result = _Py_string_to_number_with_underscores(s, len, "complex", v, type,
complex_from_string_inner);
Expand Down Expand Up @@ -955,13 +951,6 @@ actual_complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
else if (PyErr_Occurred()) {
return NULL;
}
else if (PyComplex_Check(arg)) {
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
/* Note that if arg is of a complex subtype, we're only
retaining its real & imag parts here, and the return
value is (properly) of the builtin complex type. */
Py_complex c = ((PyComplexObject*)arg)->cval;
res = complex_subtype_from_doubles(type, c.real, c.imag);
}
else if ((nbr = Py_TYPE(arg)->tp_as_number) != NULL &&
(nbr->nb_float != NULL || nbr->nb_index != NULL))
{
Expand Down Expand Up @@ -1029,9 +1018,9 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
PyErr_Format(PyExc_TypeError,
"complex() argument 'real' must be a real number, not %T",
r);
if (own_r) {
Py_DECREF(r);
}
/* Here r is not a complex subtype, hence above
try_complex_special_method() call was unsuccessful. */
assert(!own_r);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
if (i != NULL) {
Expand Down Expand Up @@ -1063,11 +1052,10 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
value is (properly) of the builtin complex type. */
cr = ((PyComplexObject*)r)->cval;
cr_is_complex = 1;
if (own_r) {
/* r was a newly created complex number, rather
than the original "real" argument. */
Py_DECREF(r);
}
assert(own_r);
/* r was a newly created complex number, rather
than the original "real" argument. */
Py_DECREF(r);
skirpichev marked this conversation as resolved.
Show resolved Hide resolved
nbr = Py_TYPE(orig_r)->tp_as_number;
if (nbr == NULL ||
(nbr->nb_float == NULL && nbr->nb_index == NULL))
Expand Down
Loading