Skip to content

Commit

Permalink
conversion of numpy.bool_ to Bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Doris committed May 22, 2024
1 parent aca2701 commit 4d7b73c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/src/conversion-to-julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ From Python, the arguments to a Julia function will be converted according to th
| `ctypes.c_voidp` | `Ptr{Cvoid}`, `Ptr` |
| `ctypes.c_char_p` | `Cstring`, `Ptr{Cchar}`, `Ptr` |
| `ctypes.c_wchar_p` | `Cwstring`, `Ptr{Cwchar}`, `Ptr` |
| `numpy.intXX`/`numpy.uintXX`/`numpy.floatXX` | `Integer`, `Rational`, `Real`, `Number` |
| `numpy.bool_`/`numpy.intXX`/`numpy.uintXX`/`numpy.floatXX` | `Bool`, `Integer`, `Rational`, `Real`, `Number` |
| Objects satisfying the buffer or array interface | `Array`, `AbstractArray` |
| **Low priority (fallback to `Py`).** | |
| Anything | `Py` |
Expand Down
1 change: 1 addition & 0 deletions docs/src/releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
* `Serialization.serialize` can use `dill` instead of `pickle` by setting the env var `JULIA_PYTHONCALL_PICKLE=dill`.
* Added conversion from `numpy.bool_` to `Bool` and other number types.

## 0.9.20 (2024-05-01)
* The IPython extension is now automatically loaded upon import if IPython is detected.
Expand Down
12 changes: 7 additions & 5 deletions src/Convert/numpy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function (::pyconvert_rule_numpysimplevalue{R,SAFE})(::Type{T}, x::Py) where {R,
end

const NUMPY_SIMPLE_TYPES = [
("bool_", Bool),
("int8", Int8),
("int16", Int16),
("int32", Int32),
Expand All @@ -27,17 +28,18 @@ const NUMPY_SIMPLE_TYPES = [
]

function init_numpy()
for (t,T) in NUMPY_SIMPLE_TYPES
isint = occursin("int", t)
isuint = occursin("uint", t)
for (t, T) in NUMPY_SIMPLE_TYPES
isbool = occursin("bool", t)
isint = occursin("int", t) || isbool
isuint = occursin("uint", t) || isbool
isfloat = occursin("float", t)
iscomplex = occursin("complex", t)
isreal = isint || isfloat
isnumber = isreal || iscomplex

name = "numpy:$t"
rule = pyconvert_rule_numpysimplevalue{T, false}()
saferule = pyconvert_rule_numpysimplevalue{T, true}()
rule = pyconvert_rule_numpysimplevalue{T,false}()
saferule = pyconvert_rule_numpysimplevalue{T,true}()

pyconvert_add_rule(name, T, saferule, PYCONVERT_PRIORITY_ARRAY)
isuint && pyconvert_add_rule(name, UInt, sizeof(T) sizeof(UInt) ? saferule : rule)
Expand Down

0 comments on commit 4d7b73c

Please sign in to comment.