Compare commits
30 Commits
Author | SHA256 | Date | |
---|---|---|---|
3b79e52c67 | |||
2225355c1c | |||
b4972a92de | |||
c1711054df | |||
99e2f9fe62 | |||
c9ffd7439b | |||
86dd7e31f3 | |||
2bc77f1780 | |||
670786715f | |||
8079568381 | |||
234322b8d1 | |||
2c4a66a529 | |||
1c99f4c877 | |||
77b6be5ecc | |||
41f8d3f1ec | |||
b3c613d68f | |||
8f126b9a45 | |||
e601f2df84 | |||
5fa33bd43a | |||
8f5c9fb5e3 | |||
565b78ffc5 | |||
d0a1905467 | |||
f73bc40d7f | |||
2011585994 | |||
3e72f4dff0 | |||
7ca05023b8 | |||
74d90c2185 | |||
9414af4e35 | |||
a58769edb1 | |||
aba234eac2 |
@@ -1,3 +0,0 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:35afe2ce3affba8ee97f2d69927fa823b08b472b7b994e36a52a964b93d16147
|
||||
size 836501
|
BIN
executing-2.1.0.tar.gz
(Stored with Git LFS)
BIN
executing-2.1.0.tar.gz
(Stored with Git LFS)
Binary file not shown.
@@ -1,147 +0,0 @@
|
||||
From 4acebfaa89196785ccc893d56b97ac8598c30e71 Mon Sep 17 00:00:00 2001
|
||||
From: Frank Hoffmann <15r10nk-git@polarbit.de>
|
||||
Date: Mon, 26 Aug 2024 21:43:13 +0200
|
||||
Subject: [PATCH 1/2] fix: backward compatibility fix for changed source
|
||||
positions in 3.12.6 (#85)
|
||||
|
||||
---
|
||||
executing/_position_node_finder.py | 65 ++++++++++
|
||||
tests/generate_small_sample.py | 9 -
|
||||
tests/small_samples/3e40f2921fbaf6ccbabb2fa5c3cd3a42b54914d66f7a67f8b2ade36f89ed761b.py | 3
|
||||
tests/test_main.py | 5
|
||||
4 files changed, 79 insertions(+), 3 deletions(-)
|
||||
|
||||
--- a/executing/_position_node_finder.py
|
||||
+++ b/executing/_position_node_finder.py
|
||||
@@ -242,6 +242,66 @@ class PositionNodeFinder(object):
|
||||
# keeping the old behaviour makes it possible to distinguish both cases.
|
||||
|
||||
return node.parent
|
||||
+
|
||||
+ if (
|
||||
+ sys.version_info >= (3, 12, 6)
|
||||
+ and instruction.opname in ("GET_ITER", "FOR_ITER")
|
||||
+ and isinstance(
|
||||
+ node.parent.parent,
|
||||
+ (ast.ListComp, ast.SetComp, ast.DictComp, ast.GeneratorExp),
|
||||
+ )
|
||||
+ and isinstance(node.parent,ast.comprehension)
|
||||
+ and node is node.parent.iter
|
||||
+ ):
|
||||
+ # same as above but only for comprehensions, see:
|
||||
+ # https://github.com/python/cpython/issues/123142
|
||||
+
|
||||
+ return node.parent.parent
|
||||
+
|
||||
+ if sys.version_info >= (3, 12,6) and instruction.opname == "CALL":
|
||||
+ before = self.instruction_before(instruction)
|
||||
+ if (
|
||||
+ before is not None
|
||||
+ and before.opname == "LOAD_CONST"
|
||||
+ and before.positions == instruction.positions
|
||||
+ and isinstance(node.parent, ast.withitem)
|
||||
+ and node is node.parent.context_expr
|
||||
+ ):
|
||||
+ # node positions for with-statements have change
|
||||
+ # and is now equal to the expression which created the context-manager
|
||||
+ # https://github.com/python/cpython/pull/120763
|
||||
+
|
||||
+ # with context_manager:
|
||||
+ # ...
|
||||
+
|
||||
+ # but there is one problem to distinguish call-expressions from __exit__()
|
||||
+
|
||||
+ # with context_manager():
|
||||
+ # ...
|
||||
+
|
||||
+ # the call for __exit__
|
||||
+
|
||||
+ # 20 1:5 1:22 LOAD_CONST(None)
|
||||
+ # 22 1:5 1:22 LOAD_CONST(None)
|
||||
+ # 24 1:5 1:22 LOAD_CONST(None)
|
||||
+ # 26 1:5 1:22 CALL() # <-- same source range as context_manager()
|
||||
+
|
||||
+ # but we can use the fact that the previous load for None
|
||||
+ # has the same source range as the call, wich can not happen for normal calls
|
||||
+
|
||||
+ # we return the same ast.With statement at the and to preserve backward compatibility
|
||||
+
|
||||
+ return node.parent.parent
|
||||
+
|
||||
+ if (
|
||||
+ sys.version_info >= (3, 12,6)
|
||||
+ and instruction.opname == "BEFORE_WITH"
|
||||
+ and isinstance(node.parent, ast.withitem)
|
||||
+ and node is node.parent.context_expr
|
||||
+ ):
|
||||
+ # handle positions changes for __enter__
|
||||
+ return node.parent.parent
|
||||
+
|
||||
return node
|
||||
|
||||
def known_issues(self, node: EnhancedAST, instruction: dis.Instruction) -> None:
|
||||
@@ -880,6 +940,11 @@ class PositionNodeFinder(object):
|
||||
def instruction(self, index: int) -> Optional[dis.Instruction]:
|
||||
return self.bc_dict.get(index,None)
|
||||
|
||||
+ def instruction_before(
|
||||
+ self, instruction: dis.Instruction
|
||||
+ ) -> Optional[dis.Instruction]:
|
||||
+ return self.bc_dict.get(instruction.offset - 2, None)
|
||||
+
|
||||
def opname(self, index: int) -> str:
|
||||
i=self.instruction(index)
|
||||
if i is None:
|
||||
--- a/tests/generate_small_sample.py
|
||||
+++ b/tests/generate_small_sample.py
|
||||
@@ -18,6 +18,7 @@ from rich.progress import Progress, trac
|
||||
from rich.syntax import Syntax
|
||||
from rich.console import Console
|
||||
import argparse
|
||||
+import ast
|
||||
|
||||
last_samples_dir = Path(__file__).parent / "last_samples"
|
||||
last_samples_dir.mkdir(exist_ok=True)
|
||||
@@ -64,6 +65,11 @@ def test_file(filename: Path):
|
||||
|
||||
test = TestFiles()
|
||||
try:
|
||||
+ ast.parse(code)
|
||||
+ except (RecursionError,SyntaxError):
|
||||
+ return True
|
||||
+
|
||||
+ try:
|
||||
with open(os.devnull, "w") as dev_null:
|
||||
with contextlib.redirect_stderr(dev_null):
|
||||
with contextlib.redirect_stdout(dev_null):
|
||||
@@ -122,9 +128,6 @@ def main():
|
||||
break_file.unlink()
|
||||
sys.exit(0)
|
||||
|
||||
- if time.time() > end_time:
|
||||
- print("Timeout")
|
||||
- sys.exit(0)
|
||||
|
||||
if not result:
|
||||
print(f"{filename} is failing the tests -> minimize\n")
|
||||
--- /dev/null
|
||||
+++ b/tests/small_samples/3e40f2921fbaf6ccbabb2fa5c3cd3a42b54914d66f7a67f8b2ade36f89ed761b.py
|
||||
@@ -0,0 +1,3 @@
|
||||
+async def wait():
|
||||
+ async with something:
|
||||
+ pass
|
||||
\ No newline at end of file
|
||||
--- a/tests/test_main.py
|
||||
+++ b/tests/test_main.py
|
||||
@@ -609,6 +609,11 @@ class TestStuff(unittest.TestCase):
|
||||
assert {i: i for i in iter_test(ast.DictComp)} == {1: 1, 2: 2}
|
||||
assert list(i for i in iter_test(ast.GeneratorExp)) == [1, 2]
|
||||
|
||||
+ assert [i for j in [0] for i in iter_test(ast.ListComp)] == [1, 2]
|
||||
+ assert {i for j in [0] for i in iter_test(ast.SetComp)} == {1, 2}
|
||||
+ assert {i: i for j in [0] for i in iter_test(ast.DictComp)} == {1: 1, 2: 2}
|
||||
+ assert list(i for j in [0] for i in iter_test(ast.GeneratorExp)) == [1, 2]
|
||||
+
|
||||
for i in iter_test(ast.For):
|
||||
assert i in (1, 2)
|
||||
|
125
pytest.patch
125
pytest.patch
@@ -1,125 +0,0 @@
|
||||
From 0edb6ca3d733d8cb3fd1ddc7994bde61991ac4c2 Mon Sep 17 00:00:00 2001
|
||||
From: Frank Hoffmann <44680962+15r10nk@users.noreply.github.com>
|
||||
Date: Sun, 12 Jan 2025 17:03:09 +0100
|
||||
Subject: [PATCH] fix: check for pytest compatibility (#94)
|
||||
|
||||
---
|
||||
executing/__init__.py | 5 ++++-
|
||||
executing/_pytest_utils.py | 16 ++++++++++++++++
|
||||
tests/conftest.py | 25 +++++++++++++++++++++++++
|
||||
tests/test_main.py | 5 -----
|
||||
tests/test_pytest.py | 5 +++++
|
||||
5 files changed, 50 insertions(+), 6 deletions(-)
|
||||
create mode 100644 executing/_pytest_utils.py
|
||||
create mode 100644 tests/conftest.py
|
||||
|
||||
diff --git a/executing/__init__.py b/executing/__init__.py
|
||||
index b645197..e5181a5 100644
|
||||
--- a/executing/__init__.py
|
||||
+++ b/executing/__init__.py
|
||||
@@ -10,6 +10,9 @@
|
||||
from collections import namedtuple
|
||||
_VersionInfo = namedtuple('_VersionInfo', ('major', 'minor', 'micro'))
|
||||
from .executing import Source, Executing, only, NotOneValueFound, cache, future_flags
|
||||
+
|
||||
+from ._pytest_utils import is_pytest_compatible
|
||||
+
|
||||
try:
|
||||
from .version import __version__ # type: ignore[import]
|
||||
if "dev" in __version__:
|
||||
@@ -22,4 +25,4 @@
|
||||
__version_info__ = _VersionInfo(*map(int, __version__.split('.')))
|
||||
|
||||
|
||||
-__all__ = ["Source"]
|
||||
+__all__ = ["Source","is_pytest_compatible"]
|
||||
diff --git a/executing/_pytest_utils.py b/executing/_pytest_utils.py
|
||||
new file mode 100644
|
||||
index 0000000..fab8693
|
||||
--- /dev/null
|
||||
+++ b/executing/_pytest_utils.py
|
||||
@@ -0,0 +1,16 @@
|
||||
+import sys
|
||||
+
|
||||
+
|
||||
+
|
||||
+def is_pytest_compatible() -> bool:
|
||||
+ """ returns true if executing can be used for expressions inside assert statements which are rewritten by pytest
|
||||
+ """
|
||||
+ if sys.version_info < (3, 11):
|
||||
+ return False
|
||||
+
|
||||
+ try:
|
||||
+ import pytest
|
||||
+ except ImportError:
|
||||
+ return False
|
||||
+
|
||||
+ return pytest.version_tuple >= (8, 3, 4)
|
||||
diff --git a/tests/conftest.py b/tests/conftest.py
|
||||
new file mode 100644
|
||||
index 0000000..5108348
|
||||
--- /dev/null
|
||||
+++ b/tests/conftest.py
|
||||
@@ -0,0 +1,25 @@
|
||||
+
|
||||
+
|
||||
+from typing import Optional, Sequence, Union
|
||||
+from executing._pytest_utils import is_pytest_compatible
|
||||
+import _pytest.assertion.rewrite as rewrite
|
||||
+import importlib.machinery
|
||||
+import types
|
||||
+
|
||||
+if not is_pytest_compatible():
|
||||
+ original_find_spec = rewrite.AssertionRewritingHook.find_spec
|
||||
+
|
||||
+
|
||||
+ def find_spec(
|
||||
+ self,
|
||||
+ name: str,
|
||||
+ path: Optional[Sequence[Union[str, bytes]]] = None,
|
||||
+ target: Optional[types.ModuleType] = None,
|
||||
+ ) -> Optional[importlib.machinery.ModuleSpec]:
|
||||
+
|
||||
+ if name == "tests.test_main":
|
||||
+ return None
|
||||
+ return original_find_spec(self, name, path, target)
|
||||
+
|
||||
+
|
||||
+ rewrite.AssertionRewritingHook.find_spec = find_spec
|
||||
diff --git a/tests/test_main.py b/tests/test_main.py
|
||||
index a3f92ee..e3bc9d6 100644
|
||||
--- a/tests/test_main.py
|
||||
+++ b/tests/test_main.py
|
||||
@@ -1,9 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
-"""
|
||||
-
|
||||
-assert rewriting will break executing
|
||||
-PYTEST_DONT_REWRITE
|
||||
-"""
|
||||
from __future__ import print_function, division
|
||||
import ast
|
||||
import contextlib
|
||||
diff --git a/tests/test_pytest.py b/tests/test_pytest.py
|
||||
index 281598d..5cbe0a2 100644
|
||||
--- a/tests/test_pytest.py
|
||||
+++ b/tests/test_pytest.py
|
||||
@@ -6,6 +6,7 @@
|
||||
from time import sleep
|
||||
|
||||
import asttokens
|
||||
+from executing._pytest_utils import is_pytest_compatible
|
||||
import pytest
|
||||
from littleutils import SimpleNamespace
|
||||
|
||||
@@ -124,6 +125,10 @@ def check_manual_linecache(filename):
|
||||
def test_exception_catching():
|
||||
frame = inspect.currentframe()
|
||||
|
||||
+ if is_pytest_compatible():
|
||||
+ assert isinstance(Source.executing(frame).node,ast.Call)
|
||||
+ return
|
||||
+
|
||||
executing.executing.TESTING = True # this is already the case in all other tests
|
||||
# Sanity check that this operation usually raises an exception.
|
||||
# This actually depends on executing not working in the presence of pytest.
|
@@ -1,116 +0,0 @@
|
||||
From 0b913873b9db8c157a8dd581e8771242bcb8864b Mon Sep 17 00:00:00 2001
|
||||
From: Frank Hoffmann <15r10nk-git@polarbit.de>
|
||||
Date: Fri, 16 Aug 2024 22:51:32 +0200
|
||||
Subject: [PATCH 1/4] fix: backward compatibility fix for changed source
|
||||
positions in 3.12.5 (#82)
|
||||
|
||||
---
|
||||
executing/_position_node_finder.py | 27 +++++++++++++++++++++++++++
|
||||
1 file changed, 27 insertions(+)
|
||||
|
||||
Index: executing-2.0.1/executing/_position_node_finder.py
|
||||
===================================================================
|
||||
--- executing-2.0.1.orig/executing/_position_node_finder.py
|
||||
+++ executing-2.0.1/executing/_position_node_finder.py
|
||||
@@ -156,6 +156,8 @@ class PositionNodeFinder(object):
|
||||
typ=typ,
|
||||
)
|
||||
|
||||
+ self.result = self.fix_result(self.result, self.instruction(lasti))
|
||||
+
|
||||
self.known_issues(self.result, self.instruction(lasti))
|
||||
|
||||
self.test_for_decorator(self.result, lasti)
|
||||
@@ -213,6 +215,32 @@ class PositionNodeFinder(object):
|
||||
if sys.version_info < (3, 12):
|
||||
index += 4
|
||||
|
||||
+ def fix_result(
|
||||
+ self, node: EnhancedAST, instruction: dis.Instruction
|
||||
+ ) -> EnhancedAST:
|
||||
+ if (
|
||||
+ sys.version_info >= (3, 12, 5)
|
||||
+ and instruction.opname in ("GET_ITER", "FOR_ITER")
|
||||
+ and isinstance(node.parent, ast.For)
|
||||
+ and node is node.parent.iter
|
||||
+ ):
|
||||
+ # node positions have changed in 3.12.5
|
||||
+ # https://github.com/python/cpython/issues/93691
|
||||
+ # `for` calls __iter__ and __next__ during execution, the calling
|
||||
+ # expression of these calls was the ast.For node since cpython 3.11 (see test_iter).
|
||||
+ # cpython 3.12.5 changed this to the `iter` node of the loop, to make tracebacks easier to read.
|
||||
+ # This keeps backward compatibility with older executing versions.
|
||||
+
|
||||
+ # there are also cases like:
|
||||
+ #
|
||||
+ # for a in iter(l): pass
|
||||
+ #
|
||||
+ # where `iter(l)` would be otherwise the resulting node for the `iter()` call and the __iter__ call of the for implementation.
|
||||
+ # keeping the old behaviour makes it possible to distinguish both cases.
|
||||
+
|
||||
+ return node.parent
|
||||
+ return node
|
||||
+
|
||||
def known_issues(self, node: EnhancedAST, instruction: dis.Instruction) -> None:
|
||||
if instruction.opname in ("COMPARE_OP", "IS_OP", "CONTAINS_OP") and isinstance(
|
||||
node, types_cmp_issue
|
||||
Index: executing-2.0.1/.github/workflows/test.yml
|
||||
===================================================================
|
||||
--- executing-2.0.1.orig/.github/workflows/test.yml
|
||||
+++ executing-2.0.1/.github/workflows/test.yml
|
||||
@@ -12,7 +12,7 @@ jobs:
|
||||
runs-on: ubuntu-20.04
|
||||
strategy:
|
||||
matrix:
|
||||
- python-version: [3.5, 3.6, 3.7, 3.8, 3.9, '3.10', 3.11, 3.12-dev, pypy-3.6]
|
||||
+ python-version: [3.8, 3.9, '3.10', 3.11, 3.12-dev]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
Index: executing-2.0.1/setup.cfg
|
||||
===================================================================
|
||||
--- executing-2.0.1.orig/setup.cfg
|
||||
+++ executing-2.0.1/setup.cfg
|
||||
@@ -25,7 +25,7 @@ packages = executing
|
||||
zip_safe = False
|
||||
include_package_data = True
|
||||
setup_requires = setuptools; setuptools_scm[toml]
|
||||
-python_requires = >=3.5
|
||||
+python_requires = >=3.8
|
||||
|
||||
[options.extras_require]
|
||||
tests =
|
||||
Index: executing-2.0.1/tox.ini
|
||||
===================================================================
|
||||
--- executing-2.0.1.orig/tox.ini
|
||||
+++ executing-2.0.1/tox.ini
|
||||
@@ -1,5 +1,5 @@
|
||||
[tox]
|
||||
-envlist = py35,py36,py37,py38,py39,py310,py311,py312,pypy35,pypy36
|
||||
+envlist = py38,py39,py310,py311,py312,pypy35,pypy36
|
||||
|
||||
[testenv]
|
||||
commands =
|
||||
@@ -10,7 +10,7 @@ passenv =
|
||||
ADD_EXECUTING_TESTS
|
||||
EXECUTING_SLOW_TESTS
|
||||
|
||||
-[testenv:generate_small_sample-py{35,36,37,38,39,310,311}]
|
||||
+[testenv:generate_small_sample-py{38,39,310,311,312}]
|
||||
extras = tests
|
||||
deps = pysource-minimize
|
||||
commands =
|
||||
Index: executing-2.0.1/tests/test_main.py
|
||||
===================================================================
|
||||
--- executing-2.0.1.orig/tests/test_main.py
|
||||
+++ executing-2.0.1/tests/test_main.py
|
||||
@@ -802,6 +802,9 @@ class TestFiles:
|
||||
or 'pyparsing.py' in filename
|
||||
or 'enum' in filename
|
||||
)
|
||||
+ or sys.version_info < (3,11) and (
|
||||
+ 'python.py' in filename
|
||||
+ )
|
||||
):
|
||||
continue
|
||||
|
Reference in New Issue
Block a user