Skip to content

Commit

Permalink
numba: Fix pickling of LayoutType(#350)
Browse files Browse the repository at this point in the history
Fixes #349, by not saving the contents of the `_cache` attribute.

Objects that are round-tripped through pickle probably still have issues, but this at least prevents it crashing immediately.
  • Loading branch information
eric-wieser committed Jul 20, 2020
2 parents 23052e9 + 62db78d commit 6659f44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions clifford/numba/_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def __init__(self, layout):
name = "LayoutType({!r})".format(layout)
super().__init__(name)

def __getstate__(self):
# the cache causes issues with numba's pickle modifications, as it
# contains a self-reference.
d = self.__dict__.copy()
d['_cache'] = {}
return d


@numba.extending.register_model(LayoutType)
class LayoutModel(numba.extending.models.OpaqueModel):
Expand Down
16 changes: 14 additions & 2 deletions clifford/test/test_numba_extensions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import numba
import operator
import pickle

import numba
import pytest

from clifford.g3c import layout, e1, e2, e3, e4, e5
import clifford as cf
import pytest


@numba.njit
Expand Down Expand Up @@ -160,3 +162,13 @@ def test_multiple_grade_projection(self, func):

assert func(a, 0, 1) == 1 + e1
assert func(a, 0, 1, 2, 3, 4, 5) == a


def test_pickling():
lt = layout._numba_type_
assert pickle.loads(pickle.dumps(lt)) is lt

# gh-349, populating `lt._cache` should not break pickling
e1._numba_type_ # int
(e1 * 1.0)._numba_type_ # float
assert pickle.loads(pickle.dumps(lt)) is lt

0 comments on commit 6659f44

Please sign in to comment.