alexmojaki / pure_eval

Safely evaluate AST nodes without side effects
MIT License
42 stars 14 forks source link

Test failure with Python 3.13.0b1 #16

Closed frenzymadness closed 2 months ago

frenzymadness commented 2 months ago

When run with Python 3.13.0b1 the test_sys_modules fails:

==================================== test session starts ====================================
platform linux -- Python 3.13.0b1, pytest-8.2.0, pluggy-1.5.0
cachedir: .tox/py313/.pytest_cache
rootdir: /home/lbalhar/Software/pure_eval
configfile: pyproject.toml
collected 47 items                                                                          

tests/test_core.py ...........3
......
tests/test_getattr_static.py .......................
tests/test_utils.py /usr/lib64/python3.13/importlib/_bootstrap.py
F......

========================================= FAILURES ==========================================
_____________________________________ test_sys_modules ______________________________________

    def test_sys_modules():
        modules = sys_modules_sources()
        if not os.environ.get('PURE_EVAL_SLOW_TESTS'):
            modules = islice(modules, 0, 3)

        for filename, source, tree in modules:
            print(filename)
            if not filename.endswith("ast.py"):
>               check_copy_ast_without_context(tree)

tests/test_utils.py:53: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

tree = <ast.Module object at 0x7fb367123fd0>

    def check_copy_ast_without_context(tree):
        tree2 = copy_ast_without_context(tree)
        dump1 = ast.dump(tree)
        dump2 = ast.dump(tree2)
        normalised_dump1 = re.sub(
            r", ctx=(Load|Store|Del)\(\)",
            "",
            dump1
        )
>       assert normalised_dump1 == dump2
E       assert "Module(body=...e__'))]))])])" == "Module(body=...e__'))]))])])"
E         
E         Skipping 4274 identical leading characters in diff, use -v to show
E         - alue=List()), Assign(targets=[Attribute(value=Name(id='self'), attr='_iterating')], value=Call(func=Name(id='set'))), Assign(targets=[Attribute(value=Name(id='self'), attr='data')], value=Dict())]), FunctionDef(name='_commit_removals', args=arguments(args=[arg(arg='self')]), body=[Assign(targets=[Name(id='pop')], value=Attribute(value=Attribute(value=Name(id='self'), attr='_pending_removals'), attr='pop')), Assign(targets=[Name(id='d')], value=Attribute(value=Name(id='self'), attr='data')), While(test=C...
E         
E         ...Full output truncated (4 lines hidden), use '-vv' to show

tests/test_utils.py:65: AssertionError
===================================== warnings summary ======================================

I run it with -s so you can see the filename in the output /usr/lib64/python3.13/importlib/_bootstrap.py. Unfortunately, when run with -vv the output is so long that it's not possible for me to detect the actual differences.

frenzymadness commented 2 months ago

So, the difference between normalized_dump1 and dump2 is for example:

78c78
< attr='_pending_removals')], value=List(ctx=Load())), 
---
> attr='_pending_removals')], value=List()),

The re.sub removes ctx.Load() only if it's preceded by , which is not true in all cases. I'm gonna propose a fix for the regular expression.