Skip to content

Commit 92aa017

Browse files
committed
Add standalone serialization function
This commit adds a standalone serialization function to the symengine wrapper module. Previously there was a load_basic() function that could be used for directly deserializing the payload without going through pickle, but no corresponding function for serialization. A new function save_basic() is added here to perform this function which will enable users to directly roundtrip symengine objects through binary serialization. Fixes symengine#449
1 parent 8d5d63c commit 92aa017

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

symengine/lib/symengine_wrapper.in.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,9 @@ cdef list vec_pair_to_list(symengine.vec_pair& vec):
833833
def load_basic(bytes s):
834834
return c2py(symengine.wrapper_loads(s))
835835

836+
def save_basic(basic):
837+
return symengine.save_basic(basic)
838+
836839

837840
repr_latex=[False]
838841

symengine/tests/test_pickling.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from symengine import symbols, sin, sinh, have_numpy, have_llvm, cos, Symbol
2+
from symengine.lib.symengine_wrapper import load_basic, save_basic
23
from symengine.test_utilities import raises
34
import pickle
45
import unittest
@@ -11,6 +12,12 @@ def test_basic():
1112
expr2 = pickle.loads(s)
1213
assert expr == expr2
1314

15+
def test_basic_direct():
16+
x, y, z = symbols('x y z')
17+
expr = sin(cos(x + y)/z)**2
18+
s = save_basic(expr)
19+
expr2 = load_basic(s)
20+
assert expr == expr2
1421

1522
class MySymbolBase(Symbol):
1623
def __init__(self, name, attr):

0 commit comments

Comments
 (0)