From 8dd6d082fcb40610b22189342e24eff7ee7b1217 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Sun, 20 Jan 2013 02:02:22 -0500 Subject: [PATCH] make write() consistently return an Int byte count. fixes #1701 --- base/io.jl | 24 +++++++++++++++--------- base/stream.jl | 12 ++++++------ src/julia.h | 2 +- src/wrapper.c | 4 ++-- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/base/io.jl b/base/io.jl index 91c70644c6f2f..c197fdb067379 100644 --- a/base/io.jl +++ b/base/io.jl @@ -29,15 +29,19 @@ write(s::IO, x::Uint8) = error(typeof(s)," does not support byte I/O") if ENDIAN_BOM == 0x01020304 function write(s::IO, x::Integer) - for n = sizeof(x):-1:1 + sz = sizeof(x) + for n = sz:-1:1 write(s, uint8((x>>>((n-1)<<3)))) end + sz end else function write(s::IO, x::Integer) - for n = 1:sizeof(x) + sz = sizeof(x) + for n = 1:sz write(s, uint8((x>>>((n-1)<<3)))) end + sz end end @@ -46,9 +50,11 @@ write(s::IO, x::Float32) = write(s, box(Int32,unbox(Float32,x))) write(s::IO, x::Float64) = write(s, box(Int64,unbox(Float64,x))) function write(s::IO, a::AbstractArray) + nb = 0 for i = 1:length(a) - write(s, a[i]) + nb += write(s, a[i]) end + nb end function write(s::IO, c::Char) @@ -312,12 +318,11 @@ memio() = memio(0, true) ## low-level calls ## -write(s::IOStream, b::Uint8) = ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, s.ios) -write(s::IOStream, c::Char) = ccall(:jl_pututf8, Int32, (Ptr{Void}, Char), s.ios, c) +write(s::IOStream, b::Uint8) = int(ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, s.ios)) function write{T}(s::IOStream, a::Array{T}) if isa(T,BitsKind) - ccall(:jl_write, Uint, (Ptr{Void}, Ptr{Void}, Uint), + ccall(:ios_write, Int, (Ptr{Void}, Ptr{Void}, Uint), s.ios, a, length(a)*sizeof(T)) else invoke(write, (IO, Array), s, a) @@ -325,7 +330,7 @@ function write{T}(s::IOStream, a::Array{T}) end function write(s::IOStream, p::Ptr, nb::Integer) - ccall(:jl_write, Uint, (Ptr{Void}, Ptr{Void}, Uint), s.ios, p, nb) + ccall(:ios_write, Int, (Ptr{Void}, Ptr{Void}, Uint), s.ios, p, nb) end function write{T,N,A<:Array}(s::IOStream, a::SubArray{T,N,A}) @@ -334,10 +339,11 @@ function write{T,N,A<:Array}(s::IOStream, a::SubArray{T,N,A}) end colsz = size(a,1)*sizeof(T) if N<=1 - write(s, pointer(a, 1), colsz) + return write(s, pointer(a, 1), colsz) else cartesian_map((idxs...)->write(s, pointer(a, idxs), colsz), tuple(1, size(a)[2:]...)) + return colsz*trailingsize(a,2) end end @@ -367,7 +373,7 @@ end ## text I/O ## -write(s::IOStream, c::Char) = ccall(:ios_pututf8, Int32, (Ptr{Void}, Char), s.ios, c) +write(s::IOStream, c::Char) = int(ccall(:ios_pututf8, Int32, (Ptr{Void}, Char), s.ios, c)) read(s::IOStream, ::Type{Char}) = ccall(:jl_getutf8, Char, (Ptr{Void},), s.ios) takebuf_string(s::IOStream) = diff --git a/base/stream.jl b/base/stream.jl index e5d296163a412..4203a5a27153d 100644 --- a/base/stream.jl +++ b/base/stream.jl @@ -558,20 +558,20 @@ end ## low-level calls write(s::AsyncStream, b::ASCIIString) = - ccall(:jl_puts, Int32, (Ptr{Uint8},Ptr{Void}),b.data,handle(s)) + int(ccall(:jl_puts, Int32, (Ptr{Uint8},Ptr{Void}),b.data,handle(s))) write(s::AsyncStream, b::Uint8) = - ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, handle(s)) + int(ccall(:jl_putc, Int32, (Uint8, Ptr{Void}), b, handle(s))) write(s::AsyncStream, c::Char) = - ccall(:jl_pututf8, Int32, (Ptr{Void},Char), handle(s), c) + int(ccall(:jl_pututf8, Int32, (Ptr{Void},Char), handle(s), c)) function write{T}(s::AsyncStream, a::Array{T}) if(isa(T,BitsKind)) - ccall(:jl_write, Uint,(Ptr{Void}, Ptr{Void}, Uint32),handle(s), a, uint(length(a)*sizeof(T))) + ccall(:jl_write, Int, (Ptr{Void}, Ptr{Void}, Uint32), handle(s), a, uint(length(a)*sizeof(T))) else invoke(write,(IO,Array),s,a) end end -write(s::AsyncStream, p::Ptr, nb::Integer) = ccall(:jl_write, Uint,(Ptr{Void}, Ptr{Void}, Uint),handle(s), p, uint(nb)) -_write(s::AsyncStream, p::Ptr{Void}, nb::Integer) = ccall(:jl_write, Uint,(Ptr{Void}, Ptr{Void}, Uint),handle(s),p,uint(nb)) +write(s::AsyncStream, p::Ptr, nb::Integer) = ccall(:jl_write, Int, (Ptr{Void}, Ptr{Void}, Uint), handle(s), p, uint(nb)) +_write(s::AsyncStream, p::Ptr{Void}, nb::Integer) = ccall(:jl_write, Int, (Ptr{Void}, Ptr{Void}, Uint), handle(s), p, uint(nb)) _jl_connect_raw(sock::TcpSocket,sockaddr::Ptr{Void}) = ccall(:jl_connect_raw,Int32,(Ptr{Void},Ptr{Void}),sock.handle,sockaddr) _jl_getaddrinfo(loop::Ptr{Void},host::ByteString,service::Ptr{Void},cb::Function) = ccall(:jl_getaddrinfo,Int32,(Ptr{Void},Ptr{Uint8},Ptr{Uint8},Function),loop,host,service,cb) diff --git a/src/julia.h b/src/julia.h index d8aed3ae6df28..c23e4f88a69dc 100644 --- a/src/julia.h +++ b/src/julia.h @@ -1125,7 +1125,7 @@ DLLEXPORT void jl_free2(void *p, void *hint); DLLEXPORT int jl_cpu_cores(void); -DLLEXPORT int jl_write(uv_stream_t *stream, const char *str, size_t n); +DLLEXPORT size_t jl_write(uv_stream_t *stream, const char *str, size_t n); DLLEXPORT int jl_printf(uv_stream_t *s, const char *format, ...); DLLEXPORT int jl_vprintf(uv_stream_t *s, const char *format, va_list args); diff --git a/src/wrapper.c b/src/wrapper.c index c70aad2b38467..e38ecfc585ba3 100755 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -408,9 +408,9 @@ DLLEXPORT int jl_putc(unsigned char c, uv_stream_t *stream) } } -DLLEXPORT int jl_write(uv_stream_t *stream, const char *str, size_t n) +DLLEXPORT size_t jl_write(uv_stream_t *stream, const char *str, size_t n) { -//TODO: BAD!! Needed because Julia can't yet detect null stdio + //TODO: BAD!! Needed because Julia can't yet detect null stdio if (stream == 0) return 0; if (stream->type