Skip to content

Commit d5f8ace

Browse files
authored
reparameterise PyArray (#674)
* reparameterise PyArray * undocumented unused flags * update release notes * fix elsize bug * update tests for new PyArray parameterisation --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent 4b812e3 commit d5f8ace

File tree

4 files changed

+141
-144
lines changed

4 files changed

+141
-144
lines changed

docs/src/releasenotes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* `enable(true)` replaces `enable()`.
66
* `enable(false)` replaces `disable()`.
77
* `gc()` added.
8+
* Breaking changes to Python wrapper types:
9+
* `PyArray` has been reparametrised from `PyArray{T,N,M,L,R}` to `PyArray{T,N,F}`:
10+
* `F` is a tuple of symbols representing flags, with `:linear` replacing `L` and `:mutable` replacing `M`.
11+
* `R` is removed and is now implied by `T`, which currently must be either a bits type (equal to `R`) or `Py`, or a tuple of these.
812
* Breaking changes to Julia wrapper types:
913
* Classes renamed: `ValueBase` to `JlBase`, `AnyValue` to `Jl`, `ArrayValue` to `JlArray`, etc.
1014
* Classes removed: `RawValue`, `ModuleValue`, `TypeValue`, `NumberValue`, `ComplexValue`, `RealValue`, `RationalValue`, `IntegerValue`.

src/API/types.jl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ baremodule pybuiltins end
5151
# Wrap
5252

5353
"""
54-
PyArray{T,N,M,L,R}(x; copy=true, array=true, buffer=true)
54+
PyArray{T,N,F}(x; copy=true, array=true, buffer=true)
5555
5656
Wrap the Python array `x` as a Julia `AbstractArray{T,N}`.
5757
@@ -62,31 +62,34 @@ If `copy=false` then the resulting array is guaranteed to directly wrap the data
6262
The type parameters are all optional, and are:
6363
- `T`: The element type.
6464
- `N`: The number of dimensions.
65-
- `M`: True if the array is mutable.
66-
- `L`: True if the array supports fast linear indexing.
67-
- `R`: The element type of the underlying buffer. Often equal to `T`.
65+
- `F`: Tuple of symbols, including:
66+
- `:mutable`: The array is mutable.
67+
- `:linear`: Supports fast linear indexing.
68+
- `:contiguous`: Data is F-contiguous. Implies `:linear`.
6869
"""
69-
struct PyArray{T,N,M,L,R} <: AbstractArray{T,N}
70-
ptr::Ptr{R} # pointer to the data
70+
struct PyArray{T,N,F} <: AbstractArray{T,N}
71+
ptr::Ptr{Cvoid} # pointer to the data
7172
length::Int # length of the array
7273
size::NTuple{N,Int} # size of the array
7374
strides::NTuple{N,Int} # strides (in bytes) between elements
7475
py::Py # underlying python object
7576
handle::Py # the data in this array is valid as long as this handle is alive
76-
function PyArray{T,N,M,L,R}(
77+
function PyArray{T,N,F}(
7778
::Val{:new},
78-
ptr::Ptr{R},
79+
ptr::Ptr,
7980
size::NTuple{N,Int},
8081
strides::NTuple{N,Int},
8182
py::Py,
8283
handle::Py,
83-
) where {T,N,M,L,R}
84+
) where {T,N,F}
8485
T isa Type || error("T must be a Type")
8586
N isa Int || error("N must be an Int")
86-
M isa Bool || error("M must be a Bool")
87-
L isa Bool || error("L must be a Bool")
88-
R isa DataType || error("R must be a DataType")
89-
new{T,N,M,L,R}(ptr, prod(size), size, strides, py, handle)
87+
F isa Tuple{Vararg{Symbol}} || error("F must be a tuple of Symbols")
88+
for flag in F
89+
flag in (:mutable, :linear, :contiguous, :copy, :nocopy) ||
90+
error("invalid entry of F: $(repr(flag))")
91+
end
92+
new{T,N,F}(ptr, prod(size), size, strides, py, handle)
9093
end
9194
end
9295

0 commit comments

Comments
 (0)