def testDefaultsTestSerializationDeserialization(self):
     v1 = sample_service_mojom.DefaultsTest()
     v1.a18 = []
     v1.a19 = ""
     v1.a21 = sample_import_mojom.Point()
     v1.a22.location = sample_import_mojom.Point()
     v1.a22.size = sample_import2_mojom.Size()
     (data, handles) = v1.Serialize()
     context = serialization.RootDeserializationContext(data, handles)
     v2 = sample_service_mojom.DefaultsTest.Deserialize(context)
     # NaN needs to be a special case.
     self.assertNotEquals(v1, v2)
     self.assertTrue(math.isnan(v2.a28))
     self.assertTrue(math.isnan(v2.a31))
     v1.a28 = v2.a28 = v1.a31 = v2.a31 = 0
     self.assertEquals(v1, v2)
Beispiel #2
0
 def Accept(message):
     try:
         assert message.header.message_type == method.ordinal
         payload = message.payload
         response = method.response_struct.Deserialize(
             serialization.RootDeserializationContext(
                 payload.data, payload.handles))
         as_dict = response.AsDict()
         if len(as_dict) == 1:
             value = as_dict.values()[0]
             if not isinstance(value, dict):
                 response = value
         resolve(response)
         return True
     except Exception as e:
         # Adding traceback similarly to python 3.0 (pep-3134)
         e.__traceback__ = sys.exc_info()[2]
         reject(e)
         return False
     finally:
         self._error_handler.RemoveCallback(reject)
Beispiel #3
0
    def Accept(self, message, responder=None):
        try:
            header = message.header
            assert header.expects_response == bool(responder)
            assert header.message_type in methods_by_ordinal
            method = methods_by_ordinal[header.message_type]
            payload = message.payload
            parameters = method.parameters_struct.Deserialize(
                serialization.RootDeserializationContext(
                    payload.data, payload.handles)).AsDict()
            response = getattr(self.impl, method.name)(**parameters)
            if header.expects_response:

                def SendResponse(response):
                    if isinstance(response, dict):
                        response_message = _GetMessage(
                            method, messaging.MESSAGE_IS_RESPONSE_FLAG,
                            **response)
                    else:
                        response_message = _GetMessage(
                            method, messaging.MESSAGE_IS_RESPONSE_FLAG,
                            response)
                    response_message.header.request_id = header.request_id
                    responder.Accept(response_message)

                p = promise.Promise.Resolve(response).Then(SendResponse)
                if self.impl.manager:
                    # Close the connection in case of error.
                    p.Catch(lambda _: self.impl.manager.Close())
            return True
        # pylint: disable=W0702
        except:
            # Close the connection in case of error.
            logging.warning(
                'Error occured in accept method. Connection will be closed.')
            if self.impl.manager:
                self.impl.manager.Close()
            return False
 def testFooSerializationDeserialization(self):
     foo1 = _NewFoo()
     (data, handles) = foo1.Serialize()
     context = serialization.RootDeserializationContext(data, handles)
     foo2 = sample_service_mojom.Foo.Deserialize(context)
     self.assertEquals(foo1, foo2)
 def testFooDeserialization(self):
     (data, handles) = _NewFoo().Serialize()
     context = serialization.RootDeserializationContext(data, handles)
     self.assertTrue(sample_service_mojom.Foo.Deserialize(context))