Some additional things that might be worth doing. I believe this exposes enough
to allow people to build an object graph walker out of the `pickle`/`copy`
protocol without having to access fragile internals, and without restricting
future evolution of the internals of the protocol. See [the same -ideas
thread][1] again for details on how it could be used and why.
These would all be changes to the `copy` module, together with the changes to
`pickle` and `copyreg` in the previous message:
class Memo(dict):
"""Memo is a mapping that can be used to do memoization exactly the
same way
deepcopy does, so long as you only use ids as keys and only use these
operations:
y = memo.get(id(x), default)
memo[id(x)] = y
memo.keep_alive(x)
"""
def keep_alive(self, x):
self.setdefault(id(self), []).append(x)
def reconstruct(x, memo: Memo, reduction, *, recurse=deepcopy):
"""reconstruct(x, memo, reduction, recurse=recursive_walker)
Constructs a new object from the reduction by calling recursive_walker
on each value. The reduction should have been obtained as
pickle.reduce(x)
and the memo should be a Memo instance (which will be passed to each
recursive_walker call).
"""
return _reconstruct(x, memo, *reduction, deepcopy=recurse)
def copier(cls):
"""copier(cls) -> func
Returns a function func(x, memo, recurse) that can be used to copy
objects
of type cls without reducing and reconstructing them, or None if there
is no
such function.
"""
if c := _deepcopy_dispatch.get(cls):
return c
if issubclass(cls, type):
return _deepcopy_atomic
Also, all of the private functions that are stored in `_deepcopy_dispatch`
would rename their `deepcopy` parameter to `recurse`, and the two that don't
have such a parameter would add it.
[1]:
https://mail.python.org/archives/list/[email protected]/thread/RTZGM7L7JOTKQQICN6XDSLOAMU4A62CA/
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/VSHNDMSQ7XSIICVTQM2LPCDDPX3Q7I2M/
Code of Conduct: http://python.org/psf/codeofconduct/