Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source code serializers that skip dill entirely #1815

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog.d/20250227_143258_chris_pure_source_serde.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
New Functionality
^^^^^^^^^^^^^^^^^

- Added two new serialization strategies,
:class:`~globus_compute_sdk.serialize.PureSourceTextInspect` and
:class:`~globus_compute_sdk.serialize.PureSourceDill`, which serialize functions as
raw source code strings (avoiding ``dill`` bytecode serialization entirely).
4 changes: 4 additions & 0 deletions compute_sdk/globus_compute_sdk/serialize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
DillCodeTextInspect,
DillDataBase64,
JSONData,
PureSourceDill,
PureSourceTextInspect,
)
from globus_compute_sdk.serialize.facade import AllowlistWildcard, ComputeSerializer

Expand All @@ -22,6 +24,8 @@
"DillCode",
"DillCodeSource",
"DillCodeTextInspect",
"PureSourceDill",
"PureSourceTextInspect",
"DillDataBase64",
"JSONData",
]
80 changes: 80 additions & 0 deletions compute_sdk/globus_compute_sdk/serialize/concretes.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,86 @@ def deserialize(self, payload: str, variation: int = 0) -> t.Any:
raise DeserializationError(f"Deserialization failed after {count} tries")


class PureSourceTextInspect(SerializationStrategy):
"""
Extracts a function's definition via :func:`inspect.getsource` and sends the
resulting string directly over the wire.

.. list-table:: Supports
:header-rows: 1
:align: center

* - Functions defined in Python interpreter
- Code from notebooks
- Interop between mismatched Python versions
- Decorated functions
* - ❌
- ✅
- ⚠️
- ❌
"""

identifier = "st\n"
for_code = True

_separator = ":"

def serialize(self, data) -> str:
":meta private:"
name = data.__name__
body = inspect.getsource(data)
x = name + self._separator + body
return self.identifier + x

def deserialize(self, payload: str):
":meta private:"
chomped = self.chomp(payload)
name, body = chomped.split(self._separator, 1)
exec_ns: dict = {}
exec(body, exec_ns)
return exec_ns[name]


class PureSourceDill(SerializationStrategy):
"""
Extracts a function's definition via |dill.getsource|_ and sends the resulting
string directly over the wire.

.. list-table:: Supports
:header-rows: 1
:align: center

* - Functions defined in Python interpreter
- Code from notebooks
- Interop between mismatched Python versions
- Decorated functions
* - ✅
- ❌
- ⚠️
- ❌
"""

identifier = "sd\n"
for_code = True

_separator = ":"

def serialize(self, data) -> str:
":meta private:"
name = data.__name__
body = dill.source.getsource(data, lstrip=True)
x = name + self._separator + body
return self.identifier + x

def deserialize(self, payload: str):
":meta private:"
chomped = self.chomp(payload)
name, body = chomped.split(self._separator, 1)
exec_ns: dict = {}
exec(body, exec_ns)
return exec_ns[name]


SELECTABLE_STRATEGIES = [
t.__class__
for t in SerializationStrategy._CACHE.values()
Expand Down
Loading