Skip to content

Commit 9b59adc

Browse files
committed
Make gitlab objects serializable
With current implementation of API v3 and v4 support, some instances have properties of type module and are not serializable. Handle these properties manually with setstate and getstate methods.
1 parent 36c023d commit 9b59adc

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

gitlab/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ def __init__(self, url, private_token=None, email=None, password=None,
138138
manager = getattr(objects, cls_name)(self)
139139
setattr(self, var_name, manager)
140140

141+
def __getstate__(self):
142+
state = self.__dict__.copy()
143+
state.pop('_objects')
144+
return state
145+
146+
def __setstate__(self, state):
147+
self.__dict__.update(state)
148+
objects = importlib.import_module('gitlab.v%s.objects' %
149+
self._api_version)
150+
self._objects = objects
151+
141152
@property
142153
def api_version(self):
143154
return self._api_version

gitlab/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,17 @@ def __init__(self, gl, data=None, **kwargs):
416416
if not hasattr(self, "id"):
417417
self.id = None
418418

419+
def __getstate__(self):
420+
state = self.__dict__.copy()
421+
module = state.pop('_module')
422+
state['_module_name'] = module.__name__
423+
return state
424+
425+
def __setstate__(self, state):
426+
module_name = state.pop('_module_name')
427+
self.__dict__.update(state)
428+
self._module = importlib.import_module(module_name)
429+
419430
def _set_manager(self, var, cls, attrs):
420431
manager = cls(self.gitlab, self, attrs)
421432
setattr(self, var, manager)
@@ -555,6 +566,17 @@ def __init__(self, manager, attrs):
555566
self.__dict__['_parent_attrs'] = self.manager.parent_attrs
556567
self._create_managers()
557568

569+
def __getstate__(self):
570+
state = self.__dict__.copy()
571+
module = state.pop('_module')
572+
state['_module_name'] = module.__name__
573+
return state
574+
575+
def __setstate__(self, state):
576+
module_name = state.pop('_module_name')
577+
self.__dict__.update(state)
578+
self._module = importlib.import_module(module_name)
579+
558580
def __getattr__(self, name):
559581
try:
560582
return self.__dict__['_updated_attrs'][name]

gitlab/tests/test_gitlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from __future__ import print_function
2020

21+
import pickle
2122
try:
2223
import unittest
2324
except ImportError:
@@ -26,7 +27,6 @@
2627
from httmock import HTTMock # noqa
2728
from httmock import response # noqa
2829
from httmock import urlmatch # noqa
29-
import pickle
3030
import six
3131

3232
import gitlab

0 commit comments

Comments
 (0)