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

ctypes array cannot be returned by value on ARM64 #110190

Closed
Labels
pendingThe issue will be closed if no feedback is providedtopic-ctypestype-bugAn unexpected behavior, bug, or error
@pdegroote

Description

@pdegroote

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. When using ArrayTypes in Python, it seems the struct is not returned properly.

On Windows and Linux platforms the same code does work as expected.

The 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.

To 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).

I saved this file asctypesexample.c

structexample{doublecoords[3];};structexampleset_defaults(){structexampleex;ex.coords[0]=1.0;ex.coords[1]=2.0;ex.coords[2]=3.0;returnex;}

I saved this file asreproduce.py

importosimportsubprocessimportctypesdefcompile():"""Compile the c file using gcc."""subprocess.call(["gcc","-c","-Wall","-Werror","-fpic","ctypesexample.c"])subprocess.call(["gcc","-shared","-o","libctypesexample","ctypesexample.o"])assertos.path.isfile('libctypesexample')# Three ways to wrap the struct:classExample1(ctypes.Structure):_fields_= [            ('coords',ctypes.c_double*3)               ]classCoordType(ctypes.Array):_type_=ctypes.c_double_length_=3classExample2(ctypes.Structure):_fields_= [('coords',CoordType)]classExample3(ctypes.Structure):_fields_= [            ('x',ctypes.c_double),            ('y',ctypes.c_double),            ('z',ctypes.c_double)               ]defrun():# Load the shared libraryso_location=os.path.abspath("libctypesexample")_lib=ctypes.cdll.LoadLibrary(so_location)_lib.set_defaults.restype=Example1o=_lib.set_defaults()print(f"Example1: ({o.coords[0]},{o.coords[1]},{o.coords[2]})")_lib.set_defaults.restype=Example2o=_lib.set_defaults()print(f"Example2: ({o.coords[0]},{o.coords[1]},{o.coords[2]})")_lib.set_defaults.restype=Example3o=_lib.set_defaults()print(f"Example3: ({o.x},{o.y},{o.z})")if__name__=="__main__":ifnotos.path.isfile('libctypesexample'):compile()run()

I 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

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')

and Python:

Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux

I get this correct output:

Example1: (1.0, 2.0, 3.0)Example2: (1.0, 2.0, 3.0)Example3: (1.0, 2.0, 3.0)

While onDarwin (ARM) with the following platform info:

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')

and Python

Python 3.11.4 (main, Jun 12 2023, 11:39:45) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin

I get the following output:

Example1: (3.025693809e-314, 3.0256938013e-314, 2.1481083817e-314)Example2: (3.025693809e-314, 3.0256938013e-314, 2.1481083817e-314)Example3: (1.0, 2.0, 3.0)

In 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.

I hope my bug report finds you without any errors, and many thanks for giving the opportunity to report.

Best regards

CPython versions tested on:

3.10, 3.11

Operating systems tested on:

Linux, macOS, Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp