Skip to content

Commit

Permalink
Make Redmine class picklable
Browse files Browse the repository at this point in the history
The `Redmine` class couldn't be pickled because the `gettatr` would attempt to
turn any attribute access into a `Resource`, even `__getstate__`.

The proposed fix treats attributes that begin with an underscore (and
double-underscore) as non-`Resource` attributes and returns their value as-is.

Fixes #64
  • Loading branch information
rconradharris committed Nov 3, 2014
1 parent a827812 commit 8b0e18b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
1.0.2 (2014-10-XX)
++++++++++++++++++

- Fixed: `Issue #64 <https://github.com/maxtepkeev/python-redmine/issues/64>`__ (Make the Redmine class picklable) (thanks to `Rick Harris <https://github.com/rconradharris>`__)
- Fixed: `Issue #55 <https://github.com/maxtepkeev/python-redmine/issues/55>`__ (TypeError was
raised during processing validation errors from Redmine when one of the errors was returned as
a list)
Expand Down
8 changes: 7 additions & 1 deletion redmine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ def __init__(self, url, **kwargs):

def __getattr__(self, resource):
"""Returns either ResourceSet or Resource object depending on the method used on the ResourceManager"""
return ResourceManager(self, resource)
if resource.startswith('_'):
try:
return self.__dict__[resource]
except KeyError:
raise AttributeError
else:
return ResourceManager(self, resource)

def upload(self, filepath):
"""Uploads file from filepath to Redmine and returns an assigned token"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_redmine.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,13 @@ def test_auth(self):
self.response.status_code = 200
self.response.json = json_response({'user': {'firstname': 'John', 'lastname': 'Smith', 'id': 1}})
self.assertEqual(self.redmine.auth().firstname, 'John')

def test_getattr_on_under_method(self):
"""
Attributes that begin with an underscore should not be treated as a
`Resource`, rather we should return the value as-is. The impetus of
this was to make `Redmine` picklable by preventing the `__getstate__`
access from being treated a `Resource`.
"""
self.redmine._foo = True
self.assertEqual(True, self.redmine._foo)

0 comments on commit 8b0e18b

Please sign in to comment.