|
43 | 43 | from collections.abc import Sized, Sequence
|
44 | 44 | import copy
|
45 | 45 | import functools
|
| 46 | +import importlib |
46 | 47 | import inspect
|
47 | 48 | import io
|
48 | 49 | import itertools
|
@@ -1528,9 +1529,22 @@ def _make_norm_from_scale(scale_cls, base_norm_cls, bound_init_signature):
|
1528 | 1529 |
|
1529 | 1530 | class Norm(base_norm_cls):
|
1530 | 1531 | def __reduce__(self):
|
| 1532 | + cls = type(self) |
| 1533 | + # If the class is toplevel-accessible, it is possible to directly |
| 1534 | + # pickle it "by name". This is required to support norm classes |
| 1535 | + # defined at a module's toplevel, as the inner base_norm_cls is |
| 1536 | + # otherwise unpicklable (as it gets shadowed by the generated norm |
| 1537 | + # class). If either import or attribute access fails, fall back to |
| 1538 | + # the general path. |
| 1539 | + try: |
| 1540 | + if cls is getattr(importlib.import_module(cls.__module__), |
| 1541 | + cls.__qualname__): |
| 1542 | + return (_create_empty_object_of_class, (cls,), vars(self)) |
| 1543 | + except (ImportError, AttributeError): |
| 1544 | + pass |
1531 | 1545 | return (_picklable_norm_constructor,
|
1532 | 1546 | (scale_cls, base_norm_cls, bound_init_signature),
|
1533 |
| - self.__dict__) |
| 1547 | + vars(self)) |
1534 | 1548 |
|
1535 | 1549 | def __init__(self, *args, **kwargs):
|
1536 | 1550 | ba = bound_init_signature.bind(*args, **kwargs)
|
@@ -1603,11 +1617,14 @@ def autoscale_None(self, A):
|
1603 | 1617 | return Norm
|
1604 | 1618 |
|
1605 | 1619 |
|
1606 |
| -def _picklable_norm_constructor(*args): |
1607 |
| - cls = _make_norm_from_scale(*args) |
| 1620 | +def _create_empty_object_of_class(cls): |
1608 | 1621 | return cls.__new__(cls)
|
1609 | 1622 |
|
1610 | 1623 |
|
| 1624 | +def _picklable_norm_constructor(*args): |
| 1625 | + return _create_empty_object_of_class(_make_norm_from_scale(*args)) |
| 1626 | + |
| 1627 | + |
1611 | 1628 | @make_norm_from_scale(
|
1612 | 1629 | scale.FuncScale,
|
1613 | 1630 | init=lambda functions, vmin=None, vmax=None, clip=False: None)
|
|
0 commit comments