Skip to content

Commit

Permalink
add optional argument to errno() for setting it, and update docs. fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jan 24, 2014
1 parent c9447ac commit 0304a6a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion base/error.jl
Expand Up @@ -32,7 +32,8 @@ catch_backtrace() = ccall(:jl_get_backtrace, Array{Ptr{Void},1}, ())

## system error handling ##

errno() = ccall(:jl_errno, Int32, ())
errno() = ccall(:jl_errno, Cint, ())
errno(e::Integer) = ccall(:jl_set_errno, Void, (Cint,), e)
strerror(e::Integer) = bytestring(ccall(:strerror, Ptr{Uint8}, (Int32,), e))
strerror() = strerror(errno())
systemerror(p, b::Bool) = b ? throw(SystemError(string(p))) : nothing
Expand Down
48 changes: 25 additions & 23 deletions doc/stdlib/base.rst
Expand Up @@ -4844,6 +4844,18 @@ C Interface

Close shared library referenced by handle.

.. function:: find_library(names, locations)

Searches for the first library in ``names`` in the paths in the ``locations`` list, ``DL_LOAD_PATH``, or system
library paths (in that order) which can successfully be dlopen'd. On success, the return value will be one of
the names (potentially prefixed by one of the paths in locations). This string can be assigned to a ``global const``
and used as the library name in future ``ccall``'s. On failure, it returns the empty string.

.. data:: DL_LOAD_PATH

When calling ``dlopen``, the paths in this list will be searched first, in order, before searching the
system locations for a valid library handle.

.. function:: c_malloc(size::Integer)

Call ``malloc`` from the C standard library.
Expand Down Expand Up @@ -4923,17 +4935,22 @@ C Interface
Re-enable Ctrl-C handler during execution of a function. Temporarily
reverses the effect of ``disable_sigint``.

.. function:: find_library(names, locations)
.. function:: errno([code])

Searches for the first library in ``names`` in the paths in the ``locations`` list, ``DL_LOAD_PATH``, or system
library paths (in that order) which can successfully be dlopen'd. On success, the return value will be one of
the names (potentially prefixed by one of the paths in locations). This string can be assigned to a ``global const``
and used as the library name in future ``ccall``'s. On failure, it returns the empty string.
Get the value of the C library's ``errno``. If an argument is specified, it is
used to set the value of ``errno``.

.. data:: DL_LOAD_PATH
The value of ``errno`` is only valid immediately after a ``ccall`` to a C
library routine that sets it. Specifically, you cannot call ``errno`` at the next
prompt in a REPL, because lots of code is executed between prompts.

When calling ``dlopen``, the paths in this list will be searched first, in order, before searching the
system locations for a valid library handle.
.. function:: systemerror(sysfunc, iftrue)

Raises a ``SystemError`` for ``errno`` with the descriptive string ``sysfunc`` if ``bool`` is true

.. function:: strerror(n)

Convert a system call error code to a descriptive string

.. data:: Cchar

Expand Down Expand Up @@ -5030,21 +5047,6 @@ Errors
Get the backtrace of the current exception, for use within ``catch``
blocks.

.. function:: errno()

Get the value of the C library's ``errno``. As in C, ``errno()`` must be called
directly after a function that uses ``errno`` for error reporting. Specifically
you can not call ``errno`` on the next prompt in a REPL, because lots of code
is executed between REPL prompts.

.. function:: systemerror(sysfunc, iftrue)

Raises a ``SystemError`` for ``errno`` with the descriptive string ``sysfunc`` if ``bool`` is true

.. function:: strerror(n)

Convert a system call error code to a descriptive string

.. function:: assert(cond, [text])

Raise an error if ``cond`` is false. Also available as the macro ``@assert expr``.
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Expand Up @@ -822,6 +822,7 @@ DLLEXPORT jl_value_t *jl_eqtable_get(jl_array_t *h, void *key, jl_value_t *deflt

// system information
DLLEXPORT int jl_errno(void);
DLLEXPORT void jl_set_errno(int e);
DLLEXPORT int32_t jl_stat(const char* path, char* statbuf);

// environment entries
Expand Down
1 change: 1 addition & 0 deletions src/sys.c
Expand Up @@ -297,6 +297,7 @@ void jl_free2(void *p, void *hint)
// -- syscall utilities --

int jl_errno(void) { return errno; }
void jl_set_errno(int e) { errno = e; }

// -- get the number of CPU cores --

Expand Down

0 comments on commit 0304a6a

Please sign in to comment.