Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitefbe0bb

Browse files
committed
gh-61103: don't use native complex types to implement F/D/G in ctypes
* drop _complex.h header* use appropriate real arrays to replace complex types
1 parent66d7e54 commitefbe0bb

File tree

5 files changed

+32
-91
lines changed

5 files changed

+32
-91
lines changed

‎Makefile.pre.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3313,7 +3313,7 @@ MODULE_CMATH_DEPS=$(srcdir)/Modules/_math.h
33133313
MODULE_MATH_DEPS=$(srcdir)/Modules/_math.h
33143314
MODULE_PYEXPAT_DEPS=@LIBEXPAT_INTERNAL@
33153315
MODULE_UNICODEDATA_DEPS=$(srcdir)/Modules/unicodedata_db.h $(srcdir)/Modules/unicodename_db.h
3316-
MODULE__CTYPES_DEPS=$(srcdir)/Modules/_ctypes/ctypes.h $(srcdir)/Modules/_complex.h
3316+
MODULE__CTYPES_DEPS=$(srcdir)/Modules/_ctypes/ctypes.h
33173317
MODULE__CTYPES_TEST_DEPS=$(srcdir)/Modules/_ctypes/_ctypes_test_generated.c.h
33183318
MODULE__CTYPES_MALLOC_CLOSURE=@MODULE__CTYPES_MALLOC_CLOSURE@
33193319
MODULE__DECIMAL_DEPS=$(srcdir)/Modules/_decimal/docstrings.h @LIBMPDEC_INTERNAL@

‎Modules/_complex.h

Lines changed: 0 additions & 54 deletions
This file was deleted.

‎Modules/_ctypes/callproc.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ module _ctypes
103103
#include"pycore_global_objects.h"// _Py_ID()
104104
#include"pycore_traceback.h"// _PyTraceback_Add()
105105

106-
#if defined(Py_HAVE_C_COMPLEX)&& defined(Py_FFI_SUPPORT_C_COMPLEX)
107-
#include"../_complex.h"// complex
108-
#endif
109106
#defineclinic_state() (get_module_state(module))
110107
#include"clinic/callproc.c.h"
111108
#undef clinic_state
@@ -652,11 +649,9 @@ union result {
652649
doubled;
653650
floatf;
654651
void*p;
655-
#if defined(Py_HAVE_C_COMPLEX)&& defined(Py_FFI_SUPPORT_C_COMPLEX)
656-
doublecomplexD;
657-
floatcomplexF;
658-
longdoublecomplexG;
659-
#endif
652+
doubleD[2];
653+
floatF[2];
654+
longdoubleG[2];
660655
};
661656

