Skip to content

Commit 05dd27d

Browse files
author
Christopher Doris
committed
clearing out src/old
1 parent ac1af40 commit 05dd27d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+838
-2308
lines changed

src/PyDict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Base.length(x::PyDict) = Int(pylen(x))
7272

7373
Base.empty!(x::PyDict) = (@py `$x.clear()`; x)
7474

75-
Base.copy(x::PyDict) = @pyv typeof(x) `$x.copy()`
75+
Base.copy(x::PyDict) = @pyv `$x.copy()`::typeof(x)
7676

7777
Base.haskey(x::PyDict{K}, _k) where {K} = begin
7878
k = tryconvertref(K, _k)

src/PyException.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function Base.showerror(io::IO, e::PyException)
115115

116116
# print the type name
117117
try
118-
tname = @pyv String `$(e.tref).__name__`
118+
tname = @pyv `$(e.tref).__name__`::String
119119
print(io, tname)
120120
catch
121121
print(io, "<error while printing type>")
@@ -125,7 +125,7 @@ function Base.showerror(io::IO, e::PyException)
125125
if !isnull(e.vref)
126126
print(io, ": ")
127127
try
128-
vstr = @pyv String `str($(e.vref))`
128+
vstr = @pyv `str($(e.vref))`::String
129129
print(io, vstr)
130130
catch
131131
print(io, "<error while printing value>")

src/old/PyIO.jl renamed to src/PyIO.jl

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ If `text=false` then `o` must be a binary stream and arbitrary binary I/O is pos
1010
For efficiency, reads and writes are buffered before being sent to `o`. The size of the buffer is `buflen`.
1111
"""
1212
mutable struct PyIO <: IO
13-
o :: PyObject
13+
ref :: PyRef
1414
# true to close the file automatically
1515
own :: Bool
1616
# true if `o` is text, false if binary
@@ -24,8 +24,8 @@ mutable struct PyIO <: IO
2424
obuflen :: Int
2525
obuf :: Vector{UInt8}
2626

27-
function PyIO(o::PyObject; own::Bool=false, text::Union{Missing,Bool}=missing, buflen::Integer=4096, ibuflen=buflen, obuflen=buflen)
28-
io = new(PyObject(o), own, text===missing ? pyisinstance(o, pyiomodule.TextIOBase) : text, false, ibuflen, UInt8[], obuflen, UInt8[])
27+
function PyIO(o; own::Bool=false, text::Union{Missing,Bool}=missing, buflen::Integer=4096, ibuflen=buflen, obuflen=buflen)
28+
io = new(PyRef(o), own, text===missing ? pyisinstance(o, pyiomodule().TextIOBase) : text, false, ibuflen, UInt8[], obuflen, UInt8[])
2929
finalizer(io) do io
3030
io.own ? close(io) : flush(io)
3131
end
@@ -34,6 +34,11 @@ mutable struct PyIO <: IO
3434
end
3535
export PyIO
3636

37+
ispyreftype(::Type{PyIO}) = true
38+
pyptr(io::PyIO) = pyptr(io.ref)
39+
Base.unsafe_convert(::Type{CPyPtr}, io::PyIO) = checknull(pyptr(io))
40+
C.PyObject_TryConvert__initial(o, ::Type{PyIO}) = C.putresult(PyIO, PyIO(pyborrowedref(o)))
41+
3742
"""
3843
PyIO(f, o; ...)
3944
@@ -50,39 +55,32 @@ function PyIO(f::Function, o; opts...)
5055
end
5156
end
5257

53-
pyobject(io::PyIO) = io.o
54-
5558
# If obuf is non-empty, write it to the underlying stream.
5659
function putobuf(io::PyIO)
5760
if !isempty(io.obuf)
58-
io.text ? io.o.write(String(io.obuf)) : io.o.write(io.obuf)
61+
@py `$io.write($(io.text ? pystr(io.obuf) : pybytes(io.obuf)))`
5962
empty!(io.obuf)
6063
end
6164
nothing
6265
end
6366

