Title: Add support for C99 complex type (_Complex) to the struct module · Issue #121249 · python/cpython · GitHub
Open Graph Title: Add support for C99 complex type (_Complex) to the struct module · Issue #121249 · python/cpython
X Title: Add support for C99 complex type (_Complex) to the struct module · Issue #121249 · python/cpython
Description: Feature or enhancement Proposal: The struct module has support for float and double types, so at least there should be also float _Complex and double _Complex. I'll work on a patch. Initial version diff --git a/Lib/ctypes/__init__.py b/L...
Open Graph Description: Feature or enhancement Proposal: The struct module has support for float and double types, so at least there should be also float _Complex and double _Complex. I'll work on a patch. Initial version...
X Description: Feature or enhancement Proposal: The struct module has support for float and double types, so at least there should be also float _Complex and double _Complex. I'll work on a patch. Initial ver...
Opengraph URL: https://github.com/python/cpython/issues/121249
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add support for C99 complex type (_Complex) to the struct module","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nThe struct module has support for ``float`` and ``double`` types, so at least there should be also ``float _Complex`` and ``double _Complex``. I'll work on a patch.\n\n\u003cdetails\u003e\n\n\u003csummary\u003eInitial version\u003c/summary\u003e\n\n```diff\ndiff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py\nindex d2e6a8bfc8..d941036719 100644\n--- a/Lib/ctypes/__init__.py\n+++ b/Lib/ctypes/__init__.py\n@@ -208,6 +208,7 @@ class c_longdouble(_SimpleCData):\n try:\n class c_double_complex(_SimpleCData):\n _type_ = \"C\"\n+ _check_size(c_double_complex)\n except AttributeError:\n pass\n \ndiff --git a/Modules/_struct.c b/Modules/_struct.c\nindex 6a68478dd4..caf4975413 100644\n--- a/Modules/_struct.c\n+++ b/Modules/_struct.c\n@@ -12,6 +12,9 @@\n #include \"pycore_long.h\" // _PyLong_AsByteArray()\n #include \"pycore_moduleobject.h\" // _PyModule_GetState()\n \n+#ifdef Py_HAVE_C_COMPLEX\n+# include \"_complex.h\" // complex\n+#endif\n #include \u003cstddef.h\u003e // offsetof()\n \n /*[clinic input]\n@@ -80,6 +83,9 @@ typedef struct { char c; int x; } st_int;\n typedef struct { char c; long x; } st_long;\n typedef struct { char c; float x; } st_float;\n typedef struct { char c; double x; } st_double;\n+#ifdef Py_HAVE_C_COMPLEX\n+typedef struct { char c; double complex x; } st_double_complex;\n+#endif\n typedef struct { char c; void *x; } st_void_p;\n typedef struct { char c; size_t x; } st_size_t;\n typedef struct { char c; _Bool x; } st_bool;\n@@ -89,6 +95,9 @@ typedef struct { char c; _Bool x; } st_bool;\n #define LONG_ALIGN (sizeof(st_long) - sizeof(long))\n #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))\n #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))\n+#ifdef Py_HAVE_C_COMPLEX\n+#define DOUBLE_COMPLEX_ALIGN (sizeof(st_double_complex) - sizeof(double complex))\n+#endif\n #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))\n #define SIZE_T_ALIGN (sizeof(st_size_t) - sizeof(size_t))\n #define BOOL_ALIGN (sizeof(st_bool) - sizeof(_Bool))\n@@ -518,6 +527,17 @@ nu_double(_structmodulestate *state, const char *p, const formatdef *f)\n return PyFloat_FromDouble(x);\n }\n \n+#ifdef Py_HAVE_C_COMPLEX\n+static PyObject *\n+nu_double_complex(_structmodulestate *state, const char *p, const formatdef *f)\n+{\n+ double complex x;\n+\n+ memcpy(\u0026x, p, sizeof(x));\n+ return PyComplex_FromDoubles(creal(x), cimag(x));\n+}\n+#endif\n+\n static PyObject *\n nu_void_p(_structmodulestate *state, const char *p, const formatdef *f)\n {\n@@ -791,6 +811,24 @@ np_double(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)\n return 0;\n }\n \n+#ifdef Py_HAVE_C_COMPLEX\n+static int\n+np_double_complex(_structmodulestate *state, char *p, PyObject *v,\n+ const formatdef *f)\n+{\n+ Py_complex c = PyComplex_AsCComplex(v);\n+ double complex x = CMPLX(c.real, c.imag);\n+\n+ if (c.real == -1 \u0026\u0026 PyErr_Occurred()) {\n+ PyErr_SetString(state-\u003eStructError,\n+ \"required argument is not a complex\");\n+ return -1;\n+ }\n+ memcpy(p, (char *)\u0026x, sizeof(x));\n+ return 0;\n+}\n+#endif\n+\n static int\n np_void_p(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)\n {\n@@ -829,6 +867,9 @@ static const formatdef native_table[] = {\n {'e', sizeof(short), SHORT_ALIGN, nu_halffloat, np_halffloat},\n {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},\n {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},\n+#ifdef Py_HAVE_C_COMPLEX\n+ {'C', sizeof(double complex), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},\n+#endif\n {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},\n {0}\n };\n```\n\n\u003c/details\u003e\n\n### Has this already been discussed elsewhere?\n\nThis is a minor feature, which does not need previous discussion elsewhere\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-121613\n* gh-131867\n* gh-132827\n* gh-132863\n* gh-132864\n* gh-133249\n* gh-149346\n* gh-152274\n* gh-152309\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/skirpichev","@type":"Person","name":"skirpichev"},"datePublished":"2024-07-02T03:36:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":23},"url":"https://github.com/121249/cpython/issues/121249"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:62f9e3f0-1a4d-a913-6fd3-4660d419bab1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9D28:1F40D1:4AE670:679D0B:6A51213C |
| html-safe-nonce | 70778510df8463bad0eecf775a7cbea79486978fbf3d69fa71b3b7e89519d7ae |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDI4OjFGNDBEMTo0QUU2NzA6Njc5RDBCOjZBNTEyMTNDIiwidmlzaXRvcl9pZCI6IjM3NDExNDM1Mzk5ODEwMzM3ODgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 54a0e049f7e2af601b22675e17774b00009d1899731253396c1c5c1081fd5107 |
| hovercard-subject-tag | issue:2385121145 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/121249/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fe23f6e76c6e61ca5275588aa7422fe4aa173dbced85bce6373ac50e14b7a54d/python/cpython/issues/121249 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fe23f6e76c6e61ca5275588aa7422fe4aa173dbced85bce6373ac50e14b7a54d/python/cpython/issues/121249 |
| og:image:alt | Feature or enhancement Proposal: The struct module has support for float and double types, so at least there should be also float _Complex and double _Complex. I'll work on a patch. Initial version... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | skirpichev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 689b683944b0d7f015d0e776452c970832fa81b82dc15284469b556ecced715b |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 942b51ed3bd5feee3d5e2719f890a5fe286edbbb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width