From f2b509f9fa4af2bda050a1d0e5c9504b2c02c208 Mon Sep 17 00:00:00 2001 From: Espy | Neko | 404 <34942042+neko404notfound@users.noreply.github.com> Date: Wed, 23 May 2018 18:28:24 +0100 Subject: [PATCH] Fixed potential case of GC crippling Cite: https://docs.python.org/3/library/inspect.html#the-interpreter-stack --- src/shared_libs/ioutils.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/shared_libs/ioutils.py b/src/shared_libs/ioutils.py index 284f156..7609aa7 100644 --- a/src/shared_libs/ioutils.py +++ b/src/shared_libs/ioutils.py @@ -3,6 +3,7 @@ """ IO stuff. """ +import copy import inspect import os @@ -42,13 +43,20 @@ def in_here(first_path_bit: str, *path_bits: str, stack_depth: int=0) -> str: raise RuntimeError('Could not find a stack record. Interpreter has ' 'been shot.') else: - module = inspect.getmodule(frame[0]) + module = inspect.getmodule(frame[0]) assert hasattr(module, '__file__'), 'No `__file__\' attr, welp.' - + + # https://docs.python.org/3/library/inspect.html#the-interpreter-stack + # If Python caches strings rather than copying when we move them + # around or modify them, then this may cause a referential cycle which + # will consume more memory and stop the garbage collection system + # from working correctly. Best thing to do here is deepcopy anything + # we need and prevent this occuring. Del the references to allow them + # to be freed. file = module.__file__ - + file = copy.deepcopy(file) + del module, frame dir_name = os.path.dirname(file) abs_dir_name = os.path.abspath(dir_name) - pathish = os.path.join(abs_dir_name, first_path_bit, *path_bits) return pathish