Skip to content

Commit

Permalink
Squashed 'wrap/' changes from d37b8a972..10e1efd6f
Browse files Browse the repository at this point in the history
10e1efd6f Merge pull request #32 from ayushbaid/feature/pickle
55d5d7fbe ignoring pickle in matlab wrap
8d70c7fe2 adding newlines
dee8aaee3 adding markers for pickling
46fc45d82 separating out the marker for pickle

git-subtree-dir: wrap
git-subtree-split: 10e1efd6f25f76b774868b3da33cb3954008b233
  • Loading branch information
ayushbaid committed Mar 9, 2021
1 parent c5eb449 commit afc9333
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions gtwrap/matlab_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class MatlabWrapper(object):
}
"""Methods that should not be wrapped directly"""
whitelist = ['serializable', 'serialize']
"""Methods that should be ignored"""
ignore_methods = ['pickle']
"""Datatypes that do not need to be checked in methods"""
not_check_type = []
"""Data types that are primitive types"""
Expand Down Expand Up @@ -563,6 +565,8 @@ def class_comment(self, instantiated_class):
for method in methods:
if method.name in self.whitelist:
continue
if method.name in self.ignore_methods:
continue

comment += '%{name}({args})'.format(name=method.name, args=self._wrap_args(method.args))

Expand Down Expand Up @@ -612,6 +616,9 @@ def wrap_methods(self, methods, globals=False, global_ns=None):
methods = self._group_methods(methods)

for method in methods:
if method in self.ignore_methods:
continue

if globals:
self._debug("[wrap_methods] wrapping: {}..{}={}".format(method[0].parent.name, method[0].name,
type(method[0].parent.name)))
Expand Down Expand Up @@ -861,6 +868,8 @@ def wrap_class_methods(self, namespace_name, inst_class, methods, serialize=[Fal
method_name = method[0].name
if method_name in self.whitelist and method_name != 'serialize':
continue
if method_name in self.ignore_methods:
continue

if method_name == 'serialize':
serialize[0] = True
Expand Down Expand Up @@ -932,6 +941,9 @@ def wrap_static_methods(self, namespace_name, instantiated_class, serialize):
format_name = list(static_method[0].name)
format_name[0] = format_name[0].upper()

if static_method[0].name in self.ignore_methods:
continue

method_text += textwrap.indent(textwrap.dedent('''\
function varargout = {name}(varargin)
'''.format(name=''.join(format_name))),
Expand Down
7 changes: 6 additions & 1 deletion gtwrap/pybind_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def _wrap_method(self, method, cpp_class, prefix, suffix, method_suffix=""):
[]({class_inst} self, string serialized){{
gtsam::deserialize(serialized, *self);
}}, py::arg("serialized"))
'''.format(class_inst=cpp_class + '*'))
if cpp_method == "pickle":
if not cpp_class in self._serializing_classes:
raise ValueError("Cannot pickle a class which is not serializable")
return textwrap.dedent('''
.def(py::pickle(
[](const {cpp_class} &a){{ // __getstate__
/* Returns a string that encodes the state of the object */
Expand All @@ -85,7 +90,7 @@ def _wrap_method(self, method, cpp_class, prefix, suffix, method_suffix=""):
gtsam::deserialize(t[0].cast<std::string>(), obj);
return obj;
}}))
'''.format(class_inst=cpp_class + '*', cpp_class=cpp_class))
'''.format(cpp_class=cpp_class))

is_method = isinstance(method, instantiator.InstantiatedMethod)
is_static = isinstance(method, parser.StaticMethod)
Expand Down
2 changes: 2 additions & 0 deletions tests/expected-python/geometry_pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ PYBIND11_MODULE(geometry_py, m_) {
[](gtsam::Point2* self, string serialized){
gtsam::deserialize(serialized, *self);
}, py::arg("serialized"))

.def(py::pickle(
[](const gtsam::Point2 &a){ // __getstate__
/* Returns a string that encodes the state of the object */
Expand All @@ -71,6 +72,7 @@ PYBIND11_MODULE(geometry_py, m_) {
[](gtsam::Point3* self, string serialized){
gtsam::deserialize(serialized, *self);
}, py::arg("serialized"))

.def(py::pickle(
[](const gtsam::Point3 &a){ // __getstate__
/* Returns a string that encodes the state of the object */
Expand Down
6 changes: 6 additions & 0 deletions tests/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Point2 {
VectorNotEigen vectorConfusion();

void serializable() const; // Sets flag and creates export, but does not make serialization functions

// enable pickling in python
void pickle() const;
};

#include <gtsam/geometry/Point3.h>
Expand All @@ -35,6 +38,9 @@ class Point3 {

// enabling serialization functionality
void serialize() const; // Just triggers a flag internally and removes actual function

// enable pickling in python
void pickle() const;
};

}
Expand Down

0 comments on commit afc9333

Please sign in to comment.