Title: ctypes array cannot be returned by value on ARM64 · Issue #110190 · python/cpython · GitHub
Open Graph Title: ctypes array cannot be returned by value on ARM64 · Issue #110190 · python/cpython
X Title: ctypes array cannot be returned by value on ARM64 · Issue #110190 · python/cpython
Description: Bug report Bug description: Dear, I'm wrapping a C-library with the ctypes module on Darwin (ARM64). I have a function that simply initializes a C-struct and returns the struct by value. The C-struct contains one simple C-array type. Whe...
Open Graph Description: Bug report Bug description: Dear, I'm wrapping a C-library with the ctypes module on Darwin (ARM64). I have a function that simply initializes a C-struct and returns the struct by value. The C-stru...
X Description: Bug report Bug description: Dear, I'm wrapping a C-library with the ctypes module on Darwin (ARM64). I have a function that simply initializes a C-struct and returns the struct by value. The C-...
Opengraph URL: https://github.com/python/cpython/issues/110190
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ctypes array cannot be returned by value on ARM64","articleBody":"# Bug report\n\n### Bug description:\n\nDear,\n\nI'm wrapping a C-library with the ctypes module on Darwin (ARM64). I have a function that simply initializes a C-struct and returns the struct by value. The C-struct contains one simple C-array type. When using ArrayTypes in Python, it seems the struct is not returned properly.\n\nOn Windows and Linux platforms the same code does work as expected.\n\nThe strange thing is that if I lay out the struct explicitly with singular types in Python (as I expect the layout of the struct is the same), it does work as expected on all tried platforms.\n\nTo reproduce, I first create a simple .c file with the struct and the initializing function. In Python, I give code that I use to compile (with gcc), and then three different ways to wrap the struct and function in Python using ctypes. I believe the three ways should be equivalent (and they are on Windows and Linux), but not on Darwin (ARM64).\n\nI saved this file as ``ctypesexample.c``\n```c\n\nstruct example{double coords[3];};\nstruct example set_defaults(){\n struct example ex;\n ex.coords[0] = 1.0;\n ex.coords[1] = 2.0;\n ex.coords[2] = 3.0;\n return ex;}\n```\n\nI saved this file as ``reproduce.py``\n```python\n\nimport os\nimport subprocess\nimport ctypes\n\ndef compile():\n \"\"\"Compile the c file using gcc.\"\"\"\n subprocess.call([\"gcc\",\"-c\",\"-Wall\",\"-Werror\",\"-fpic\",\"ctypesexample.c\"])\n subprocess.call([\"gcc\",\"-shared\",\"-o\",\"libctypesexample\",\"ctypesexample.o\"])\n assert os.path.isfile('libctypesexample')\n\n# Three ways to wrap the struct:\nclass Example1(ctypes.Structure):\n _fields_ = [\n ('coords', ctypes.c_double * 3)\n ]\n\nclass CoordType(ctypes.Array):\n _type_ = ctypes.c_double\n _length_ = 3\n\nclass Example2(ctypes.Structure):\n _fields_ = [('coords', CoordType)]\n \nclass Example3(ctypes.Structure):\n _fields_ = [\n ('x', ctypes.c_double),\n ('y', ctypes.c_double),\n ('z', ctypes.c_double)\n ]\n\ndef run():\n # Load the shared library\n so_location = os.path.abspath(\"libctypesexample\")\n _lib = ctypes.cdll.LoadLibrary(so_location)\n _lib.set_defaults.restype = Example1\n o = _lib.set_defaults()\n print(f\"Example1: ({o.coords[0]}, {o.coords[1]}, {o.coords[2]})\")\n \n _lib.set_defaults.restype = Example2\n o = _lib.set_defaults()\n print(f\"Example2: ({o.coords[0]}, {o.coords[1]}, {o.coords[2]})\")\n\n _lib.set_defaults.restype = Example3\n o = _lib.set_defaults()\n print(f\"Example3: ({o.x}, {o.y}, {o.z})\")\n\nif __name__ == \"__main__\":\n if not os.path.isfile('libctypesexample'):\n compile()\n run()\n```\n\nI tried this code on Linux (debian) -- both in Docker and WSL. And this works perfectly. Equivalent code in Windows is also working fine. In the past, I have run similar code as above on many different versions of Python (3.5-3.11) on both platforms for years (and still) without problems. For example on WSL with the following platform info\n\n`uname_result(system='Linux', release='5.15.90.1-microsoft-standard-WSL2', version='#1 SMP Fri Jan 27 02:56:13 UTC 2023', machine='x86_64')`\n\nand Python:\n\nPython 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux\n\nI get this correct output:\n\n```\nExample1: (1.0, 2.0, 3.0)\nExample2: (1.0, 2.0, 3.0)\nExample3: (1.0, 2.0, 3.0)\n```\n\nWhile on **Darwin (ARM)** with the following platform info:\n\n`system='Darwin', release='22.1.0', version='Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103', machine='arm64')`\n\nand Python\n\nPython 3.11.4 (main, Jun 12 2023, 11:39:45) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin\n\nI get the following output:\n\n```\nExample1: (3.025693809e-314, 3.0256938013e-314, 2.1481083817e-314)\nExample2: (3.025693809e-314, 3.0256938013e-314, 2.1481083817e-314)\nExample3: (1.0, 2.0, 3.0)\n```\n\nIn summary, the struct doesn't seem to be properly returned as value using array types, though with a literal listing of the types, it seems there is no problem. So there is a workaround, but as we're using ctypes for automatic wrapping through reflection and the code works properly on Windows and Linux, it would be quite some work to implement the workaround.\n\nI hope my bug report finds you without any errors, and many thanks for giving the opportunity to report.\n\nBest regards\n\n\n### CPython versions tested on:\n\n3.10, 3.11\n\n### Operating systems tested on:\n\nLinux, macOS, Windows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-112604\n* gh-112766\n* gh-112767\n* gh-112818\n* gh-112829\n* gh-112830\n* gh-112959\n* gh-113167\n* gh-113170\n* gh-114753\n* gh-114774\n* gh-114775\n* gh-118233\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/pdegroote","@type":"Person","name":"pdegroote"},"datePublished":"2023-10-01T19:29:34Z","url":"https://github.com/python/cpython/issues/110190"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:d8ac33b5-b746-8395-144a-9959743cdf0e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD02:1F2F:197B6AC:21D8B4D:6B020276 |
| html-safe-nonce | 7a351ced79764f4d8194060f150db38764c1fe795e9a12f41b9b0bccee26edb0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDAyOjFGMkY6MTk3QjZBQzoyMUQ4QjREOjZCMDIwMjc2IiwidmlzaXRvcl9pZCI6IjEzMDU5OTQ3MDc3NjcyNjM4NjIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | dc8fac0ccfe6c46bf0deb6c32896ac42c1e5ab164880f53b956c0fb9be8c306a |
| hovercard-subject-tag | issue:1920931168 |
| 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/110190/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b98175a7faeafe720f941da3e6d0ee83bcf2df1e51377b374e6f694e99b061fb/python/cpython/issues/110190 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b98175a7faeafe720f941da3e6d0ee83bcf2df1e51377b374e6f694e99b061fb/python/cpython/issues/110190 |
| og:image:alt | Bug report Bug description: Dear, I'm wrapping a C-library with the ctypes module on Darwin (ARM64). I have a function that simply initializes a C-struct and returns the struct by value. The C-stru... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pdegroote |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width