| 1 |
""" This file provides some support for things like standard_c_lib and |
| 2 |
errno access, as portable as possible |
| 3 |
""" |
| 4 |
|
| 5 |
import ctypes |
| 6 |
import ctypes.util |
| 7 |
import sys |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
if sys.platform == 'win32': |
| 12 |
import _ffi |
| 13 |
standard_c_lib = ctypes.CDLL('msvcrt', handle=_ffi.get_libc()) |
| 14 |
elif sys.platform == 'cygwin': |
| 15 |
standard_c_lib = ctypes.CDLL(ctypes.util.find_library('cygwin')) |
| 16 |
else: |
| 17 |
standard_c_lib = ctypes.CDLL(ctypes.util.find_library('c')) |
| 18 |
|
| 19 |
if sys.platform == 'win32': |
| 20 |
standard_c_lib._errno.restype = ctypes.POINTER(ctypes.c_int) |
| 21 |
standard_c_lib._errno.argtypes = None |
| 22 |
def _where_is_errno(): |
| 23 |
return standard_c_lib._errno() |
| 24 |
|
| 25 |
elif sys.platform in ('linux', 'freebsd6'): |
| 26 |
standard_c_lib.__errno_location.restype = ctypes.POINTER(ctypes.c_int) |
| 27 |
standard_c_lib.__errno_location.argtypes = None |
| 28 |
def _where_is_errno(): |
| 29 |
return standard_c_lib.__errno_location() |
| 30 |
|
| 31 |
elif sys.platform == 'darwin' or sys.platform.startswith('freebsd'): |
| 32 |
standard_c_lib.__error.restype = ctypes.POINTER(ctypes.c_int) |
| 33 |
standard_c_lib.__error.argtypes = None |
| 34 |
def _where_is_errno(): |
| 35 |
return standard_c_lib.__error() |
| 36 |
|
| 37 |
def get_errno(): |
| 38 |
errno_p = _where_is_errno() |
| 39 |
return errno_p.contents.value |
| 40 |
|
| 41 |
def set_errno(value): |
| 42 |
errno_p = _where_is_errno() |
| 43 |
errno_p.contents.value = value |