File: ctypes_support.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (43 lines) | stat: -rw-r--r-- 1,359 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
""" This file provides some support for things like standard_c_lib and
errno access, as portable as possible
"""

import ctypes
import ctypes.util
import sys

# __________ the standard C library __________

if sys.platform == 'win32':
    import _ffi
    standard_c_lib = ctypes.CDLL('msvcrt', handle=_ffi.get_libc())
elif sys.platform == 'cygwin':
    standard_c_lib = ctypes.CDLL(ctypes.util.find_library('cygwin'))
else:
    standard_c_lib = ctypes.CDLL(ctypes.util.find_library('c'))

if sys.platform == 'win32':
    standard_c_lib._errno.restype = ctypes.POINTER(ctypes.c_int)
    standard_c_lib._errno.argtypes = None
    def _where_is_errno():
        return standard_c_lib._errno()

elif sys.platform in ('linux2', 'freebsd6'):
    standard_c_lib.__errno_location.restype = ctypes.POINTER(ctypes.c_int)
    standard_c_lib.__errno_location.argtypes = None
    def _where_is_errno():
        return standard_c_lib.__errno_location()

elif sys.platform == 'darwin' or sys.platform.startswith('freebsd'):
    standard_c_lib.__error.restype = ctypes.POINTER(ctypes.c_int)
    standard_c_lib.__error.argtypes = None
    def _where_is_errno():
        return standard_c_lib.__error()

def get_errno():
    errno_p = _where_is_errno()
    return errno_p.contents.value

def set_errno(value):
    errno_p = _where_is_errno()
    errno_p.contents.value = value