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

Appearance settings

BUG: .from_dlpack never sets writeable flag #28599

Copy link
Copy link
Closed
@karl3wm

Description

@karl3wm
Issue body actions

Describe the issue:

All arrays imported from dlpacks come out set readonly.

It looks like initially setting the writeable flag was overlooked when flags are passed as 0 at

PyObject *ret = PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape,
dl_tensor.strides != NULL ? strides : NULL, data, 0, NULL);
and then cleared when already 0 at
if (readonly) {
PyArray_CLEARFLAGS((PyArrayObject *)ret, NPY_ARRAY_WRITEABLE);
}
. The flag is never set.

Reproduce the code example:

import numpy as np
original_array = np.array([0])
imported_array = np.from_dlpack(original_array)
imported_array[0] = 1
imported_array.flags.writeable = True

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: assignment destination is read-only
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot set WRITEABLE flag to True of this array

Python and NumPy Versions:

2.2.4
3.12.3 (main, Feb 4 2025, 14:48:35) [GCC 13.3.0]]

Runtime Environment:

[{'numpy_version': '2.2.4',
'python': '3.12.3 (main, Feb 4 2025, 14:48:35) [GCC 13.3.0]',
'uname': uname_result(system='Linux', node='LAPTOP-JDTFCTMR', release='5.15.167.4-microsoft-standard-WSL2', version='#1 SMP Tue Nov 5 00:21:55 UTC 2024', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3', 'SSE41', 'POPCNT', 'SSE42'],
'not_found': ['AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Katmai',
'filepath': '/home/karl3/.local/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-6bb31eeb.so',
'internal_api': 'openblas',
'num_threads': 3,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.28'}]

Context for the issue:

This issue prevents the use of numpy for writing into aliased data from other tensor libraries or in general contexts using the array api. I was implementing a lightweight n-dimensional large integer class, using views to change between unsigned and signed representations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.