def get_non_persistent_object(self, state, obj): if '_py_constant' in state: return self.simple_resolve(state.pop('_py_constant')) if '_py_type' in state: # Handle the simplified case. klass = self.simple_resolve(state.pop('_py_type')) sub_obj = copy_reg._reconstructor(klass, object, None) elif '_py_persistent_type' in state: # Another simple case for persistent objects that do not want # their own document. klass = self.simple_resolve(state.pop('_py_persistent_type')) sub_obj = copy_reg.__newobj__(klass) else: factory = self.simple_resolve(state.pop('_py_factory')) factory_args = self.get_object(state.pop('_py_factory_args'), obj) sub_obj = factory(*factory_args) if len(state): sub_obj_state = self.get_object(state, obj) if isinstance(sub_obj, persistent.Persistent): sub_obj.__setstate__(sub_obj_state) # This is a persistent sub-object -- mark it as such. Otherwise # we risk to store this object in its own collection next time. sub_obj._p_mongo_sub_object = True else: sub_obj.__dict__.update(sub_obj_state) if getattr(sub_obj, '_p_mongo_sub_object', False): sub_obj._p_mongo_doc_object = obj sub_obj._p_jar = self._jar return sub_obj
def test_reconstructor(): reconstructor_copy = copy_reg._reconstructor try: obj = copy_reg._reconstructor(object, object, None) Assert(type(obj) is object) #set,get, the value is a random int rand = _random.Random() value = rand.getrandbits(8) copy_reg._reconstructor = value result = copy_reg._reconstructor Assert(result == value, "set or get of the attribute failed!") #the value is a string value2 = "value2" copy_reg._reconstructor = value2 result = copy_reg._reconstructor Assert(result == value2, "set or get of the attribute failed!") #the value is a custom type object value3 = testclass() copy_reg._reconstructor = value3 result = copy_reg._reconstructor Assert(result == value3, "set or get of the attribute failed!") finally: copy_reg._reconstructor = reconstructor_copy
def test_reconstructor(self): reconstructor_copy = copy_reg._reconstructor try: obj = copy_reg._reconstructor(object, object, None) self.assertTrue(type(obj) is object) #set,get, the value is a random int rand = random.Random() value = rand.getrandbits(8) copy_reg._reconstructor = value result = copy_reg._reconstructor self.assertTrue(result == value, "set or get of the attribute failed!") #the value is a string value2 = "value2" copy_reg._reconstructor = value2 result = copy_reg._reconstructor self.assertTrue(result == value2, "set or get of the attribute failed!") #the value is a custom type object value3 = testclass() copy_reg._reconstructor = value3 result = copy_reg._reconstructor self.assertTrue(result == value3, "set or get of the attribute failed!") finally: copy_reg._reconstructor = reconstructor_copy
def get_non_persistent_object(self, state, obj): if '_py_constant' in state: return self.simple_resolve(state['_py_constant']) # this method must NOT change the passed in state dict state = dict(state) if '_py_type' in state: # Handle the simplified case. klass = self.simple_resolve(state.pop('_py_type')) sub_obj = copy_reg._reconstructor(klass, object, None) elif interfaces.PY_TYPE_ATTR_NAME in state: # Another simple case for persistent objects that do not want # their own document. klass = self.simple_resolve(state.pop(interfaces.PY_TYPE_ATTR_NAME)) sub_obj = copy_reg.__newobj__(klass) else: factory = self.simple_resolve(state.pop('_py_factory')) factory_args = self.get_object(state.pop('_py_factory_args'), obj) sub_obj = factory(*factory_args) if len(state): sub_obj_state = self.get_object(state, obj) if hasattr(sub_obj, '__setstate__'): sub_obj.__setstate__(sub_obj_state) else: sub_obj.__dict__.update(sub_obj_state) if isinstance(sub_obj, persistent.Persistent): # This is a persistent sub-object -- mark it as such. Otherwise # we risk to store this object in its own table next time. setattr(sub_obj, interfaces.SUB_OBJECT_ATTR_NAME, True) if getattr(sub_obj, interfaces.SUB_OBJECT_ATTR_NAME, False): setattr(sub_obj, interfaces.DOC_OBJECT_ATTR_NAME, obj) sub_obj._p_jar = self._jar return sub_obj
def load_reduce(self): stack = self.stack args = stack.pop() func = stack[-1] if len(args) != 3 or func != _reconstructor: raise UnpicklingError("Security: Unacceptable reduce function use detected") # Some potential room for abuse value = _reconstructor(args[0], args[1], args[2]) stack[-1] = value
def get_non_persistent_object(self, state, obj): if '_py_type' in state: # Handle the simplified case. klass = self.simple_resolve(state.pop('_py_type')) sub_obj = copy_reg._reconstructor(klass, object, None) elif '_py_persistent_type' in state: # Another simple case for persistent objects that do not want # their own document. klass = self.simple_resolve(state.pop('_py_persistent_type')) sub_obj = copy_reg.__newobj__(klass) else: factory = self.simple_resolve(state.pop('_py_factory')) factory_args = self.get_object(state.pop('_py_factory_args'), obj) sub_obj = factory(*factory_args) if len(state): sub_obj_state = self.get_object(state, obj) if isinstance(sub_obj, persistent.Persistent): sub_obj.__setstate__(sub_obj_state) else: sub_obj.__dict__.update(sub_obj_state) if getattr(sub_obj, '_p_mongo_sub_object', False): sub_obj._p_mongo_doc_object = obj sub_obj._p_jar = self._jar return sub_obj