can it be used to convert a c FILE (STDOUT) to a specific julia IO object? On Wed, Mar 9, 2016 at 6:43 PM, Yichao Yu <[email protected]> wrote:
> On Wed, Mar 9, 2016 at 5:36 PM, Yichao Yu <[email protected]> wrote: > > On Wed, Mar 9, 2016 at 5:21 PM, Jeffrey Sarnoff > > <[email protected]> wrote: > >> In both version 0.4 and 0.5-dev everything is as you show until: > >> pointer_to_array(Ptr{UInt8}(ptr[]), sz[], true) > >> then > >> julia> pointer_to_array(Ptr{UInt8}(ptr[]), sz[], true) > >> 140685948747867-element Array{UInt8,1}: > > > > What I see is that `sz` is not updated until the stream is > > flushed/closed. Calling `fclose` here does the job you can try to add > > a call to `fflush` to see if it helps. > > I didn't realized that there's Libc.FILE which can be used to convert > a julia IO to a c FILE. However, for reasons I've already noted, you > can't use it to convert arbitrary julia IO object and the two are not > sync'd with each other. > > > > >> > >> signal (11): Segmentation fault > >> while loading no file, in expression starting on line 0 > >> alignment at ./show.jl:1104 > >> unknown function (ip: 0x7ff401d4117f) > >> [inline] at > >> > /home/centos/buildbot/slave/package_tarball64/build/src/julia_internal.h:63 > >> jl_call_method_internal at > >> /home/centos/buildbot/slave/package_tarball64/build/src/gf.c:1914 > >> ... > >> jl_apply_generic at > >> /home/centos/buildbot/slave/package_tarball64/build/src/gf.c:1917 > >> run_frontend at ./REPL.jl:860 > >> run_repl at ./REPL.jl:168 > >> unknown function (ip: 0x7ff4023431e3) > >> [inline] at > >> > /home/centos/buildbot/slave/package_tarball64/build/src/julia_internal.h:63 > >> jl_call_method_internal at > >> /home/centos/buildbot/slave/package_tarball64/build/src/gf.c:1914 > >> _start at ./client.jl:342 > >> unknown function (ip: 0x7ff6042f0199) > >> jl_apply_generic at > >> /home/centos/buildbot/slave/package_tarball64/build/src/gf.c:1917 > >> unknown function (ip: 0x401c64) > >> unknown function (ip: 0x40181f) > >> __libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line) > >> unknown function (ip: 0x401865) > >> /home/jas/Desktop/Juliav05: line 2: 4104 Segmentation fault > >> /usr/bin/julia > >> _ > >> (generic Linux version of julia) > >> > >> On Wednesday, March 9, 2016 at 2:55:31 PM UTC-5, Jeffrey Sarnoff wrote: > >>> > >>> Thank you, much appreciated. > >>> > >>> On Wednesday, March 9, 2016 at 2:52:01 PM UTC-5, Yichao Yu wrote: > >>>> > >>>> On Wed, Mar 9, 2016 at 1:05 PM, Jeffrey Sarnoff > >>>> <[email protected]> wrote: > >>>> > I am bound to the api, at least for calling into the application. I > >>>> > don't > >>>> > know how to do either of the things you recommend. Could you point > me > >>>> > to an > >>>> > example? > >>>> > >>>> See below. Note that the `open_memstream` API may not be widely > >>>> available on non-Linux. (Apparently it's aded to POSIX though...) > >>>> The reason you have to deal directly with C FILE* is that the C FILE* > >>>> API is not designed to be extensible (or at least there's no portable > >>>> API to do so). Therefore, you can't convert a opaque julia IO object > >>>> to a C FILE* and let C libraries write through it instead... > >>>> > >>>> ``` > >>>> julia> p_cstdout = unsafe_load(cglobal(:stdout, Ptr{Void})) > >>>> Ptr{Void} @0x00007f6dcb81f600 > >>>> > >>>> julia> ccall(:fwrite, Csize_t, (Cstring, Csize_t, Csize_t, Ptr{Void}), > >>>> "aaa\n", 4, 1, p_cstdout) > >>>> aaa > >>>> 0x0000000000000001 > >>>> > >>>> julia> ptr = Ref{Ptr{Void}}() > >>>> Base.RefValue{Ptr{Void}}(Ptr{Void} @0x000000000000000e) > >>>> > >>>> julia> sz = Ref{Csize_t}() > >>>> Base.RefValue{UInt64}(0x00007f6bc942f5f0) > >>>> > >>>> julia> strstm = ccall(:open_memstream, Ptr{Void}, (Ptr{Ptr{Void}}, > >>>> Ptr{Csize_t}), ptr, sz) > >>>> Ptr{Void} @0x0000000002638f70 > >>>> > >>>> julia> ccall(:fwrite, Csize_t, (Cstring, Csize_t, Csize_t, Ptr{Void}), > >>>> "aaa\n", 4, 1, strstm) > >>>> 0x0000000000000001 > >>>> > >>>> julia> ccall(:fwrite, Csize_t, (Cstring, Csize_t, Csize_t, Ptr{Void}), > >>>> "aaa\n", 4, 1, strstm) > >>>> 0x0000000000000001 > >>>> > >>>> julia> sz[] > >>>> 0x00007f6bc942f5f0 > >>>> > >>>> julia> ccall(:fclose, Cint, (Ptr{Void},), strstm) > >>>> 0 > >>>> > >>>> julia> sz[] > >>>> 0x0000000000000008 > >>>> > >>>> julia> pointer_to_array(Ptr{UInt8}(ptr[]), sz[], true) > >>>> 8-element Array{UInt8,1}: > >>>> 0x61 > >>>> 0x61 > >>>> 0x61 > >>>> 0x0a > >>>> 0x61 > >>>> 0x61 > >>>> 0x61 > >>>> 0x0a > >>>> > >>>> julia> ary = ans > >>>> 8-element Array{UInt8,1}: > >>>> 0x61 > >>>> 0x61 > >>>> 0x61 > >>>> 0x0a > >>>> 0x61 > >>>> 0x61 > >>>> 0x61 > >>>> 0x0a > >>>> > >>>> julia> bytestring(ary) > >>>> "aaa\naaa\n" > >>>> ``` > >>>> > >>>> > > >>>> > On Wednesday, March 9, 2016 at 12:58:20 PM UTC-5, Yichao Yu wrote: > >>>> >> > >>>> >> > >>>> >> On Mar 9, 2016 12:38 PM, "Jeffrey Sarnoff" <[email protected]> > >>>> >> wrote: > >>>> >> > > >>>> >> > I am trying to wrap this so it will (a) print to STDOUT (b) (if > >>>> >> > possible) print to a string: > >>>> >> > void arf_fprint(FILE * file, const arf_t x)ΒΆ > >>>> >> > > >>>> >> > Prints x as an integer mantissa and exponent to the stream file. > >>>> >> > > >>>> >> > > >>>> >> > arf_t is made with arf(x), that works. > >>>> >> > > >>>> >> > > >>>> >> > this: > >>>> >> > > >>>> >> > function arf_fprint(x::arb) > >>>> >> > > >>>> >> > a = arf(x); s=STDOUT > >>>> >> > > >>>> >> > ccall((:arf_fprint, :libarb), Void, (Ptr{s}, > >>>> >> > Ptr{arf_struct},), &s, &a) > >>>> >> > >>>> >> Julia does not use c FILE*, if you are bound to this api, you > should > >>>> >> probably fetch the c stream with cglobal or construct a string > stream > >>>> >> with > >>>> >> platform dependent api. > >>>> >> > >>>> >> > > >>>> >> > end > >>>> >> > > >>>> >> > does this: > >>>> >> > ERROR: TypeError: Ptr: in parameter, expected Type{T}, got > Base.TTY > >>>> >> > in arf_fprint at none:3 > >>>> >> > >
