Skip to content

Commit 4d5dbb0

Browse files
authored
rename pydel! to unsafe_pydel and export (#788)
Co-authored-by: Christopher Rowley <github.com/cjdoris>
1 parent deb7570 commit 4d5dbb0

25 files changed

Lines changed: 153 additions & 147 deletions

BENCHMARKS.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
| ------- | ------ | ------------------- | ----------- |
77
| Python | 1.0x | 280 | ? |
88
| PythonCall | 2.4x | 680 | 5008 |
9-
| PythonCall + `pydel!` | 1.1x | 300 | 1008 |
9+
| PythonCall + `unsafe_pydel` | 1.1x | 300 | 1008 |
1010
| PythonCall `@py` | 1.4x | 420 | 1002 |
11-
| PythonCall `@py` + `@pydel!` | 1.1x | 300 | 2 |
11+
| PythonCall `@py` + `unsafe_pydel` | 1.1x | 300 | 2 |
1212
| PyCall | 5.4x | 1620 | 10987 |
1313
| PyCall (readable but wrong) | 5.9x | 1784 | 11456 |
1414

@@ -42,7 +42,7 @@ test (generic function with 1 method)
4242
julia> @benchmark test()
4343
```
4444

45-
PythonCall + `pydel!` code:
45+
PythonCall + `unsafe_pydel` code:
4646
```julia-repl
4747
julia> using PythonCall, BenchmarkTools
4848
@@ -54,10 +54,10 @@ julia> function test()
5454
r = random()
5555
v = i + r
5656
x[k] = v
57-
pydel!(k)
58-
pydel!(r)
59-
pydel!(v)
60-
pydel!(i)
57+
unsafe_pydel(k)
58+
unsafe_pydel(r)
59+
unsafe_pydel(v)
60+
unsafe_pydel(i)
6161
end
6262
return x
6363
end
@@ -75,8 +75,8 @@ julia> test() = @py begin
7575
x = {}
7676
for i in range(1000)
7777
x[str(i)] = i + random()
78-
# Uncomment for pydel! version:
79-
# @jl PythonCall.pydel!(i)
78+
# Uncomment for unsafe_pydel version:
79+
# @jl PythonCall.unsafe_pydel(i)
8080
end
8181
x
8282
end

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Removed comparisons between `Py` and `Number` (like `Py(3) < 5`).
88
* Removed arithmetic between `Py` and `Number` (like `Py(2) * 10`).
99
* Removed unsafe API: `getptr`, `pycopy!`, `pyisnull`, `pynew`, `PyNULL`, `unsafe_pynext`.
10+
* Renamed `pydel!` to `unsafe_pydel` and exported.
1011
A `Py` is now always a valid, fixed, non-NULL Python object in the documented API.
1112
* Python errors no longer automatically set `sys.last_traceback` etc. when displayed from Julia.
1213
* Removed `CONFIG.auto_sys_last_traceback`.

benchmark/benchmarks.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using BenchmarkTools
22
using PythonCall
3-
using PythonCall: pydel!, pyimport, pydict, pystr, pyrange
3+
using PythonCall: unsafe_pydel, pyimport, pydict, pystr, pyrange
44

55
const SUITE = BenchmarkGroup()
66

@@ -23,10 +23,10 @@ function test_pydict_pydel()
2323
r = random()
2424
v = i + r
2525
x[k] = v
26-
pydel!(k)
27-
pydel!(r)
28-
pydel!(v)
29-
pydel!(i)
26+
unsafe_pydel(k)
27+
unsafe_pydel(r)
28+
unsafe_pydel(v)
29+
unsafe_pydel(i)
3030
end
3131
return x
3232
end
@@ -40,7 +40,7 @@ SUITE["basic"]["julia"]["pydict"]["pydel"] = @benchmarkable test_pydict_pydel()
4040
x = {}
4141
for i in range(1000)
4242
x[str(i)] = i + random()
43-
$(use_pydel ? :(@jl PythonCall.pydel!(i)) : :(nothing))
43+
$(use_pydel ? :(@jl PythonCall.unsafe_pydel(i)) : :(nothing))
4444
end
4545
x
4646
end

docs/src/pythoncall-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ The functions here are not exported. They are mostly unsafe in the sense that yo
250250
crash Julia by using them incorrectly.
251251

252252
```@docs
253-
PythonCall.pydel!
253+
PythonCall.unsafe_pydel
254254
```
255255

256256
## NumpyDates

docs/src/v1-migration-guide.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ between Python and Julia is explicit.
2424
* Instead of `Py(5) * 6` use `Py(5) * Py(6)` or `pymul(Py(5), 6)`.
2525
* Instead of `np.array([1,2,3]) < 3` use `pylt(np.array([1,2,3]), 3)`.
2626

27-
The unsafe API has been removed: `getptr`, `pycopy!`, `pyisnull`, `pynew`, `PyNULL`,
27+
Most of the unsafe API has been removed: `getptr`, `pycopy!`, `pyisnull`, `pynew`, `PyNULL`,
2828
`unsafe_pynext`. This means that a `Py` is immutable and non-NULL in the documented API
2929
for PythonCall (mutability and NULLs are internal implementation details). These functions
3030
were primarily used in packages to have global `Py` objects with this pattern:
@@ -59,6 +59,11 @@ end
5959
* Instead of `PythonCall.pycopy!(x, y)` use `x[] = y`.
6060
* Instead of `PythonCall.unsafe_pynext(x)` (and check for `pyisnull`) use `pynext(x, nothing)` (and check for `nothing`).
6161

62+
`pydel!` has been renamed to `unsafe_pydel` to highlight that it is an unsafe function.
63+
It is now exported.
64+
65+
* Instead of `PythonCall.pydel!(x)` use `unsafe_pydel(x)`.
66+
6267
When a Python error is displayed in Julia, PythonCall no longer sets `sys.last_traceback`
6368
and friends. This means that the Python post-mortem debugger `pdb.pm()` will no longer
6469
work.

src/API/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export pytuple
9898
export pytype
9999
export pywith
100100
export pyxor
101+
export unsafe_pydel
101102

102103
# Convert
103104
export @pyconvert

src/API/functions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function pycomplex end
2222
function pycontains end
2323
function pydate end
2424
function pydatetime end
25-
function pydel! end
2625
function pydelattr end
2726
function pydelitem end
2827
function pydict end
@@ -98,6 +97,7 @@ function pytuple end
9897
function pytype end
9998
function pywith end
10099
function pyxor end
100+
function unsafe_pydel end
101101

102102
# Convert
103103
function pyconvert end

src/API/publics.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ if Base.VERSION ≥ v"1.11"
1313
1414
# Core
1515
CONFIG,
16-
pydel!,
1716
1817
# Compat
1918
event_loop_off,

src/Convert/pyconvert.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ function pytryconvert(::Type{T}, x_) where {T}
347347
rules = get!(trules, tptr) do
348348
t = pynew(incref(tptr))
349349
ans = pyconvert_get_rules(T, t)::Vector{Function}
350-
pydel!(t)
350+
unsafe_pydel(t)
351351
ans
352352
end
353353

src/Convert/rules.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pyconvert_rule_int(::Type{T}, x::Py) where {T<:Number} = begin
9090
# try converting -> int -> str -> BigInt -> T
9191
x_int = pyint(x)
9292
x_str = pystr(String, x_int)
93-
pydel!(x_int)
93+
unsafe_pydel(x_int)
9494
v = parse(BigInt, x_str)
9595
return pyconvert_tryconvert(T, v)
9696
end
@@ -203,7 +203,7 @@ function _pyconvert_rule_iterable(ans::Vector{T0}, it::Py, ::Type{T1}) where {T0
203203
@label again
204204
x_ = unsafe_pynext(it)
205205
if pyisnull(x_)
206-
pydel!(it)
206+
unsafe_pydel(it)
207207
return pyconvert_return(ans)
208208
end
209209
x = @pyconvert(T1, x_)
@@ -234,7 +234,7 @@ function _pyconvert_rule_iterable(ans::Set{T0}, it::Py, ::Type{T1}) where {T0,T1
234234
@label again
235235
x_ = unsafe_pynext(it)
236236
if pyisnull(x_)
237-
pydel!(it)
237+
unsafe_pydel(it)
238238
return pyconvert_return(ans)
239239
end
240240
x = @pyconvert(T1, x_)
@@ -271,7 +271,7 @@ function _pyconvert_rule_mapping(
271271
@label again
272272
k_ = unsafe_pynext(it)
273273
if pyisnull(k_)
274-
pydel!(it)
274+
unsafe_pydel(it)
275275
return pyconvert_return(ans)
276276
end
277277
v_ = pygetitem(x, k_)
@@ -378,24 +378,24 @@ function pyconvert_rule_iterable(
378378
it = pyiter(x)
379379
k_ = unsafe_pynext(it)
380380
if pyisnull(k_)
381-
pydel!(it)
382-
pydel!(k_)
381+
unsafe_pydel(it)
382+
unsafe_pydel(k_)
383383
return pyconvert_unconverted()
384384
end
385385
k = @pyconvert(K1, k_)
386386
v_ = unsafe_pynext(it)
387387
if pyisnull(v_)
388-
pydel!(it)
389-
pydel!(v_)
388+
unsafe_pydel(it)
389+
unsafe_pydel(v_)
390390
return pyconvert_unconverted()
391391
end
392392
v = @pyconvert(V1, v_)
393393
z_ = unsafe_pynext(it)
394-
pydel!(it)
394+
unsafe_pydel(it)
395395
if pyisnull(z_)
396-
pydel!(z_)
396+
unsafe_pydel(z_)
397397
else
398-
pydel!(z_)
398+
unsafe_pydel(z_)
399399
return pyconvert_unconverted()
400400
end
401401
K2 = Utils._promote_type_bounded(K0, typeof(k), K1)
@@ -421,7 +421,7 @@ function pyconvert_rule_iterable(::Type{R}, x::Py) where {R<:NamedTuple}
421421
pyistuple(x) || return pyconvert_unconverted()
422422
names2_ = pygetattr(x, "_fields", pybuiltins.None)
423423
names2 = @pyconvert(names === nothing ? Tuple{Vararg{Symbol}} : typeof(names), names2_)
424-
pydel!(names2_)
424+
unsafe_pydel(names2_)
425425
names === nothing || names === names2 || return pyconvert_unconverted()
426426
types2 = types === nothing ? NTuple{length(names2),Any} : types
427427
vals = @pyconvert(types2, x)
@@ -461,7 +461,7 @@ function pyconvert_rule_datetime(::Type{DateTime}, x::Py)
461461
days = pyconvert(Int, d.days)
462462
seconds = pyconvert(Int, d.seconds)
463463
microseconds = pyconvert(Int, d.microseconds)
464-
pydel!(d)
464+
unsafe_pydel(d)
465465
iszero(mod(microseconds, 1000)) || return pyconvert_unconverted()
466466
return pyconvert_return(
467467
_base_datetime +

0 commit comments

Comments
 (0)