Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-61103: Support float and long double complex types in ctypes module#121248
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
20355d6e2fe2f5a8f25ea16bb4bde9fe1320bb9ba3061c45bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -33,6 +33,20 @@ def test_csqrt(self): | ||
| self.assertAlmostEqual(lib.my_csqrt(-1-0.01j), | ||
| 0.004999937502734214-1.0000124996093955j) | ||
| lib.my_csqrtf.argtypes = ctypes.c_float_complex, | ||
| lib.my_csqrtf.restype = ctypes.c_float_complex | ||
| self.assertAlmostEqual(lib.my_csqrtf(-1+0.01j), | ||
| 0.004999937502734214+1.0000124996093955j) | ||
| self.assertAlmostEqual(lib.my_csqrtf(-1-0.01j), | ||
| 0.004999937502734214-1.0000124996093955j) | ||
skirpichev marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| lib.my_csqrtl.argtypes = ctypes.c_longdouble_complex, | ||
| lib.my_csqrtl.restype = ctypes.c_longdouble_complex | ||
| self.assertAlmostEqual(lib.my_csqrtl(-1+0.01j), | ||
| 0.004999937502734214+1.0000124996093955j) | ||
| self.assertAlmostEqual(lib.my_csqrtl(-1-0.01j), | ||
| 0.004999937502734214-1.0000124996093955j) | ||
| def test_qsort(self): | ||
| comparefunc = CFUNCTYPE(c_int, POINTER(c_char), POINTER(c_char)) | ||
| lib.my_qsort.argtypes = c_void_p, c_size_t, c_size_t, comparefunc | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -146,7 +146,8 @@ def test_floats(self): | ||
| @unittest.skipUnless(hasattr(ctypes, "c_double_complex"), | ||
| "requires C11 complex type") | ||
| def test_complex(self): | ||
| for t in [ctypes.c_double_complex]: | ||
| for t in [ctypes.c_double_complex, ctypes.c_float_complex, | ||
| ctypes.c_longdouble_complex]: | ||
skirpichev marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| self.assertEqual(t(1).value, 1+0j) | ||
| self.assertEqual(t(1.0).value, 1+0j) | ||
| self.assertEqual(t(1+0.125j).value, 1+0.125j) | ||
| @@ -162,9 +163,10 @@ def test_complex_round_trip(self): | ||
| values = [complex(*_) for _ in combinations([1, -1, 0.0, -0.0, 2, | ||
| -3, INF, -INF, NAN], 2)] | ||
| for z in values: | ||
| with self.subTest(z=z): | ||
| z2 = ctypes.c_double_complex(z).value | ||
| self.assertComplexesAreIdentical(z, z2) | ||
| for t in [ctypes.c_double_complex, ctypes.c_float_complex, | ||
| ctypes.c_longdouble_complex]: | ||
| with self.subTest(z=z, type=t): | ||
| self.assertComplexesAreIdentical(z, t(z).value) | ||
| def test_integers(self): | ||
| f = FloatLike() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| Support :c:expr:`double complex` C type in :mod:`ctypes` via | ||
| :class:`~ctypes.c_double_complex` if compiler has C11 complex | ||
| arithmetic. Patch by Sergey B Kirpichev. | ||
| Support :c:expr:`float complex`, :c:expr:`double complex` and | ||
| :c:expr:`long double complex` C types in :mod:`ctypes` as | ||
| :class:`~ctypes.c_float_complex`, :class:`~ctypes.c_double_complex` and | ||
| :class:`~ctypes.c_longdouble_complex` if the compiler has C11 complex arithmetic. | ||
| Patch by Sergey B Kirpichev. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,6 +21,8 @@ | ||
| #if !defined(CMPLX) | ||
| # if defined(__clang__) && __has_builtin(__builtin_complex) | ||
| # define CMPLX(x, y) __builtin_complex ((double) (x), (double) (y)) | ||
| # define CMPLXF(x, y) __builtin_complex ((float) (x), (float) (y)) | ||
| # define CMPLXL(x, y) __builtin_complex ((long double) (x), (long double) (y)) | ||
| # else | ||
| static inline double complex | ||
| CMPLX(double real, double imag) | ||
| @@ -30,5 +32,23 @@ CMPLX(double real, double imag) | ||
| ((double *)(&z))[1] = imag; | ||
| return z; | ||
| } | ||
skirpichev marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| static inline float complex | ||
| CMPLXF(float real, float imag) | ||
| { | ||
| float complex z; | ||
| ((float *)(&z))[0] = real; | ||
| ((float *)(&z))[1] = imag; | ||
| return z; | ||
| } | ||
skirpichev marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| static inline long double complex | ||
| CMPLXL(long double real, long double imag) | ||
| { | ||
| long double complex z; | ||
| ((long double *)(&z))[0] = real; | ||
| ((long double *)(&z))[1] = imag; | ||
| return z; | ||
| } | ||
| # endif | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -454,6 +454,16 @@ EXPORT(double complex) my_csqrt(double complex a) | ||
| { | ||
| return csqrt(a); | ||
| } | ||
skirpichev marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| EXPORT(float complex) my_csqrtf(float complex a) | ||
| { | ||
| return csqrtf(a); | ||
| } | ||
skirpichev marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| EXPORT(long double complex) my_csqrtl(long double complex a) | ||
| { | ||
| return csqrtl(a); | ||
| } | ||
| #endif | ||
| EXPORT(void) my_qsort(void *base, size_t num, size_t width, int(*compare)(const void*, const void*)) | ||
Uh oh!
There was an error while loading. Please reload this page.