| 1 |
|
| 2 |
import sys, os |
| 3 |
from cffi._imp_emulation import load_dynamic |
| 4 |
|
| 5 |
if sys.version_info < (3,): |
| 6 |
__all__ = ['u', 'arraytostring', 'load_dynamic'] |
| 7 |
|
| 8 |
class U(object): |
| 9 |
def __add__(self, other): |
| 10 |
return eval('u'+repr(other).replace(r'\\u', r'\u') |
| 11 |
.replace(r'\\U', r'\U')) |
| 12 |
u = U() |
| 13 |
long = long |
| 14 |
assert u+'a\x00b' == eval(r"u'a\x00b'") |
| 15 |
assert u+'a\u1234b' == eval(r"u'a\u1234b'") |
| 16 |
assert u+'a\U00012345b' == eval(r"u'a\U00012345b'") |
| 17 |
def arraytostring(a): |
| 18 |
return a.tostring() |
| 19 |
|
| 20 |
else: |
| 21 |
__all__ = ['u', 'unicode', 'long', 'arraytostring', 'load_dynamic'] |
| 22 |
u = "" |
| 23 |
unicode = str |
| 24 |
long = int |
| 25 |
def arraytostring(a): |
| 26 |
return a.tobytes() |
| 27 |
|
| 28 |
|
| 29 |
class StdErrCapture(object): |
| 30 |
"""Capture writes to sys.stderr (not to the underlying file descriptor).""" |
| 31 |
def __enter__(self): |
| 32 |
try: |
| 33 |
from StringIO import StringIO |
| 34 |
except ImportError: |
| 35 |
from io import StringIO |
| 36 |
self.old_stderr = sys.stderr |
| 37 |
sys.stderr = f = StringIO() |
| 38 |
if hasattr(sys, '__unraisablehook__'): |
| 39 |
self.old_unraisablebook = sys.unraisablehook |
| 40 |
sys.unraisablehook = sys.__unraisablehook__ |
| 41 |
return f |
| 42 |
def __exit__(self, *args): |
| 43 |
sys.stderr = self.old_stderr |
| 44 |
if hasattr(self, 'old_unraisablebook'): |
| 45 |
sys.unraisablehook = self.old_unraisablebook |
| 46 |
|
| 47 |
|
| 48 |
class FdWriteCapture(object): |
| 49 |
"""xxx limited to capture at most 512 bytes of output, according |
| 50 |
to the Posix manual.""" |
| 51 |
|
| 52 |
def __init__(self, capture_fd=2): |
| 53 |
if sys.platform == 'win32': |
| 54 |
import pytest |
| 55 |
pytest.skip("seems not to work, too bad") |
| 56 |
self.capture_fd = capture_fd |
| 57 |
|
| 58 |
def __enter__(self): |
| 59 |
import os |
| 60 |
self.read_fd, self.write_fd = os.pipe() |
| 61 |
self.copy_fd = os.dup(self.capture_fd) |
| 62 |
os.dup2(self.write_fd, self.capture_fd) |
| 63 |
return self |
| 64 |
|
| 65 |
def __exit__(self, *args): |
| 66 |
import os |
| 67 |
os.dup2(self.copy_fd, self.capture_fd) |
| 68 |
os.close(self.copy_fd) |
| 69 |
os.close(self.write_fd) |
| 70 |
self._value = os.read(self.read_fd, 512) |
| 71 |
os.close(self.read_fd) |
| 72 |
|
| 73 |
def getvalue(self): |
| 74 |
return self._value |
| 75 |
|
| 76 |
def _verify(ffi, module_name, preamble, *args, **kwds): |
| 77 |
from cffi.recompiler import recompile |
| 78 |
from .udir import udir |
| 79 |
assert module_name not in sys.modules, "module name conflict: %r" % ( |
| 80 |
module_name,) |
| 81 |
kwds.setdefault('tmpdir', str(udir)) |
| 82 |
outputfilename = recompile(ffi, module_name, preamble, *args, **kwds) |
| 83 |
module = load_dynamic(module_name, outputfilename) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
for name in dir(module.ffi): |
| 88 |
if not name.startswith('_'): |
| 89 |
attr = getattr(module.ffi, name) |
| 90 |
if attr is not getattr(ffi, name, object()): |
| 91 |
setattr(ffi, name, attr) |
| 92 |
def typeof_disabled(*args, **kwds): |
| 93 |
raise NotImplementedError |
| 94 |
ffi._typeof = typeof_disabled |
| 95 |
for name in dir(ffi): |
| 96 |
if not name.startswith('_') and not hasattr(module.ffi, name): |
| 97 |
setattr(ffi, name, NotImplemented) |
| 98 |
return module.lib |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
if sys.platform == 'win32': |
| 106 |
extra_compile_args = [] |
| 107 |
else: |
| 108 |
if (sys.platform == 'darwin' and |
| 109 |
[int(x) for x in os.uname()[2].split('.')] >= [11, 0, 0]): |
| 110 |
|
| 111 |
extra_compile_args = ['-Werror', '-Wall', '-Wextra', '-Wconversion', |
| 112 |
'-Wno-unused-parameter', |
| 113 |
'-Wno-unreachable-code'] |
| 114 |
|
| 115 |
extra_compile_args.append('-Qunused-arguments') |
| 116 |
else: |
| 117 |
|
| 118 |
extra_compile_args = ['-Werror', '-Wall', '-Wextra', '-Wconversion', |
| 119 |
'-Wno-unused-parameter', |
| 120 |
'-Wno-unreachable-code'] |
| 121 |
|
| 122 |
is_musl = False |
| 123 |
if sys.platform == 'linux': |
| 124 |
try: |
| 125 |
from packaging.tags import platform_tags |
| 126 |
except ImportError: |
| 127 |
pass |
| 128 |
else: |
| 129 |
tagset = frozenset(platform_tags()) |
| 130 |
is_musl = any(t.startswith('musllinux') for t in tagset) |
| 131 |
if is_musl and sys.version_info >= (3, 12): |
| 132 |
if any(t.startswith('musllinux_1_1_') for t in tagset) and not any(t.startswith('musllinux_1_2_') for t in tagset): |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
extra_compile_args.append('-Wno-error=sign-conversion') |
| 137 |
|
| 138 |
del platform_tags |