Skip to content

Commit

Permalink
Massive super-commit removing current_output_stream [closes #754]
Browse files Browse the repository at this point in the history
This is an aggregation of changes by Jeff and me over the past
weeks, removing all use of the implicit current output stream
mechanism from our code base, since it was deemed a misfeature,
and it's terribly, terribly slow to use. The new approach is to
just thread an IO object for output through all the basic calls
that print stuff. Slightly annoying, but not so bad.
  • Loading branch information
StefanKarpinski committed May 2, 2012
1 parent bf1cb37 commit 366560e
Show file tree
Hide file tree
Showing 46 changed files with 572 additions and 611 deletions.
2 changes: 1 addition & 1 deletion base/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ end

## outputing ASCII strings ##

print(s::ASCIIString) = print(s.data)
print(io::IO, s::ASCIIString) = print(io, s.data)
write(io, s::ASCIIString) = write(io, s.data)

## transcoding to ASCII ##
Expand Down
2 changes: 1 addition & 1 deletion base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type BackTrace <: Exception
trace::Array{Any,1}
end

show(bt::BackTrace) = show(bt.e)
show(io, bt::BackTrace) = show(io,bt.e)

method_missing(f, args...) = throw(MethodError(f, args))

Expand Down
4 changes: 2 additions & 2 deletions base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ sizeof(::Type{Char}) = 4

## printing & showing characters ##

print(c::Char) = (write(c); nothing)
show(c::Char) = (print('\''); print_escaped(string(c), "'"); print('\''))
print(io::IO, c::Char) = (write(io,c); nothing)
show(io, c::Char) = (print(io,'\''); print_escaped(io,string(c),"'"); print(io,'\''))

## libc character class testing functions ##

Expand Down
6 changes: 4 additions & 2 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ function repl_show(v::ANY)
if !(isa(v,Function) && isgeneric(v))
if isa(v,AbstractVector) && !isa(v,Ranges)
print(summary(v))
println(":")
print_matrix(reshape(v,(length(v),1)))
if !isempty(v)
println(":")
print_matrix(OUTPUT_STREAM, reshape(v,(length(v),1)))
end
else
show(v)
end
Expand Down
20 changes: 10 additions & 10 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ imag(x::Real) = zero(x)
isfinite(z::Complex) = isfinite(real(z)) && isfinite(imag(z))
reim(z) = (real(z), imag(z))

function _jl_show(z::Complex, compact::Bool)
function _jl_show(io, z::Complex, compact::Bool)
r, i = reim(z)
if isnan(r) || isfinite(i)
compact ? showcompact(r) : show(r)
compact ? showcompact(io,r) : show(io,r)
if signbit(i)==1 && !isnan(i)
i = -i
print(compact ? "-" : " - ")
print(io, compact ? "-" : " - ")
else
print(compact ? "+" : " + ")
print(io, compact ? "+" : " + ")
end
compact ? showcompact(i) : show(i)
compact ? (i) : show(io, i)
if !(isa(i,Integer) || isa(i,Rational) ||
isa(i,Float) && isfinite(i))
print("*")
print(io, "*")
end
print("im")
print(io, "im")
else
print("complex(",r,",",i,")")
print(io, "complex(",r,",",i,")")
end
end
show(z::Complex) = _jl_show(z, false)
showcompact(z::Complex) = _jl_show(z, true)
show(io, z::Complex) = _jl_show(z, false)
showcompact(io, z::Complex) = _jl_show(z, true)

## packed complex float types ##

Expand Down
104 changes: 49 additions & 55 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
## file formats ##

function _jl_dlm_readrow(f, dlm, eol)
row = split(readuntil(f, eol), dlm, true)
function _jl_dlm_readrow(io, dlm, eol)
row = split(readuntil(io, eol), dlm, true)
row[end] = chomp(row[end])
row
end

# all strings
function _jl_dlmread(a, f, dlm, nr, nc, row, eol)
function _jl_dlmread(a, io, dlm, nr, nc, row, eol)
for i=1:nr
a[i,:] = row
if i < nr
row = _jl_dlm_readrow(f, dlm, eol)
row = _jl_dlm_readrow(io, dlm, eol)
end
end
a
end

# all numeric, with NaN for invalid data
function _jl_dlmread{T<:Number}(a::Array{T}, f, dlm, nr, nc, row, eol)
function _jl_dlmread{T<:Number}(a::Array{T}, io, dlm, nr, nc, row, eol)
tmp = Array(Float64,1)
for i=1:nr
for j=1:nc
Expand All @@ -29,15 +29,15 @@ function _jl_dlmread{T<:Number}(a::Array{T}, f, dlm, nr, nc, row, eol)
end
end
if i < nr
row = _jl_dlm_readrow(f, dlm, eol)
row = _jl_dlm_readrow(io, dlm, eol)
end
end
end

# float64 or string
_jl_dlmread(a::Array{Any}, f, dlm, nr, nc, row, eol) =
_jl_dlmread(a, f, dlm, nr, nc, row, eol, 1, 1)
function _jl_dlmread(a::Array{Any}, f, dlm, nr, nc, row, eol, i0, j0)
_jl_dlmread(a::Array{Any}, io, dlm, nr, nc, row, eol) =
_jl_dlmread(a, io, dlm, nr, nc, row, eol, 1, 1)
function _jl_dlmread(a::Array{Any}, io, dlm, nr, nc, row, eol, i0, j0)
tmp = Array(Float64,1)
j = j0
for i=i0:nr
Expand All @@ -52,59 +52,59 @@ function _jl_dlmread(a::Array{Any}, f, dlm, nr, nc, row, eol, i0, j0)
end
j = 1
if i < nr
row = _jl_dlm_readrow(f, dlm, eol)
row = _jl_dlm_readrow(io, dlm, eol)
end
end
a
end

# float64 or cell depending on data
function _jl_dlmread_auto(a, f, dlm, nr, nc, row, eol)
function _jl_dlmread_auto(a, io, dlm, nr, nc, row, eol)
tmp = Array(Float64, 1)
for i=1:nr
for j=1:nc
el = row[j]
if !float64_isvalid(el, tmp)
a = convert(Array{Any,2}, a)
_jl_dlmread(a, f, dlm, nr, nc, row, eol, i, j)
_jl_dlmread(a, io, dlm, nr, nc, row, eol, i, j)
return a
else
a[i,j] = tmp[1]
end
end
if i < nr
row = _jl_dlm_readrow(f, dlm, eol)
row = _jl_dlm_readrow(io, dlm, eol)
end
end
a
end

countlines(f) = countlines(f, '\n')
function countlines(f::String, eol::Char)
fh = open(f)
countlines(io) = countlines(io, '\n')
function countlines(io::String, eol::Char)
fh = open(io)
n = countlines(fh, eol)
close(fh)
return n
end
function countlines(f::IOStream, eol::Char)
function countlines(io::IOStream, eol::Char)
if !iswascii(eol)
error("countlines: only ASCII line terminators supported")
end
a = Array(Uint8, 8192)
nl = 0
while !eof(f)
while !eof(io)
fill!(a, uint8(eol)+1) # fill with byte we're not looking for
try
read(f, a)
read(io, a)
end
for i=1:length(a)
if a[i] == eol
nl+=1
end
end
end
skip(f,-1)
if read(f,Uint8) != eol
skip(io,-1)
if read(io,Uint8) != eol
nl+=1
end
nl
Expand All @@ -115,10 +115,10 @@ function _jl_dlmread_setup(fname::String, dlm, eol)
error("dlmread: no separator characters specified")
end
nr = countlines(fname,eol)
f = open(fname)
row = _jl_dlm_readrow(f, dlm, eol)
io = open(fname)
row = _jl_dlm_readrow(io, dlm, eol)
nc = length(row)
return (f, nr, nc, row)
return (io, nr, nc, row)
end

const _jl_dlmread_default_delimiters = [' ', ',', ';', '\t', '\v']
Expand All @@ -128,59 +128,53 @@ dlmread(fname::String, T::Type) = dlmread(fname, _jl_dlmread_default_delimiters,
dlmread(fname::String, dlm, T::Type) = dlmread(fname, dlm, T, '\n')

function dlmread(fname::String, dlm, T::Type, eol::Char)
(f, nr, nc, row) = _jl_dlmread_setup(fname, dlm, eol)
(io, nr, nc, row) = _jl_dlmread_setup(fname, dlm, eol)
a = Array(T, nr, nc)
_jl_dlmread(a, f, dlm, nr, nc, row, eol)
close(f)
_jl_dlmread(a, io, dlm, nr, nc, row, eol)
close(io)
return a
end

dlmread(fname::String) = dlmread(fname, _jl_dlmread_default_delimiters, '\n')

dlmread(fname::String, dlm) = dlmread(fname, dlm, '\n')

function dlmread(fname::String, dlm, eol::Char)
(f, nr, nc, row) = _jl_dlmread_setup(fname, dlm, eol)
(io, nr, nc, row) = _jl_dlmread_setup(fname, dlm, eol)
a = Array(Float64, nr, nc)
a = _jl_dlmread_auto(a, f, dlm, nr, nc, row, eol)
close(f)
a = _jl_dlmread_auto(a, io, dlm, nr, nc, row, eol)
close(io)
return a
end

csvread(f) = dlmread(f, ',')
csvread(f, T::Type) = dlmread(f, ',', T)
csvread(io) = dlmread(io, ',')
csvread(io, T::Type) = dlmread(io, ',', T)

# todo: keyword argument for # of digits to print
function dlmwrite(f, a, dlm::Char)
function dlmwrite(io, a, dlm::Char)
nr, nc = size(a)
try
set_current_output_stream(f)
for i = 1:nr
for j = 1:nc
elt = a[i,j]
if isa(elt,Float)
print_shortest(elt)
else
print(elt)
end
if j < nc
write(f, dlm)
end
for i = 1:nr
for j = 1:nc
elt = a[i,j]
if isa(elt,Float)
print_shortest(io, elt)
else
print(io, elt)
end
if j < nc
write(io, dlm)
end
write(f, '\n')
end
catch e
throw(e)
write(io, '\n')
end
nothing
end

function dlmwrite(fname::String, a, dlm::Char)
f = open(fname, "w")
dlmwrite(f, a, dlm)
close(f)
io = open(fname, "w")
dlmwrite(io, a, dlm)
close(io)
nothing
end

dlmwrite(f, a) = dlmwrite(f, a, ',')
csvwrite(f, a) = dlmwrite(f, a, ',')
dlmwrite(io, a) = dlmwrite(io, a, ',')
csvwrite(io, a) = dlmwrite(io, a, ',')
4 changes: 2 additions & 2 deletions base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ function next(::EnvHash, i)
(m.captures, i+1)
end

function show(::EnvHash)
function show(io, ::EnvHash)
for (k,v) = ENV
println("$k=$v")
println(io, "$k=$v")
end
end

Expand Down
12 changes: 6 additions & 6 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ isequal(x::SymbolNode, y::SymbolNode) = is(x.name,y.name)
isequal(x::SymbolNode, y::Symbol) = is(x.name,y)
isequal(x::Symbol , y::SymbolNode) = is(x,y.name)

function show(tv::TypeVar)
function show(io, tv::TypeVar)
if !is(tv.lb, None)
show(tv.lb)
print("<:".data)
show(io, tv.lb)
print(io, "<:".data)
end
print(tv.name)
print(io, tv.name)
if !is(tv.ub, Any)
print("<:".data)
show(tv.ub)
print(io, "<:".data)
show(io, tv.ub)
end
end

Expand Down
17 changes: 8 additions & 9 deletions base/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ function grisu_sig(x::Real, n::Integer)
grisu(float64(x), GRISU_PRECISION, int32(n))
end

function _show(x::Float, mode::Int32, n::Int)
s = current_output_stream()
function _show(s, x::Float, mode::Int32, n::Int)
if isnan(x); return write(s, "NaN"); end
if isinf(x); return write(s, x < 0 ? "-Inf" : "Inf"); end
@grisu_ccall x mode n
Expand Down Expand Up @@ -95,9 +94,9 @@ function _show(x::Float, mode::Int32, n::Int)
nothing
end

show(x::Float64) = _show(x, GRISU_SHORTEST, 0)
show(x::Float32) = _show(x, GRISU_SHORTEST_SINGLE, 0)
showcompact(x::Float) = _show(x, GRISU_PRECISION, 6)
show(io, x::Float64) = _show(io, x, GRISU_SHORTEST, 0)
show(io, x::Float32) = _show(io, x, GRISU_SHORTEST_SINGLE, 0)
showcompact(io, x::Float) = _show(io, x, GRISU_PRECISION, 6)

# normal:
# 0 < pt < len ####.#### len+1
Expand All @@ -108,7 +107,7 @@ showcompact(x::Float) = _show(x, GRISU_PRECISION, 6)
# pt <= 0 ########e-### len+k+2
# 0 < pt ########e### len+k+1

function _jl_print_shortest(x::Float, dot::Bool, mode::Int32)
function f_jl_print_shortest(io, x::Float, dot::Bool, mode::Int32)
s = current_output_stream()
if isnan(x); return write(s, "NaN"); end
if isinf(x); return write(s, x < 0 ? "-Inf" : "Inf"); end
Expand Down Expand Up @@ -153,6 +152,6 @@ function _jl_print_shortest(x::Float, dot::Bool, mode::Int32)
nothing
end

print_shortest(x::Float64, dot::Bool) = _jl_print_shortest(x, dot, GRISU_SHORTEST)
print_shortest(x::Float32, dot::Bool) = _jl_print_shortest(x, dot, GRISU_SHORTEST_SINGLE)
print_shortest(x::Union(Float,Integer)) = print_shortest(float(x), false)
print_shortest(io, x::Float64, dot::Bool) = f_jl_print_shortest(io, x, dot, GRISU_SHORTEST)
print_shortest(io, x::Float32, dot::Bool) = f_jl_print_shortest(io, x, dot, GRISU_SHORTEST_SINGLE)
print_shortest(io, x::Union(Float,Integer)) = print_shortest(io, float(x), false)
Loading

0 comments on commit 366560e

Please sign in to comment.