Skip to content

string.pickle makes some bad assumptions #61

Open

Description

@gvvaughan

I wonder whether we should have a special metamethod for pickle? The assumption that tostring (object) will generate a string that can be passed to functional.eval doesn't even hold true for the objects we provide in stdlib.

Two options:

  1. Objects that can be pickled have a __pickle metamethod (analogous to Python's __repr) that outputs a constructor for the object being pickled (including a require "appropriate.module".constructor {args} rather than assuming "appropriate.module" is already loaded).
  2. We have to be careful that __tostring does double duty with the above.

I'm leaning heavily towards (1) because, among others, it makes a cannot pickle error easy to determine and produce.

Activity

  1. modified the milestone: on Jun 7, 2014
  2. gvvaughan commented on Jun 7, 2014

    @gvvaughan
    ContributorAuthor

    I forgot to mention, that I wonder why pickle isn't built with render? That would at least solve the TODO item for handling recursive tables, although not in a way that promotes rebuilding the pickled object with functional.eval... so maybe I answered my own question?

  3. rrthomas commented on Jun 7, 2014

    @rrthomas
    Contributor

    I agree with leaning towards option 1. I suspect the reason pickle isn't built on render is that it's supposed to be canonical, i.e. not customizable; of course, it assumes (at least in the original version I'm looking at) that tostring produces an eval-able representation, which only makes sense for primitive types.

    I have some concerns. I think we need to make sure that pickle is supported where possible; whereas the temptation is to write a quick-and-dirty tostring method, and "worry about pickle if we need it". Can we give some incentive to do the opposite? What is it that makes pickles hard to digest, other than recursion, and suitable escaping of strings? Could we in fact end up with just pickling and pretty-printing, and implement the latter on top of the former, so that by default new classes only implement __pickle?

  4. gvvaughan commented on Jun 8, 2014

    @gvvaughan
    ContributorAuthor

    That feels like a good approach, and should be quite easy to implement in the existing stdlib uses. I'll work on that then thanks :-)

    Although object and container rely on __totable, I can't shake the feeling that having __pickle, __tostring and __totable is unnecessarily baroque, so it would be really nice to find a way of teasing apart the requirements such that having to implement all three is a rare occurrence. Maybe I needn't worry if object and container base classes have good enough defaults that they don't need to be overridden every time they are subclassed.

  5. self-assigned this
    on Jun 8, 2014
  6. removed their assignment
    on Jul 17, 2014
  7. gvvaughan commented on Jul 21, 2014

    @gvvaughan
    ContributorAuthor

    Clean ups to __totable in #68 make the implementation of std.object clearer and more idiomatic, and allows for simpler complex object creation where the default __pickle and __pairs metamethods adequately service pickling, pretty-printing and cloning functionality without overrides in children 😀

  8. gvvaughan commented on Sep 12, 2015

    @gvvaughan
    ContributorAuthor

    Bad assumptions are gone. But the API requires too much gymnastics between __pickle and __tostring when creating new object prototypes... more work required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions