From 7119fe0f9674ff382e2ad799b6d3a61de68f9f3d Mon Sep 17 00:00:00 2001 From: Kelvin Chan Date: Mon, 26 Aug 2024 12:14:00 -0400 Subject: [PATCH 1/3] Use getframe instead of stack --- depocs/__init__.py | 15 ++++++++++----- pyproject.toml | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/depocs/__init__.py b/depocs/__init__.py index 8add29d..b202cb6 100644 --- a/depocs/__init__.py +++ b/depocs/__init__.py @@ -11,6 +11,7 @@ """ import inspect +import sys import threading @@ -196,7 +197,7 @@ class will have its own stack and will be scoped independent of any _Scoped__is_open = False _Scoped__is_used = False - _Scoped__open_site = None + _Scoped__open_site_frame = None def open(self, call_site_level=1): """ @@ -238,9 +239,13 @@ def open(self, call_site_level=1): self._Scoped__is_open = True self._Scoped__is_used = True - stack = inspect.stack() - if len(stack) > call_site_level: - self._Scoped__open_site = stack[call_site_level] + try: + frame = sys._getframe(call_site_level - 1) + except ValueError: + # No frame found, skip + pass + else: + self._Scoped__open_site_frame = frame return self @@ -287,7 +292,7 @@ def is_open(self): @property def open_site(self): - return self._Scoped__open_site + return inspect.getframeinfo(self._Scoped__open_site_frame) @property def is_used(self): diff --git a/pyproject.toml b/pyproject.toml index d88cdd7..90084bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "depocs" -version = "2.0.0" +version = "2.0.0.dev1" homepage = "https://github.com/sdelements/depocs" description = "Scoped thread-local mixin class" authors = ["Security Compass "] From f42a9accafb9379db472dd041b42191af4a8ff75 Mon Sep 17 00:00:00 2001 From: Kelvin Chan Date: Mon, 26 Aug 2024 12:42:55 -0400 Subject: [PATCH 2/3] Log the same frame as before --- depocs/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/depocs/__init__.py b/depocs/__init__.py index b202cb6..1585b7b 100644 --- a/depocs/__init__.py +++ b/depocs/__init__.py @@ -240,7 +240,7 @@ def open(self, call_site_level=1): self._Scoped__is_used = True try: - frame = sys._getframe(call_site_level - 1) + frame = sys._getframe(call_site_level) except ValueError: # No frame found, skip pass @@ -291,7 +291,7 @@ def is_open(self): return self._Scoped__is_open @property - def open_site(self): + def open_site(self) -> inspect.Traceback: return inspect.getframeinfo(self._Scoped__open_site_frame) @property @@ -355,7 +355,7 @@ def clear(cls): def format_trace_entry(self): if self.open_site: return "{0}({1}) opened at {2}:{3}\n".format( - self.__class__.__name__, id(self), self.open_site[1], self.open_site[2] + self.__class__.__name__, id(self), self.open_site[0], self.open_site[1] ) else: return "{0}({1}) opened somewhere\n".format( From b654747c2fc461c33b4e196f07dab06833cd9089 Mon Sep 17 00:00:00 2001 From: Kelvin Chan Date: Tue, 27 Aug 2024 14:54:03 -0400 Subject: [PATCH 3/3] CR comment and update version --- depocs/__init__.py | 4 +--- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/depocs/__init__.py b/depocs/__init__.py index 1585b7b..a19943d 100644 --- a/depocs/__init__.py +++ b/depocs/__init__.py @@ -240,12 +240,10 @@ def open(self, call_site_level=1): self._Scoped__is_used = True try: - frame = sys._getframe(call_site_level) + self._Scoped__open_site_frame = sys._getframe(call_site_level) except ValueError: # No frame found, skip pass - else: - self._Scoped__open_site_frame = frame return self diff --git a/pyproject.toml b/pyproject.toml index 90084bd..24ee998 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "depocs" -version = "2.0.0.dev1" +version = "2.1.0" homepage = "https://github.com/sdelements/depocs" description = "Scoped thread-local mixin class" authors = ["Security Compass "]