Skip to content

Commit

Permalink
Use getframe instead of stack (#15)
Browse files Browse the repository at this point in the history
* Avoid inspect.stack to reduce performance impact
* Bump version to 2.1.0
  • Loading branch information
RouganStriker authored Aug 27, 2024
1 parent 6c86d8b commit 02a8102
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions depocs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import inspect
import sys
import threading


Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -238,9 +239,11 @@ 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:
self._Scoped__open_site_frame = sys._getframe(call_site_level)
except ValueError:
# No frame found, skip
pass

return self

Expand Down Expand Up @@ -286,8 +289,8 @@ def is_open(self):
return self._Scoped__is_open

@property
def open_site(self):
return self._Scoped__open_site
def open_site(self) -> inspect.Traceback:
return inspect.getframeinfo(self._Scoped__open_site_frame)

@property
def is_used(self):
Expand Down Expand Up @@ -350,7 +353,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(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "depocs"
version = "2.0.0"
version = "2.1.0"
homepage = "https://github.com/sdelements/depocs"
description = "Scoped thread-local mixin class"
authors = ["Security Compass <[email protected]>"]
Expand Down

0 comments on commit 02a8102

Please sign in to comment.