Skip to content

BUG: ndarray.flat is incompatible with new-style dtypes #23486

@ngoldbaum

Description

@ngoldbaum

Describe the issue:

A segfault is generated from numpy internals when trying to do things with the flat iterator.

Reproduce the code example:

import numpy as np
from numpy.core._multiarray_umath import _get_sfloat_dtype

SF = _get_sfloat_dtype()

scaling = 0.5

x = (np.array([1., 2., 3.])/scaling).view(SF(scaling))

print(x.flat[:])

Error message:

$ gdb --args $(pyenv which python) setup-scaled-float.py 
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/nathan/.pyenv/versions/3.10.9-debug/bin/python...
(gdb) r
Starting program: /home/nathan/.pyenv/versions/3.10.9-debug/bin/python setup-scaled-float.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Detaching after vfork from child process 996063]
[Detaching after vfork from child process 996064]
[Detaching after vfork from child process 996072]
[Detaching after vfork from child process 996073]
[Detaching after vfork from child process 996074]

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) up
#1  0x00007ffff668e79d in iter_subscript (self=0x555555a51090, 
    ind=<slice at remote 0x7ffff5cac230>)
    at numpy/core/src/multiarray/iterators.c:634
634	            copyswap(dptr, self->dataptr, 0, ret);
(gdb) p copyswap
$1 = (PyArray_CopySwapFunc *) 0x0

Runtime information:

1.25.0.dev0+1036.g84571a1b2
3.10.9 (main, Mar  2 2023, 11:31:32) [GCC 11.3.0]
[{'numpy_version': '1.25.0.dev0+1036.g84571a1b2',
  'python': '3.10.9 (main, Mar  2 2023, 11:31:32) [GCC 11.3.0]',
  'uname': uname_result(system='Linux', node='rous', release='5.19.0-35-generic', version='#36~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 17 15:17:25 UTC 2', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}}]

Context for the issue:

copyswap isn't defined for any of the new-style dtypes. Sebastian tells me that the fix should be to replace the copyswap with a single-element strided/contiguous copy.

Note also that array_flat_set has a similar issue, so that should probably also be fixed when this is, but it's lower priority since that's a less-used code path.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions