Cstring as ccall return value

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Cstring as ccall return value

maleadt
Does it make sense to use Cstring as the return value type when ccall'ing methods returning a char* string? I know the type is mainly intended to avoid converting Julia strings containing \0 to C strings, but I thought it would also make sense to use it as the ccall return type because it makes the code more readable (i.e. "this code returns a string" rather than "it's just a pointer").

However, I'm afraid there might be a gotcha:
julia> Ptr{UInt8}(0x0) == C_NULL
true

julia
> Cstring(Ptr{UInt8}(0x0)) == C_NULL
false
Is this intended? Some API's (for example tempnam) return NULL upon failure, so that would break when using Cstring as ccall's return value type and subsequently comparing against C_NULL.


Reply | Threaded
Open this post in threaded view
|

Re: Cstring as ccall return value

Stefan Karpinski
That comparison should really either return true or fail. Could you open an issue?

On Fri, Nov 13, 2015 at 8:28 AM, maleadt <[hidden email]> wrote:
Does it make sense to use Cstring as the return value type when ccall'ing methods returning a char* string? I know the type is mainly intended to avoid converting Julia strings containing \0 to C strings, but I thought it would also make sense to use it as the ccall return type because it makes the code more readable (i.e. "this code returns a string" rather than "it's just a pointer").

However, I'm afraid there might be a gotcha:
julia> Ptr{UInt8}(0x0) == C_NULL
true

julia
> Cstring(Ptr{UInt8}(0x0)) == C_NULL
false
Is this intended? Some API's (for example tempnam) return NULL upon failure, so that would break when using Cstring as ccall's return value type and subsequently comparing against C_NULL.



Reply | Threaded
Open this post in threaded view
|

Re: Cstring as ccall return value

maleadt
Done, see issue #13974.

Op vrijdag 13 november 2015 15:37:10 UTC+1 schreef Stefan Karpinski:
That comparison should really either return true or fail. Could you open an issue?

On Fri, Nov 13, 2015 at 8:28 AM, maleadt <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="zT2sVGpvBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">tim.b...@...> wrote:
Does it make sense to use Cstring as the return value type when ccall'ing methods returning a char* string? I know the type is mainly intended to avoid converting Julia strings containing \0 to C strings, but I thought it would also make sense to use it as the ccall return type because it makes the code more readable (i.e. "this code returns a string" rather than "it's just a pointer").

However, I'm afraid there might be a gotcha:
julia> Ptr{UInt8}(0x0) == C_NULL
true

julia
> Cstring(Ptr{UInt8}(0x0)) == C_NULL
false
Is this intended? Some API's (for example tempnam) return NULL upon failure, so that would break when using Cstring as ccall's return value type and subsequently comparing against C_NULL.