'SystemError: <built-in function hasattr> returned a result with an error set' accessing non-existant attributes of a Lib object

Issue #352 resolved
Former user created an issue

Using cffi 1.11.2, I see this in gevent on CPython 3.5.x and 3.6.x on both macOS and Linux (TravisCI). Here's a stack trace from the test case that triggers it:

AttributeError: cffi library 'gevent.libuv._corecffi' has no function, constant or global variable named '__loader__'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
...
  File "test__signal.py", line 84, in test_reload
    reload_module(site)
  File "//lib/python3.6/importlib/__init__.py", line 166, in reload
    _bootstrap._exec(spec, module)
  File "<frozen importlib._bootstrap>", line 618, in _exec
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "//lib/python3.6/site.py", line 705, in <module>
    main()
  File "//lib/python3.6/site.py", line 673, in main
    abs__file__()
  File "//lib/python3.6/site.py", line 106, in abs__file__
    hasattr(m, '__loader__')):
SystemError: <built-in function hasattr> returned a result with an error set

The object that's causing the problem is <Lib object for 'gevent.libuv._corecffi'>. This appears to be inserted in sys.modules under the key 'gevent.libuv._corecffi.lib' automatically when gevent.libuv._corecffi is imported. So it's seen when iterating sys.modules, leading reload(site) to blow up.

But this is also easily reproduced at the REPL:

Python 3.6.4 (default, Dec 21 2017, 20:33:21)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gevent.libuv._corecffi
>>> gevent.libuv._corecffi.lib
<Lib object for 'gevent.libuv._corecffi'>
>>> hasattr(gevent.libuv._corecffi.lib, '__loader__')
AttributeError: cffi library 'gevent.libuv._corecffi' has no function, constant or global variable named '__loader__'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: <built-in function hasattr> returned a result with an error set

Accessing the attribute by hand produces the same error:

>>> gevent.libuv._corecffi.lib.__loader__
AttributeError: cffi library 'gevent.libuv._corecffi' has no function, constant or global variable named '__loader__'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pprint.py", line 52, in pprint
    compact=compact)
SystemError: <class 'pprint.PrettyPrinter'> returned a result with an error set

I don't reproduce this on CPython 2.7.14 with either cffi 1.8.3 or 1.11.2.

Comments (5)

  1. Armin Rigo

    Oops. Fixed by d16706e3c2da. The lib objects have on Python 3 some dummy attributes, __loader__ and __spec__ set to None, otherwise other things explode; but I didn't realize that it never worked. Fixed. Now if you get issues because it is None, please reopen or file a new issue...

  2. Jason Madden

    Cool, thanks! I'm aware of an issue with reload(site) if it finds module objects with __cached__ of None (if they're missing completely that's fine) because it does m.__cached__ = os.path.abspath(m.__cached__) and only catches AttributeError and OSError, but abspath(None) raises TypeError.

  3. Log in to comment