diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2025-02-06 09:44:56 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2025-02-06 10:00:51 +0300 |
commit | a6c9550f60a1663b0dfa661f909c3f78d9435be5 (patch) | |
tree | 4eb505d8cfd4eec3162795035b46cfa32e5331fb /contrib/python | |
parent | ed56e2e58e962572b47d0e7c9abca6a751178168 (diff) | |
download | ydb-a6c9550f60a1663b0dfa661f909c3f78d9435be5.tar.gz |
Intermediate changes
commit_hash:b431fc94a39861d987569e88bb97ba7e6187f7e5
Diffstat (limited to 'contrib/python')
-rw-r--r-- | contrib/python/executing/.dist-info/METADATA | 4 | ||||
-rw-r--r-- | contrib/python/executing/README.md | 2 | ||||
-rw-r--r-- | contrib/python/executing/executing/__init__.py | 5 | ||||
-rw-r--r-- | contrib/python/executing/executing/_position_node_finder.py | 65 | ||||
-rw-r--r-- | contrib/python/executing/executing/_pytest_utils.py | 16 | ||||
-rw-r--r-- | contrib/python/executing/executing/version.py | 2 | ||||
-rw-r--r-- | contrib/python/executing/ya.make | 3 |
7 files changed, 91 insertions, 6 deletions
diff --git a/contrib/python/executing/.dist-info/METADATA b/contrib/python/executing/.dist-info/METADATA index 45ff9aa881..6e4ac6516d 100644 --- a/contrib/python/executing/.dist-info/METADATA +++ b/contrib/python/executing/.dist-info/METADATA @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: executing -Version: 2.1.0 +Version: 2.2.0 Summary: Get the currently executing AST node of a frame, and other information Home-page: https://github.com/alexmojaki/executing Author: Alex Hall @@ -42,7 +42,7 @@ This mini-package lets you get information about what a frame is currently doing * [How does it work?](#how-does-it-work) * [Is it reliable?](#is-it-reliable) * [Which nodes can it identify?](#which-nodes-can-it-identify) -* [Libraries that use this](#libraries-that-use-this) +* [Projects that use this](#projects-that-use-this) ## Usage diff --git a/contrib/python/executing/README.md b/contrib/python/executing/README.md index 1b4dbd8f07..61943dd7f5 100644 --- a/contrib/python/executing/README.md +++ b/contrib/python/executing/README.md @@ -13,7 +13,7 @@ This mini-package lets you get information about what a frame is currently doing * [How does it work?](#how-does-it-work) * [Is it reliable?](#is-it-reliable) * [Which nodes can it identify?](#which-nodes-can-it-identify) -* [Libraries that use this](#libraries-that-use-this) +* [Projects that use this](#projects-that-use-this) ## Usage diff --git a/contrib/python/executing/executing/__init__.py b/contrib/python/executing/executing/__init__.py index b645197391..e5181a5c32 100644 --- a/contrib/python/executing/executing/__init__.py +++ b/contrib/python/executing/executing/__init__.py @@ -10,6 +10,9 @@ Get information about what a frame is currently doing. Typical usage: 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 @@ else: __version_info__ = _VersionInfo(*map(int, __version__.split('.'))) -__all__ = ["Source"] +__all__ = ["Source","is_pytest_compatible"] diff --git a/contrib/python/executing/executing/_position_node_finder.py b/contrib/python/executing/executing/_position_node_finder.py index 7a814150da..0f8344106f 100644 --- a/contrib/python/executing/executing/_position_node_finder.py +++ b/contrib/python/executing/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: diff --git a/contrib/python/executing/executing/_pytest_utils.py b/contrib/python/executing/executing/_pytest_utils.py new file mode 100644 index 0000000000..fab8693baf --- /dev/null +++ b/contrib/python/executing/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/contrib/python/executing/executing/version.py b/contrib/python/executing/executing/version.py index b15121b0fe..e212710ff7 100644 --- a/contrib/python/executing/executing/version.py +++ b/contrib/python/executing/executing/version.py @@ -1 +1 @@ -__version__ = '2.1.0'
\ No newline at end of file +__version__ = '2.2.0'
\ No newline at end of file diff --git a/contrib/python/executing/ya.make b/contrib/python/executing/ya.make index b437b26981..32d111cc47 100644 --- a/contrib/python/executing/ya.make +++ b/contrib/python/executing/ya.make @@ -2,7 +2,7 @@ PY3_LIBRARY() -VERSION(2.1.0) +VERSION(2.2.0) LICENSE(MIT) @@ -13,6 +13,7 @@ PY_SRCS( executing/__init__.py executing/_exceptions.py executing/_position_node_finder.py + executing/_pytest_utils.py executing/executing.py executing/version.py ) |