sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.32k stars 453 forks source link

Pickling of Boolean functions #21038

Open nbruin opened 8 years ago

nbruin commented 8 years ago

Presently:

sage: dumps(BooleanFunction([0]))
ValueError: negative shift count
sage: dumps(BooleanFunction([0,1]))
ValueError: negative shift count

This is because

sage: BooleanFunction([0]).truth_table(format="hex")
ValueError: negative shift count
sage: BooleanFunction([0,1]).truth_table(format="hex")
ValueError: negative shift count

This problem was reported in the thread of https://groups.google.com/d/msg/sage-devel/evIMO7NEFNc/GzCwfqv1AAAJ and a fix is proposed there.

CC: @jpflori

Component: coding theory

Issue created by migration from https://trac.sagemath.org/ticket/21038

nbruin commented 8 years ago
comment:1

The proposed fix is to pickle boolean functions in 0 or 1 variables with a different encoding format. This should be backward compatible with how functions with more variables were pickled before.

def __reduce__(self):
    if self.nvariables() <2:
        return unpickle_BooleanFunction, (self.truth_table(format='int'),)
    else
        unpickle_BooleanFunction, (self.truth_table(format='hex'),)