-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
import logging | ||
import sys | ||
from types import ModuleType | ||
from typing import Callable, Set, Tuple | ||
|
||
from ipyflow.patches.cloudpickle_patch import patch_cloudpickle_function_reduce | ||
from ipyflow.patches.pyspark_patch import patch_pyspark_udf | ||
from ipyflow.singletons import flow | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
_predicate_patch_pairs: Tuple[ | ||
Tuple[Callable[[str], bool], Callable[[ModuleType], None]], ... | ||
] = ( | ||
( | ||
lambda modname: modname.endswith("cloudpickle.cloudpickle_fast"), | ||
patch_cloudpickle_function_reduce, | ||
), | ||
(lambda modname: modname == "pyspark.sql.udf", patch_pyspark_udf), | ||
) | ||
|
||
_patched_modules: Set[str] = set() | ||
|
||
|
||
def apply_patches(modname: str, module: ModuleType) -> None: | ||
if modname in _patched_modules: | ||
return | ||
flow_ = flow() | ||
for predicate, patch in _predicate_patch_pairs: | ||
try: | ||
if predicate(modname): | ||
patch(module) | ||
_patched_modules.add(modname) | ||
except Exception: # noqa | ||
if flow_.is_dev_mode: | ||
logger.exception("Failed to apply patch to module %s", modname) | ||
|
||
|
||
def patch_all() -> None: | ||
for modname, module in list(sys.modules.items()): | ||
apply_patches(modname, module) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
from types import FunctionType, LambdaType, ModuleType | ||
from typing import TYPE_CHECKING, Type, Union | ||
|
||
from ipyflow.tracing.uninstrument import uninstrument | ||
|
||
if TYPE_CHECKING: | ||
from cloudpickle.cloudpickle_fast import CloudPickler | ||
|
||
|
||
def patch_cloudpickle_function_reduce(module: ModuleType) -> None: | ||
pickler_cls: Type["CloudPickler"] = module.CloudPickler | ||
_function_reduce = pickler_cls._function_reduce | ||
|
||
def _patched_function_reduce( | ||
self_: "CloudPickler", obj: Union[FunctionType, LambdaType] | ||
) -> None: | ||
uninstrumented = uninstrument(obj) | ||
return _function_reduce( | ||
self_, obj if uninstrumented is None else uninstrumented | ||
) | ||
|
||
pickler_cls._function_reduce = _patched_function_reduce |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
from types import ModuleType | ||
from typing import TYPE_CHECKING, Type | ||
|
||
from ipyflow.tracing.uninstrument import uninstrument | ||
|
||
if TYPE_CHECKING: | ||
from pyspark.sql.udf import UserDefinedFunction | ||
|
||
|
||
def patch_pyspark_udf(module: ModuleType) -> None: | ||
udf_cls: Type["UserDefinedFunction"] = module.UserDefinedFunction | ||
udf_cls_init = udf_cls.__init__ | ||
|
||
def _patched_init(self_: "UserDefinedFunction", func, *args, **kwargs) -> None: | ||
uninstrumented = uninstrument(func) | ||
return udf_cls_init( | ||
self_, func if uninstrumented is None else uninstrumented, *args, **kwargs | ||
) | ||
|
||
udf_cls.__init__ = _patched_init |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters