Skip to content

Commit

Permalink
python/cffi_example: use a specific tag when recompiling with bare CC
Browse files Browse the repository at this point in the history
There are issues when using CC='clang -m32' with PyPy-3.7

    python ./cffi_example.py
    getuid() = 1001
    getgid() = 121
    getpid() = 22233
    Traceback (most recent call last):
      File "./cffi_example.py", line 37, in <module>
        libv = ffi.verify('const size_t LONG_SIZE = sizeof(long);')
      File "/opt/hostedtoolcache/PyPy/3.7.10/x64/lib_pypy/cffi/api.py", line 468, in verify
        lib = self.verifier.load_library()
      File "/opt/hostedtoolcache/PyPy/3.7.10/x64/lib_pypy/cffi/verifier.py", line 106, in load_library
        return self._load_library()
      File "/opt/hostedtoolcache/PyPy/3.7.10/x64/lib_pypy/cffi/verifier.py", line 217, in _load_library
        return self._vengine.load_library()
      File "/opt/hostedtoolcache/PyPy/3.7.10/x64/lib_pypy/cffi/vengine_gen.py", line 70, in load_library
        module = backend.load_library(filename, flags)
    OSError: Cannot load library
    /home/runner/work/shared/shared/python/__pycache__/_cffi__g2731a638xb51bc5c3.pypy37-pp73-x86_64-linux-gnu.so:
    /home/runner/work/shared/shared/python/__pycache__/_cffi__g2731a638xb51bc5c3.pypy37-pp73-x86_64-linux-gnu.so:
    wrong ELF class: ELFCLASS32

This is caused by the "retry without $CC" logic using the same cached
filename (here
_cffi__g2731a638xb51bc5c3.pypy37-pp73-x86_64-linux-gnu.so). Instead,
define a custom tag which is included in the file name:
https://foss.heptapod.net/pypy/pypy/-/blob/release-pypy3.7-v7.3.3/lib_pypy/cffi/verifier.py#L62

NB. Use a tag with an underscore, as it is used to name some functions
of the C module, in CPython 3.5:

    __pycache__/_cffi_empty-cc_xfda08e47xb51bc5c3.c:303:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘-’ token
     PyInit__cffi_empty-cc_xfda08e47xb51bc5c3(void)
                       ^
    __pycache__/_cffi_empty-cc_xfda08e47xb51bc5c3.c:293:27: warning: ‘_cffi_module_def’ defined but not used [-Wunused-variable]
     static struct PyModuleDef _cffi_module_def = {
                               ^~~~~~~~~~~~~~~~
  • Loading branch information
fishilico committed Sep 26, 2021
1 parent fcadeeb commit 5c31347
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/cffi_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
ffi.cdef('const size_t LONG_SIZE;')
try:
libv = ffi.verify('const size_t LONG_SIZE = sizeof(long);')
except cffi.VerificationError as exc:
except (cffi.VerificationError, OSError) as exc:
# This may have when using a custom $CC
if initial_cc:
print(exc)
print("... Warning: cffi.verify failed with CC={}, retrying without".format(repr(initial_cc)))
del os.environ['CC']
libv = ffi.verify('const size_t LONG_SIZE = sizeof(long);')
# Use a tag in order to force using another cached module file (to force recompilation)
libv = ffi.verify('const size_t LONG_SIZE = sizeof(long);', tag='empty_cc')
os.environ['CC'] = initial_cc
else:
raise
Expand Down

0 comments on commit 5c31347

Please sign in to comment.