Example #1
0
def object_mapper(instance):
    """Given an object, return the primary Mapper associated with the object instance.

    Raises UnmappedInstanceError if no mapping is configured.

    """
    try:
        state = attributes.instance_state(instance)
        if not state.manager.mapper:
            raise exc.UnmappedInstanceError(instance)
        return state.manager.mapper
    except exc.NO_STATE:
        raise exc.UnmappedInstanceError(instance)
Example #2
0
    def __setstate__(self, state):
        self.obj = weakref.ref(state['instance'], self._cleanup)
        self.class_ = state['instance'].__class__
        self.manager = manager = manager_of_class(self.class_)
        if manager is None:
            raise orm_exc.UnmappedInstanceError(
                state['instance'],
                "Cannot deserialize object of type %r - no mapper() has"
                " been configured for this class within the current Python process!"
                % self.class_)
        elif manager.is_mapped and not manager.mapper.compiled:
            manager.mapper.compile()

        self.committed_state = state.get('committed_state', {})
        self.pending = state.get('pending', {})
        self.parents = state.get('parents', {})
        self.modified = state.get('modified', False)
        self.expired = state.get('expired', False)
        self.callables = state.get('callables', {})

        if self.modified:
            self._strong_obj = state['instance']

        self.__dict__.update([(k, state[k])
                              for k in ('key', 'load_options', 'mutable_dict')
                              if k in state])

        if 'load_path' in state:
            self.load_path = interfaces.deserialize_path(state['load_path'])
    def test_exceptions(self):
        class Foo(object):
            pass
        users = self.tables.users
        mapper(User, users)

        for sa_exc in (
            orm_exc.UnmappedInstanceError(Foo()),
            orm_exc.UnmappedClassError(Foo),
            orm_exc.ObjectDeletedError(attributes.instance_state(User())),
        ):
            for loads, dumps in picklers():
                repickled = loads(dumps(sa_exc))
                eq_(repickled.args[0], sa_exc.args[0])
Example #4
0
    def __setstate__(self, state):
        from sqlalchemy.orm import instrumentation
        inst = state['instance']
        if inst is not None:
            self.obj = weakref.ref(inst, self._cleanup)
            self.class_ = inst.__class__
        else:
            # None being possible here generally new as of 0.7.4
            # due to storage of state in "parents".  "class_"
            # also new.
            self.obj = None
            self.class_ = state['class_']
        self.manager = manager = instrumentation.manager_of_class(self.class_)
        if manager is None:
            raise orm_exc.UnmappedInstanceError(
                        inst,
                        "Cannot deserialize object of type %r - no mapper() has"
                        " been configured for this class within the current Python process!" %
                        self.class_)
        elif manager.is_mapped and not manager.mapper.configured:
            mapperlib.configure_mappers()

        self.committed_state = state.get('committed_state', {})
        self.pending = state.get('pending', {})
        self.parents = state.get('parents', {})
        self.modified = state.get('modified', False)
        self.expired = state.get('expired', False)
        self.callables = state.get('callables', {})

        if self.modified:
            self._strong_obj = inst

        self.__dict__.update([
            (k, state[k]) for k in (
                'key', 'load_options', 'mutable_dict'
            ) if k in state 
        ])

        if 'load_path' in state:
            self.load_path = interfaces.deserialize_path(state['load_path'])

        # setup _sa_instance_state ahead of time so that 
        # unpickle events can access the object normally.
        # see [ticket:2362]
        manager.setup_instance(inst, self)
        manager.dispatch.unpickle(self, state)
Example #5
0
 def test_delete(self, mock_get_one, mock_warning, mock_os, mock_shutil):
     self.o.name = "anyName"
     self.o.session = None
     self.assertFalse(self.o.delete())
     mock_session = mock.Mock()
     mock_session.delete.side_effect = exc.UnmappedInstanceError(1)
     self.o.session = mock_session
     mock_get_one.return_value = "anyOng"
     self.assertFalse(self.o.delete())
     mock_session.rollback.assert_called_with()
     mock_warning.assert_called_with('Delete from DB fail.')
     mock_session.delete.side_effect = None
     mock_os.getcwd.return_value = "/home/ocr"
     mock_os.path.join.return_value = '/home/ocr/ocr' + ONG_FOLDER + '/anyName'
     self.assertTrue(self.o.delete())
     mock_os.getcwd.assert_called_with()
     mock_os.path.join.assert_called_with('/home/ocr', 'ocr', ONG_FOLDER, 'anyName')
     mock_shutil.rmtree.assert_called_with('/home/ocr/ocr' + ONG_FOLDER + '/anyName')
     mock_shutil.rmtree.side_effect = OSError()
     self.assertFalse(self.o.delete())
     mock_warning.assert_called_with('The ONG directory does not exists')