6467
# If ibuf is empty, read some more from the underlying stream.
6568
# After this call, if ibuf is empty then we are at EOF.
69+
# TODO: in binary mode, `io.readinto()` to avoid copying data
6670
function getibuf(io::PyIO)
6771
if isempty(io.ibuf)
68-
if io.text
69-
append!(io.ibuf, PyVector{UInt8,UInt8,false,true}(pystr_asutf8string(io.o.read(io.ibuflen))))
70-
else
71-
resize!(io.ibuf, io.ibuflen)
72-
n = io.o.readinto(io.ibuf).jl!i
73-
resize!(io.ibuf, n)
74-
end
72+
append!(io.ibuf, @pyv `$io.read($(io.ibuflen))`::Vector{UInt8})
7573
end
7674
nothing
7775
end
7876

7977
function Base.flush(io::PyIO)
8078
putobuf(io)
81-
io.o.flush()
79+
@py `$io.flush()`
8280
nothing
8381
end
8482

85-
Base.close(io::PyIO) = (flush(io); io.o.close(); nothing)
83+
Base.close(io::PyIO) = (flush(io); @py `$io.close()`; nothing)
8684

8785
function Base.eof(io::PyIO)
8886
if io.eof
@@ -95,13 +93,13 @@ function Base.eof(io::PyIO)
9593
end
9694
end
9795

98-
Base.fd(io::PyIO) = io.o.fileno().jl!i
96+
Base.fd(io::PyIO) = @pyv `$io.fileno()`::Int
9997

100-
Base.isreadable(io::PyIO) = io.o.readable().jl!b
98+
Base.isreadable(io::PyIO) = @pyv `$io.readable()`::Bool
10199

102-
Base.iswritable(io::PyIO) = io.o.writable().jl!b
100+
Base.iswritable(io::PyIO) = @pyv `$io.writable()`::Bool
103101

104-
Base.isopen(io::PyIO) = !io.o.closed.jl!b
102+
Base.isopen(io::PyIO) = @pyv `not $io.closed`::Bool
105103

106104
function Base.unsafe_write(io::PyIO, ptr::Ptr{UInt8}, n::UInt)
107105
while true
@@ -150,7 +148,7 @@ function Base.seek(io::PyIO, pos::Integer)
150148
putobuf(io)
151149
empty!(io.ibuf)
152150
io.eof = false
153-
io.o.seek(pos, 0)
151+
@py `$io.seek($pos, 0)`
154152
io
155153
end
156154

@@ -164,15 +162,15 @@ function Base.seekstart(io::PyIO)
164162
putobuf(io)
165163
empty!(io.ibuf)
166164
io.eof = false
167-
io.o.seek(0, 0)
165+
@py `$io.seek(0, 0)`
168166
io
169167
end
170168

171169
function Base.seekend(io::PyIO)
172170
putobuf(io)
173171
empty!(io.ibuf)
174172
io.eof = false
175-
io.o.seek(0, 2)
173+
@py `$io.seek(0, 2)`
176174
io
177175
end
178176

@@ -188,7 +186,7 @@ function Base.skip(io::PyIO, n::Integer)
188186
if 0 n io.ibuflen
189187
read(io, n)
190188
else
191-
io.o.seek(n - length(io.ibuf), 1)
189+
@py `$io.seek($(n - length(io.ibuf)), 1)`
192190
empty!(io.ibuf)
193191
io.eof = false
194192
end
@@ -199,11 +197,11 @@ function Base.position(io::PyIO)
199197
putobuf(io)
200198
if io.text
201199
if isempty(io.ibuf)
202-
io.o.position().jl!i
200+
@pyv `$io.position()`::Int
203201
else
204202
error("`position(io)` text PyIO streams only implemented for empty input buffer (e.g. do `read(io, length(io.ibuf))` first)")
205203
end
206204
else
207-
io.o.position().jl!i - length(io.ibuf)
205+
(@py `$io.position()`::Int) - length(io.ibuf)
208206
end
209207
end

