Skip to content

Commit

Permalink
cache code objects from parsed .py files
Browse files Browse the repository at this point in the history
  • Loading branch information
trehn committed Aug 22, 2017
1 parent 2d985e9 commit 0552fb1
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions bundlewrap/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from ..exceptions import DontCache, FaultUnavailable

__GETATTR_CACHE = {}
__GETATTR_CODE_CACHE = {}
__GETATTR_RESULT_CACHE = {}
__GETATTR_NODEFAULT = "very_unlikely_default_value"


Expand Down Expand Up @@ -151,39 +152,44 @@ def get_file_contents(path):
return content


def get_all_attrs_from_file(path, cache=True, base_env=None):
def get_all_attrs_from_file(path, base_env=None):
"""
Reads all 'attributes' (if it were a module) from a source file.
"""
if base_env is None:
base_env = {}
if base_env:

if not base_env and path in __GETATTR_RESULT_CACHE:
# do not allow caching when passing in a base env because that
# breaks repeated calls with different base envs for the same
# file
cache = False
if path not in __GETATTR_CACHE or not cache:
return __GETATTR_RESULT_CACHE[path]

if path not in __GETATTR_CODE_CACHE:
source = get_file_contents(path)
env = base_env.copy()
try:
exec(source, env)
except:
from .ui import io
io.stderr("Exception while executing {}".format(path))
raise
if cache:
__GETATTR_CACHE[path] = env
else:
env = __GETATTR_CACHE[path]
__GETATTR_CODE_CACHE[path] = compile(source, path, mode='exec')

code = __GETATTR_CODE_CACHE[path]
env = base_env.copy()
try:
exec(code, env)
except:
from .ui import io
io.stderr("Exception while executing {}".format(path))
raise

if not base_env:
__GETATTR_RESULT_CACHE[path] = env

return env


def getattr_from_file(path, attrname, base_env=None, cache=True, default=__GETATTR_NODEFAULT):
def getattr_from_file(path, attrname, base_env=None, default=__GETATTR_NODEFAULT):
"""
Reads a specific 'attribute' (if it were a module) from a source
file.
"""
env = get_all_attrs_from_file(path, base_env=base_env, cache=cache)
env = get_all_attrs_from_file(path, base_env=base_env)
if default == __GETATTR_NODEFAULT:
return env[attrname]
else:
Expand Down

0 comments on commit 0552fb1

Please sign in to comment.