Skip to content

Commit

Permalink
refactor function cloning into utility and update history
Browse files Browse the repository at this point in the history
  • Loading branch information
smacke committed Jul 12, 2024
1 parent 5faaece commit f5f602b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
5 changes: 5 additions & 0 deletions docs/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
History
=======

0.0.56 (2024-07-12)
-------------------
* Support exception type instrumentation;
* Add support for bytecode caching;

0.0.55 (2024-06-24)
-------------------
* Remove bare except;
Expand Down
22 changes: 4 additions & 18 deletions pyccolo/import_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,19 @@
from importlib.machinery import SourceFileLoader
from importlib.util import decode_source, find_spec, spec_from_loader
from types import CodeType, ModuleType
from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, List, Optional, Tuple
from typing import TYPE_CHECKING, Callable, Dict, Generator, List, Optional, Tuple

from pyccolo.trace_events import TraceEvent
from pyccolo.utils import clone_function

if TYPE_CHECKING:
from pyccolo.tracer import BaseTracer

logger = logging.getLogger(__name__)


_local_env: Dict[str, Any] = {}
exec(
"def cache_from_source(*args, **kwargs): pass",
importlib.util.cache_from_source.__globals__,
_local_env,
)
orig_cache_from_source = _local_env["cache_from_source"]
orig_cache_from_source.__code__ = importlib.util.cache_from_source.__code__
_local_env.clear()
exec(
"def source_from_cache(*args, **kwargs): pass",
importlib.util.source_from_cache.__globals__,
_local_env,
)
orig_source_from_cache = _local_env["source_from_cache"]
orig_source_from_cache.__code__ = importlib.util.source_from_cache.__code__
del _local_env
orig_cache_from_source = clone_function(importlib.util.cache_from_source) # type: ignore
orig_source_from_cache = clone_function(importlib.util.source_from_cache) # type: ignore


_pyccolo_loader: "TraceLoader" = None # type: ignore
Expand Down
20 changes: 17 additions & 3 deletions pyccolo/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
import importlib
from contextlib import ExitStack, contextmanager
from typing import Type
from types import FunctionType
from typing import TYPE_CHECKING, Any, Dict, Type

from pyccolo.tracer import BaseTracer
if TYPE_CHECKING:
from pyccolo.tracer import BaseTracer


def resolve_tracer(ref: str) -> Type[BaseTracer]:
def resolve_tracer(ref: str) -> Type["BaseTracer"]:
module, attr = ref.rsplit(".", 1)
return getattr(importlib.import_module(module), attr)

Expand All @@ -15,3 +17,15 @@ def resolve_tracer(ref: str) -> Type[BaseTracer]:
def multi_context(cms):
with ExitStack() as stack:
yield [stack.enter_context(mgr) for mgr in cms]


def clone_function(func: FunctionType) -> FunctionType:
local_env: Dict[str, Any] = {}
exec(
f"def {func.__name__}(*args, **kwargs): pass",
func.__globals__,
local_env,
)
cloned_func = local_env[func.__name__]
cloned_func.__code__ = func.__code__
return cloned_func

0 comments on commit f5f602b

Please sign in to comment.