src/PyIterable.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
PyIterable{T=PyObject}(o)
3+
4+
Wrap the Python object `o` into a Julia object which iterates values of type `T`.
5+
"""
6+
struct PyIterable{T}
7+
ref :: PyRef
8+
PyIterable{T}(o) where {T} = new{T}(PyRef(o))
9+
end
10+
PyIterable(o) = PyIterable{PyObject}(o)
11+
export PyIterable
12+
13+
ispyreftype(::Type{<:PyIterable}) = true
14+
pyptr(x::PyIterable) = pyptr(x.ref)
15+
Base.unsafe_convert(::Type{CPyPtr}, x::PyIterable) = checknull(pyptr(x))
16+
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PyIterable} = C.putresult(T, T(pyborrowedref(o)))
17+
18+
Base.length(x::PyIterable) = Int(pylen(x))
19+
20+
Base.IteratorSize(::Type{<:PyIterable}) = Base.SizeUnknown()
21+
22+
Base.IteratorEltype(::Type{<:PyIterable}) = Base.HasEltype()
23+
24+
Base.eltype(::Type{PyIterable{T}}) where {T} = T
25+
26+
function Base.iterate(x::PyIterable{T}, it=pyiter(PyRef, x)) where {T}
27+
vo = C.PyIter_Next(it)
28+
if !isnull(vo)
29+
r = C.PyObject_Convert(vo, T)
30+
C.Py_DecRef(vo)
31+
checkm1(r)
32+
(takeresult(T), it)
33+
elseif C.PyErr_IsSet()
34+
pythrow()
35+
else
36+
nothing
37+
end
38+
end

src/PyList.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ end
2525
Base.unsafe_convert(::Type{CPyPtr}, x::PyList) = checknull(pyptr(x))
2626
C.PyObject_TryConvert__initial(o, ::Type{T}) where {T<:PyList} = C.putresult(T, T(pyborrowedref(o)))
2727

28-
# Base.length(x::PyList) = @pyv Int `len($x)`
29-
Base.length(x::PyList) = Int(checkm1(C.PyObject_Length(x)))
28+
# Base.length(x::PyList) = @pyv `len($x)`::Int
29+
Base.length(x::PyList) = Int(pylen(x))
3030

3131
Base.size(x::PyList) = (length(x),)
3232

3333
Base.getindex(x::PyList{T}, i::Integer) where {T} = begin
3434
checkbounds(x, i)
3535
# The following line implements this function, but is typically 3x slower.
36-
# @pyv T `$x[$(i-1)]`
36+
# @pyv `$x[$(i-1)]`::T
3737
p = checknull(C.PySequence_GetItem(x, i-1))
3838
r = C.PyObject_Convert(p, T)
3939
C.Py_DecRef(p)
@@ -59,9 +59,9 @@ Base.push!(x::PyList{T}, v) where {T} = (@py `$x.append($(convertref(T, v)))`; x
5959

6060
Base.pushfirst!(x::PyList, v) = insert!(x, 1, v)
6161

62-
Base.pop!(x::PyList{T}) where {T} = @pyv T `$x.pop()`
62+
Base.pop!(x::PyList{T}) where {T} = @pyv `$x.pop()`::T
6363

64-
Base.popat!(x::PyList{T}, i::Integer) where {T} = (checkbounds(x, i); @pyv T `$x.pop($(i-1))`)
64+
Base.popat!(x::PyList{T}, i::Integer) where {T} = (checkbounds(x, i); @pyv `$x.pop($(i-1))`::T)
6565

6666
Base.popfirst!(x::PyList) = pop!(x, 1)
6767

@@ -72,4 +72,4 @@ Base.sort!(x::PyList; rev::Bool=false) = (@py `$x.sort(reverse=$rev)`; x)
7272

7373
Base.empty!(x::PyList) = (@py `$x.clear()`; x)
7474

75-
Base.copy(x::PyList) = @pyv typeof(x) `$x.copy()`
75+
Base.copy(x::PyList) = @pyv `$x.copy()`::typeof(x)

0 commit comments

Comments
 (0)