Skip to content

Numpy does not recognize ctypes arrays with c_wchar field #10100

@macat

Description

@macat

The casting of a ctypes Structure object array to numpy array usually results with a numpy structured array. But if the Structure contains a c_wchar array (string) field it becomes an object. I could not find the reason why this is happening.

Example:

import numpy as np
import ctypes

class A(ctypes.Structure):
    _fields_ = [('s', ctypes.c_wchar * 5)]

np.array((A*2)())
# array([<__main__.A object at 0x7fa99678f9d8>,
#       <__main__.A object at 0x7fa98804f1e0>], dtype=object)

While:

class B(ctypes.Structure):
    _fields_ = [('s', ctypes.c_char * 5)]

np.array((B*2)())
# array([([b'', b'', b'', b'', b''],), ([b'', b'', b'', b'', b''],)],
#      dtype=[('s', 'S1', (5,))])

Environment:
Python 3.6, numpy 1.13.1, Ubuntu 16.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions