Reply to Stefan Behnel and Chris Angelico.

On 18-12-27 22:42, Stefan Behnel wrote:
>  >>> import pickle, re
>  >>> p = re.compile("[abc]")
>  >>> pickle.dumps(p)
>  b'\x80\x03cre\n_compile\nq\x00X\x05\x00\x00\x00[abc]q\x01K \x86q\x02Rq\x03.'
>
> What this does, essentially, is to make the pickle loader pass the original regex pattern string into re.compile() to "unpickle" it. Meaning, it compiles the regex on the way in. Thus, there isn't much to gain from using (the current form of) regex pickling here.

Yes, re module only pickles pattern string and flags, it's safe for cross-version pickle/unpickle. re module's pickle code:

def _pickle(p):
    return _compile, (p.pattern, p.flags)
copyreg.pickle(Pattern, _pickle, _compile)

On 18-12-28 1:27, Chris Angelico wrote:
> What Stefan pointed out regarding the stdlib's "re" module is also
> true of the third party "regex" - unpickling just compiles from the
> original string.

I had followed regex module for a year, it does pickle the compiled data, this is its code:

def _pickle(pattern):
    return _regex.compile, pattern._pickled_data
_copy_reg.pickle(Pattern, _pickle)

// in _regex.c file
self->pickled_data = Py_BuildValue("OnOOOOOnOnn", pattern, flags,
    code_list, groupindex, indexgroup, named_lists, named_list_indexes,
    req_offset, required_chars, req_flags, public_group_count);
if (!self->pickled_data) {
    Py_DECREF(self);
    return NULL;
}

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to