662657
structargument {

‎Modules/_ctypes/cfield.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
#include<ffi.h>
1515
#include"ctypes.h"
1616

17-
#if defined(Py_HAVE_C_COMPLEX)&& defined(Py_FFI_SUPPORT_C_COMPLEX)
18-
# include"../_complex.h"// complex
19-
#endif
20-
2117
#defineCTYPES_CFIELD_CAPSULE_NAME_PYMEM "_ctypes/cfield.c pymem"
2218

2319
/*[clinic input]
@@ -763,80 +759,87 @@ d_get(void *ptr, Py_ssize_t size)
763759
returnPyFloat_FromDouble(val);
764760
}
765761

766-
#if defined(Py_HAVE_C_COMPLEX)&& defined(Py_FFI_SUPPORT_C_COMPLEX)
762+
#if defined(Py_FFI_SUPPORT_C_COMPLEX)
763+
764+
/* We don't use Annex G complex types here, using arrays instead, as the C17+
765+
standard says: "Each complex type has the same representation and alignment
766+
requirements as an array type containing exactly two elements of the
767+
corresponding real type; the first element is equal to the real part, and
768+
the second element to the imaginary part, of the complex number." */
769+
767770
/* D: double complex */
768771
staticPyObject*
769772
D_set(void*ptr,PyObject*value,Py_ssize_tsize)
770773
{
771-
assert(NUM_BITS(size)|| (size==sizeof(doublecomplex)));
774+
assert(NUM_BITS(size)|| (size==2*sizeof(double)));
772775
Py_complexc=PyComplex_AsCComplex(value);
773776

774777
if (c.real==-1&&PyErr_Occurred()) {
775778
returnNULL;
776779
}
777-
doublecomplexx=CMPLX(c.real,c.imag);
780+
doublex[2]={c.real,c.imag};
778781
memcpy(ptr,&x,sizeof(x));
779782
_RET(value);
780783
}
781784

782785
staticPyObject*
783786
D_get(void*ptr,Py_ssize_tsize)
784787
{
785-
assert(NUM_BITS(size)|| (size==sizeof(doublecomplex)));
786-
doublecomplexx;
788+
assert(NUM_BITS(size)|| (size==2*sizeof(double)));
789+
doublex[2];
787790

788791
memcpy(&x,ptr,sizeof(x));
789-
returnPyComplex_FromDoubles(creal(x),cimag(x));
792+
returnPyComplex_FromDoubles(x[0],x[1]);
790793
}
791794

792795
/* F: float complex */
793796
staticPyObject*
794797
F_set(void*ptr,PyObject*value,Py_ssize_tsize)
795798
{
796-
assert(NUM_BITS(size)|| (size==sizeof(floatcomplex)));
799+
assert(NUM_BITS(size)|| (size==2*sizeof(float)));
797800
Py_complexc=PyComplex_AsCComplex(value);
798801

799802
if (c.real==-1&&PyErr_Occurred()) {
800803
returnNULL;
801804
}
802-
floatcomplexx=CMPLXF((float)c.real, (float)c.imag);
805+
floatx[2]={(float)c.real, (float)c.imag};
803806
memcpy(ptr,&x,sizeof(x));
804807
_RET(value);
805808
}
806809

807810
staticPyObject*
808811
F_get(void*ptr,Py_ssize_tsize)
809812
{
810-
assert(NUM_BITS(size)|| (size==sizeof(floatcomplex)));
811-
floatcomplexx;
813+
assert(NUM_BITS(size)|| (size==2*sizeof(float)));
814+
floatx[2];
812815

813816
memcpy(&x,ptr,sizeof(x));
814-
returnPyComplex_FromDoubles(crealf(x),cimagf(x));
817+
returnPyComplex_FromDoubles(x[0],x[1]);
815818
}
816819

817820
/* G: long double complex */
818821
staticPyObject*
819822
G_set(void*ptr,PyObject*value,Py_ssize_tsize)
820823
{
821-
assert(NUM_BITS(size)|| (size==sizeof(longdoublecomplex)));
824+
assert(NUM_BITS(size)|| (size==2*sizeof(longdouble)));
822825
Py_complexc=PyComplex_AsCComplex(value);
823826

824827
if (c.real==-1&&PyErr_Occurred()) {
825828
returnNULL;
826829
}
827-
longdoublecomplexx=CMPLXL(c.real,c.imag);
830+
longdoublex[2]={c.real,c.imag};
828831
memcpy(ptr,&x,sizeof(x));
829832
_RET(value);
830833
}
831834

832835
staticPyObject*
833836
G_get(void*ptr,Py_ssize_tsize)
834837
{
835-
assert(NUM_BITS(size)|| (size==sizeof(longdoublecomplex)));
836-
longdoublecomplexx;
838+
assert(NUM_BITS(size)|| (size==2*sizeof(longdouble)));
839+
longdoublex[2];
837840

838841
memcpy(&x,ptr,sizeof(x));
839-
returnPyComplex_FromDoubles((double)creall(x), (double)cimagl(x));
842+
returnPyComplex_FromDoubles((double)x[0], (double)x[1]);
840843
}
841844
#endif
842845

@@ -1596,7 +1599,7 @@ for base_code, base_c_type in [
15961599
///////////////////////////////////////////////////////////////////////////
15971600

15981601
TABLE_ENTRY_SW(d,&ffi_type_double);
1599-
#if defined(Py_HAVE_C_COMPLEX)&& defined(Py_FFI_SUPPORT_C_COMPLEX)
1602+
#if defined(Py_FFI_SUPPORT_C_COMPLEX)
16001603
if (Py_FFI_COMPLEX_AVAILABLE) {
16011604
TABLE_ENTRY(D,&ffi_type_complex_double);
16021605
TABLE_ENTRY(F,&ffi_type_complex_float);

‎Modules/_ctypes/ctypes.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
// Do we support C99 complex types in ffi?
1313
// For Apple's libffi, this must be determined at runtime (see gh-128156).
14-
#if defined(Py_HAVE_C_COMPLEX)&& defined(Py_FFI_SUPPORT_C_COMPLEX)
15-
# include"../_complex.h"// complex
14+
#if defined(Py_FFI_SUPPORT_C_COMPLEX)
1615
# ifUSING_APPLE_OS_LIBFFI&& defined(__has_builtin)
1716
# if__has_builtin(__builtin_available)
1817
# definePy_FFI_COMPLEX_AVAILABLE __builtin_available(macOS 10.15, *)
@@ -493,11 +492,9 @@ struct tagPyCArgObject {
493492
doubled;
494493
floatf;
495494
void*p;
496-
#if defined(Py_HAVE_C_COMPLEX)&& defined(Py_FFI_SUPPORT_C_COMPLEX)
497-
doublecomplexD;
498-
floatcomplexF;
499-
longdoublecomplexG;
500-
#endif
495+
doubleD[2];
496+
floatF[2];
497+
longdoubleG[2];
501498
}value;
502499
PyObject*obj;
503500
Py_ssize_tsize;/* for the 'V' tag */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp