src/extras/pyrepl/dumbpickle.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # A really dumb pickle replacement, that converts the given object into a
       
     2 # string with the repr function. Only objects that support repr and
       
     3 # for which eval(repr(a))==a holds are accepted.
       
     4 #
       
     5 # Copyright (c) 2005 Nokia Corporation
       
     6 #
       
     7 # Licensed under the Apache License, Version 2.0 (the "License");
       
     8 # you may not use this file except in compliance with the License.
       
     9 # You may obtain a copy of the License at
       
    10 #
       
    11 #     http://www.apache.org/licenses/LICENSE-2.0
       
    12 #
       
    13 # Unless required by applicable law or agreed to in writing, software
       
    14 # distributed under the License is distributed on an "AS IS" BASIS,
       
    15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    16 # See the License for the specific language governing permissions and
       
    17 # limitations under the License.
       
    18 
       
    19 
       
    20 def dump(obj, file, protocol=None, bin=None):
       
    21     file.write(dumps(obj,protocol,bin))
       
    22 
       
    23 def dumps(obj, protocol=None, bin=None):
       
    24     if protocol or bin:
       
    25         raise "Arguments protocol and bin not implemented."
       
    26     objstr=repr(obj)
       
    27     # check that the object can be unpickled properly
       
    28     if eval(objstr)!=obj:
       
    29         raise "Sorry, dumbpickle can't pickle this object because "+\
       
    30               "eval(repr(obj))!=obj."
       
    31     return objstr
       
    32 
       
    33 def load(file):
       
    34     return loads(' '.join(file.readlines()))
       
    35 
       
    36 def loads(string):
       
    37     return eval(string)
       
    38