From cb707eadc3c352202f245284b502e6a61cb372e2 Mon Sep 17 00:00:00 2001 From: Prashant Sengar Date: Thu, 6 Apr 2023 16:27:19 +0530 Subject: [PATCH 1/3] Fix: __builtins__ is not a dict __builtins__ is not a dict. Hence getting and setting attributes using getattr and setattr respectively --- import_profiler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/import_profiler.py b/import_profiler.py index 39b4819..0547b79 100644 --- a/import_profiler.py +++ b/import_profiler.py @@ -56,14 +56,14 @@ def compute_intime(parent, full_stack, elapsed, ordered_visited, visited, depth= class ImportProfilerContext(object): def __init__(self): - self._original_importer = __builtins__["__import__"] + self._original_importer = getattr(__builtins__, "__import__") self._import_stack = ImportStack() def enable(self): - __builtins__["__import__"] = self._profiled_import + setattr(__builtins__, "__import__", self._profiled_import) def disable(self): - __builtins__["__import__"] = self._original_importer + setattr(__builtins__, "__import__", self._original_importer) def print_info(self, threshold=1.): """ Print profiler results. From 0b42174bf2e946049e449df02f30efb2a2f0b5e8 Mon Sep 17 00:00:00 2001 From: Prashant Sengar Date: Thu, 6 Apr 2023 16:29:10 +0530 Subject: [PATCH 2/3] Initialize imports at level=0 In Python 3, min level of imports is 0. It fails at -1 with error: ``` ValueError: level must be >= 0 ``` __builtins__ is not a dict. Hence getting and setting attributes using getattr and setattr respectively --- import_profiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/import_profiler.py b/import_profiler.py index 0547b79..3f869cd 100644 --- a/import_profiler.py +++ b/import_profiler.py @@ -116,7 +116,7 @@ def __exit__(self, *a, **kw): self.disable() def _profiled_import(self, name, globals=None, locals=None, fromlist=None, - level=-1, *a, **kw): + level=0, *a, **kw): if globals is None: context_name = None else: From b12f65195acbca6df4871ae6e244543a87433bf0 Mon Sep 17 00:00:00 2001 From: prashantsengar-cl <92354548+prashantsengar-cl@users.noreply.github.com> Date: Fri, 18 Oct 2024 18:26:34 +0530 Subject: [PATCH 3/3] Update setup.py Can be installed as a package named "import_profiler3" --- setup.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 6796949..f1b6bb9 100644 --- a/setup.py +++ b/setup.py @@ -7,13 +7,13 @@ if __name__ == "__main__": setup( - name="import_profiler", - version="0.0.4.dev0", - author="David Cournapeau", + name="import_profiler3", + version="0.0.1", + author="David Cournapeau, Prashant Sengar", author_email="cournape@gmail.com", license="MIT", install_requires=["attrs >= 17.1.0", "tabulate >= 0.7.5"], - description="Import profiler to find bottlenecks in import times.", + description="Import profiler to find bottlenecks in import times - supports Python 3.", long_description=DESCRIPTION, py_modules=["import_profiler"], )