-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By enabling register_pickle_by_value=True, the function is serialized by value instead of being referenced by its module path. This embeds the function unpickled even if the original module is unavailable on the remote computer. Note: If the function contains import statements, the imported modules must still be installed on the remote computer.
- Loading branch information
1 parent
839e3ba
commit 3b0f48f
Showing
5 changed files
with
102 additions
and
33 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
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
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
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
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 |
---|---|---|
@@ -1,17 +1,46 @@ | ||
import pytest | ||
from aiida_pythonjob.utils import build_function_data | ||
|
||
|
||
def test_build_function_data(): | ||
from math import sqrt | ||
|
||
function_data = build_function_data(sqrt) | ||
assert function_data == { | ||
"name": "sqrt", | ||
"mode": "use_module_path", | ||
"source_code": "from math import sqrt", | ||
} | ||
# | ||
try: | ||
function_data = build_function_data(1) | ||
except Exception as e: | ||
assert str(e) == "Provided object is not a callable function or class." | ||
"""Test the build_function_data function behavior.""" | ||
|
||
with pytest.raises(TypeError, match="Provided object is not a callable function or class."): | ||
build_function_data(1) | ||
|
||
function_data = build_function_data(build_function_data) | ||
assert function_data["name"] == "build_function_data" | ||
assert "source_code" in function_data | ||
assert "pickled_function" in function_data | ||
node = function_data["pickled_function"] | ||
with node.base.repository.open(node.FILENAME, mode="rb") as f: | ||
text = f.read() | ||
assert b"cloudpickle" not in text | ||
|
||
function_data = build_function_data(build_function_data, register_pickle_by_value=True) | ||
assert function_data["name"] == "build_function_data" | ||
assert "source_code" in function_data | ||
assert "pickled_function" in function_data | ||
node = function_data["pickled_function"] | ||
with node.base.repository.open(node.FILENAME, mode="rb") as f: | ||
text = f.read() | ||
assert b"cloudpickle" in text | ||
|
||
def local_function(x, y): | ||
return x + y | ||
|
||
function_data = build_function_data(local_function) | ||
assert function_data["name"] == "local_function" | ||
assert "source_code" in function_data | ||
assert function_data["mode"] == "use_pickled_function" | ||
|
||
def outer_function(): | ||
def nested_function(x, y): | ||
return x + y | ||
|
||
return nested_function | ||
|
||
nested_func = outer_function() | ||
function_data = build_function_data(nested_func) | ||
assert function_data["name"] == "nested_function" | ||
assert function_data["mode"] == "use_pickled_function" |