Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port wrapt to heap types and stable ABI #187

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

tiran
Copy link

@tiran tiran commented Sep 28, 2021

Port wrapt to use heap types and stable ABI. Python's stable ABI does not support static types and makes type struct opaque.

wrapt uses some features that are not supported by limited API in Python 3.8 and earlier. I modified the code to use unstable API for 3.6 to 3.8 and limited API + abi3 for 3.9+.

@GrahamDumpleton
Copy link
Owner

I would need to make a last release with Python 2.7 support first. I keep forgetting to release 1.13 with all the wheel stuff. I should do that tomorrow I guess. :-)

@tiran
Copy link
Author

tiran commented Sep 28, 2021

I'm unable to run any tests locally:

$ tox -e py39-install-extensions
...
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 33, in __getattr__
    return next(
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 34, in <genexpr>
    ast.literal_eval(statement.value)
  File "/usr/lib64/python3.9/ast.py", line 105, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib64/python3.9/ast.py", line 104, in _convert
    return _convert_signed_num(node)
  File "/usr/lib64/python3.9/ast.py", line 78, in _convert_signed_num
    return _convert_num(node)
  File "/usr/lib64/python3.9/ast.py", line 69, in _convert_num
    _raise_malformed_node(node)
  File "/usr/lib64/python3.9/ast.py", line 66, in _raise_malformed_node
    raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <ast.Call object at 0x7fc5ffbdd970>

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

Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 387, in _parse_attr
    return getattr(StaticModule(module_name), attr_name)
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 41, in __getattr__
    raise AttributeError(
AttributeError: wrapt has no attribute __version__

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/heimes/dev/py/wrapt/setup.py", line 33, in <module>
    setuptools.setup(
  File "/usr/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib64/python3.9/distutils/core.py", line 121, in setup
    dist.parse_config_files()
  File "/usr/lib/python3.9/site-packages/setuptools/dist.py", line 680, in parse_config_files
    parse_configuration(self, self.command_options,
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 157, in parse_configuration
    meta.parse()
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 463, in parse
    section_parser_method(section_options)
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 436, in parse_section
    self[name] = value
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 220, in __setitem__
    value = parser(value)
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 552, in _parse_version
    version = self._parse_attr(value, self.package_dir)
  File "/usr/lib/python3.9/site-packages/setuptools/config.py", line 390, in _parse_attr
    module = importlib.import_module(module_name)
  File "/usr/lib64/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/heimes/dev/py/wrapt/src/wrapt/__init__.py", line 13, in <module>
    from .importer import (register_post_import_hook, when_imported,
  File "/home/heimes/dev/py/wrapt/src/wrapt/importer.py", line 48, in <module>
    def register_post_import_hook(hook, name):
AttributeError: 'function' object has no attribute 'keys'

@GrahamDumpleton
Copy link
Owner

Not sure whether was same error, but tox on macOS has been broken for me for a while, but under Linux with GitHub actions has been working fine. So I wasn't sure if was some local issue in my setup.

@GrahamDumpleton
Copy link
Owner

Actually, was also working find under macOS and Windows under GitHub actions, thus why figured my system was broken.

@GrahamDumpleton
Copy link
Owner

My memory is obviously bad, tox runs fine for me locally so I obviously fixed the issue. This is before your changes though. Not tried those and they fail for all C extensions tests under GitHub actions as well.

@tiran tiran force-pushed the heap_types branch 2 times, most recently from ab1c761 to c584b0d Compare September 28, 2021 18:56
@tiran tiran force-pushed the heap_types branch 2 times, most recently from 0c37cd2 to c9530de Compare September 29, 2021 10:18
@tiran tiran marked this pull request as ready for review September 29, 2021 10:22
@tiran
Copy link
Author

tiran commented Sep 29, 2021

@GrahamDumpleton I figured out all the segfaults and got most tests passing. There are two failing tests for Python 3.10's annotation overrides left.

@tiran tiran changed the title [WIP] Port wrapt to heap types and stable ABI Port wrapt to heap types and stable ABI Sep 29, 2021
@GrahamDumpleton
Copy link
Owner

@tiran
Copy link
Author

tiran commented Sep 29, 2021

I filed https://bugs.python.org/issue45319 for the __annotations__ issue with Python 3.10

#define WRAPT_HEAPTYPE_SUPPORT_OFFSET 1
#endif

/* Instanes of heap types in Python < 3.8 don't hold strong ref to their
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fairly sure this is not the case:

Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class C: pass
...
>>> import sys
>>> sys.getrefcount(C)
5
>>> x = [C() for _ in range(100)]
>>> sys.getrefcount(C)
105

In code:
https://github.com/python/cpython/blob/v3.6.15/Objects/typeobject.c#L946
https://github.com/python/cpython/blob/v3.6.15/Objects/typeobject.c#L1228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants