This commit is contained in:
2023-04-19 20:24:03 -04:00
parent 451ee7a557
commit 9933b0b880
4 changed files with 41 additions and 8 deletions
+10 -8
View File
@@ -9,7 +9,7 @@ from sys import getsizeof
def get_bytecode_size(module_name):
"""Get the size of the cached .pyc bytecode file for a module."""
spec = importlib.util.find_spec(module_name)
if spec and spec.origin.endswith('.py'):
if spec and spec.origin and spec.origin.endswith('.py'):
pyc_file = importlib.util.cache_from_source(spec.origin)
if os.path.exists(pyc_file):
return os.path.getsize(pyc_file)
@@ -19,7 +19,7 @@ def get_bytecode_size(module_name):
def get_dso_size(module_name):
"""Get the size of the DSO (Dynamic Shared Object) module file for a module."""
spec = importlib.util.find_spec(module_name)
if spec and spec.origin.endswith(('.so', '.dll')):
if spec and spec.origin and spec.origin.endswith(('.so', '.dll')):
return os.path.getsize(spec.origin)
return 0
@@ -51,7 +51,7 @@ def generate_module_report():
# Generate histogram of module directory names
dir_histogram = defaultdict(int)
for module_name in sys.modules:
module_file = sys.modules[module_name].__file__
module_file = getattr(sys.modules[module_name], '__file__', None)
if module_file:
module_dir = os.path.dirname(module_file)
dir_histogram[module_dir] += 1
@@ -64,9 +64,11 @@ def generate_module_report():
if __name__ == "__main__":
# Set __spec__ attribute for the main module
if not hasattr(sys.modules['__main__'], '__spec__'):
sys.modules['__main__'].__spec__ = importlib.util.spec_from_file_location('__main__', __file__)
__spec__ = importlib.util.spec_from_file_location('__main__', __file__)
report = generate_module_report()
print(report)
report = generate_module_report()
print(report)