我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用google.protobuf.message.DecodeError()。
def start(self): """Starts receiving messages on the underlying socket and passes them to the message router. """ self._is_running = True while self._is_running: try: zmq_msg = await self._socket.recv_multipart() message = Message() message.ParseFromString(zmq_msg[-1]) await self._msg_router.route_msg(message) except DecodeError as e: LOGGER.warning('Unable to decode: %s', e) except zmq.ZMQError as e: LOGGER.warning('Unable to receive: %s', e) return except asyncio.CancelledError: self._is_running = False
def _parse_header(cls, header_proto, resource): """Deserializes a resource's base64 encoded Protobuf header. """ header = header_proto() try: header_bytes = base64.b64decode(resource['header']) header.ParseFromString(header_bytes) except (KeyError, TypeError, ValueError, DecodeError): header = resource.get('header', None) LOGGER.error( 'The validator sent a resource with %s %s', 'a missing header' if header is None else 'an invalid header:', header or '') raise errors.ResourceHeaderInvalid() resource['header'] = cls._message_to_dict(header) return resource
def handle(self, connection_id, message_content): response_proto = client_batch_submit_pb2.ClientBatchSubmitResponse def make_response(out_status): return HandlerResult( status=HandlerStatus.RETURN, message_out=response_proto(status=out_status), message_type=Message.CLIENT_BATCH_SUBMIT_RESPONSE) try: request = client_batch_submit_pb2.ClientBatchSubmitRequest() request.ParseFromString(message_content) except DecodeError: return make_response(response_proto.INTERNAL_ERROR) for batch in request.batches: if batch.trace: LOGGER.debug("TRACE %s: %s", batch.header_signature, self.__class__.__name__) if not all(map(is_valid_batch, request.batches)): return make_response(response_proto.INVALID_BATCH) return HandlerResult(status=HandlerStatus.PASS)
def handle(self, connection_id, message_content): """Handles parsing incoming requests, and wrapping the final response. Args: connection_id (str): ZMQ identity sent over ZMQ socket message_content (bytes): Byte encoded request protobuf to be parsed Returns: HandlerResult: result to be sent in response back to client """ try: request = self._request_proto() request.ParseFromString(message_content) except DecodeError: LOGGER.info('Protobuf %s failed to deserialize', request) return self._wrap_result(self._status.INTERNAL_ERROR) try: response = self._respond(request) except _ResponseFailed as e: response = e.status return self._wrap_result(response)
def _parse_main_response(self, response_raw, subrequests): self.log.debug('Parsing main RPC response...') if response_raw.status_code == 403: raise ServerSideAccessForbiddenException("Seems your IP Address is banned or something else went badly wrong...") elif response_raw.status_code == 502: raise ServerBusyOrOfflineException("502: Bad Gateway") elif response_raw.status_code != 200: error = 'Unexpected HTTP server response - needs 200 got {}'.format(response_raw.status_code) self.log.warning(error) self.log.debug('HTTP output: \n%s', response_raw.content.decode('utf-8')) raise UnexpectedResponseException(error) if response_raw.content is None: self.log.warning('Empty server response!') return False response_proto = ResponseEnvelope() try: response_proto.ParseFromString(response_raw.content) except message.DecodeError as e: self.log.warning('Could not parse response: %s', e) return False self.log.debug('Protobuf structure of rpc response:\n\r%s', response_proto) try: self.log.debug('Decode raw over protoc (protoc has to be in your PATH):\n\r%s', self.decode_raw(response_raw.content).decode('utf-8')) except: self.log.debug('Error during protoc parsing - ignored.') response_proto_dict = protobuf_to_dict(response_proto) response_proto_dict = self._parse_sub_responses(response_proto, subrequests, response_proto_dict) return response_proto_dict
def extract_channel_config(configtx_proto_envelope): """ Extracts the protobuf 'ConfigUpdate' object out ouf the 'ConfigEnvelope'. Args: configtx_proto_envelope (common_pb2.Envelope): The encoded bytes of the ConfigEnvelope protofbuf. Returns: config_update (configtx_pb2.ConfigUpadeEnvelope.config_update): The encoded bytes of the ConfigUpdate protobuf, ready to be signed Raises: ValueError: If there is an error in protobuf_decode due to a wrong or not valid profobuf file a ValueError is raised. """ _logger.debug('extract_channel_config - start') try: envelope = common_pb2.Envelope() envelope.ParseFromString(configtx_proto_envelope) payload = common_pb2.Payload() payload.ParseFromString(envelope.payload) configtx = configtx_pb2.ConfigUpdateEnvelope() configtx.ParseFromString(payload.data) except DecodeError as e: _logger.error('extract_channel_config - an error occurred decoding' ' the configtx_proto_envelope: {}'.format(e)) raise ValueError('The given configtx_proto_envelope was not valid: {}' .format(e)) return configtx.config_update
def read_bundle_file(bundle_file): # Read in bundle file. bundle = generator_pb2.GeneratorBundle() with tf.gfile.Open(bundle_file, 'rb') as f: try: bundle.ParseFromString(f.read()) except message.DecodeError as e: raise GeneratorBundleParseException(e) return bundle
def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs): while True: try: response = api_req(service, api_endpoint, access_token, *args, **kwargs) if response: return response debug('retrying_api_req: api_req returned None, retrying') except (InvalidURL, ConnectionError, DecodeError), e: debug('retrying_api_req: request error ({}), retrying'.format( str(e))) time.sleep(1)
def read_blocks_from_queue(self): """Returns a generator of the blocks in the queue. Override this method if you wish to change the queue (blocks transformation) form. Yields: Each yield is a single block object (block_pb2.Block). """ message_lines = [] for line in sys.stdin: if constants.QUEUE_DELIMITER in line: block = block_pb2.Block() try: block.ParseFromString('\n'.join(message_lines)) except message_mod.DecodeError: sys.stderr.write( 'ERROR: Can not read protocol buffer from queue. Is ' 'human_readable perhaps set to true? I am not a human. ' 'Aborting...\n') sys.exit(-1) yield block message_lines = [] else: message_lines.append(line.rstrip('\n'))
def testAssertOversizeProto(self): from google.protobuf.pyext._message import SetAllowOversizeProtos SetAllowOversizeProtos(False) q = self.proto_cls() try: q.ParseFromString(self.p_serialized) except message.DecodeError as e: self.assertEqual(str(e), 'Error parsing message')
def testParseTruncated(self): # This test is only applicable for the Python implementation of the API. if api_implementation.Type() != 'python': return first_proto = unittest_pb2.TestAllTypes() test_util.SetAllFields(first_proto) serialized = first_proto.SerializeToString() for truncation_point in range(len(serialized) + 1): try: second_proto = unittest_pb2.TestAllTypes() unknown_fields = unittest_pb2.TestEmptyMessage() pos = second_proto._InternalParse(serialized, 0, truncation_point) # If we didn't raise an error then we read exactly the amount expected. self.assertEqual(truncation_point, pos) # Parsing to unknown fields should not throw if parsing to known fields # did not. try: pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point) self.assertEqual(truncation_point, pos2) except message.DecodeError: self.fail('Parsing unknown fields failed when parsing known fields ' 'did not.') except message.DecodeError: # Parsing unknown fields should also fail. self.assertRaises(message.DecodeError, unknown_fields._InternalParse, serialized, 0, truncation_point)
def testParseTruncated(self): # This test is only applicable for the Python implementation of the API. if api_implementation.Type() != 'python': return first_proto = unittest_pb2.TestAllTypes() test_util.SetAllFields(first_proto) serialized = first_proto.SerializeToString() for truncation_point in xrange(len(serialized) + 1): try: second_proto = unittest_pb2.TestAllTypes() unknown_fields = unittest_pb2.TestEmptyMessage() pos = second_proto._InternalParse(serialized, 0, truncation_point) # If we didn't raise an error then we read exactly the amount expected. self.assertEqual(truncation_point, pos) # Parsing to unknown fields should not throw if parsing to known fields # did not. try: pos2 = unknown_fields._InternalParse(serialized, 0, truncation_point) self.assertEqual(truncation_point, pos2) except message.DecodeError: self.fail('Parsing unknown fields failed when parsing known fields ' 'did not.') except message.DecodeError: # Parsing unknown fields should also fail. self.assertRaises(message.DecodeError, unknown_fields._InternalParse, serialized, 0, truncation_point)
def do_test(request): test_message = test_messages_proto3_pb2.TestAllTypes() response = conformance_pb2.ConformanceResponse() test_message = test_messages_proto3_pb2.TestAllTypes() try: if request.WhichOneof('payload') == 'protobuf_payload': try: test_message.ParseFromString(request.protobuf_payload) except message.DecodeError as e: response.parse_error = str(e) return response elif request.WhichOneof('payload') == 'json_payload': try: json_format.Parse(request.json_payload, test_message) except Exception as e: response.parse_error = str(e) return response else: raise ProtocolError("Request didn't have payload.") if request.requested_output_format == conformance_pb2.UNSPECIFIED: raise ProtocolError("Unspecified output format") elif request.requested_output_format == conformance_pb2.PROTOBUF: response.protobuf_payload = test_message.SerializeToString() elif request.requested_output_format == conformance_pb2.JSON: try: response.json_payload = json_format.MessageToJson(test_message) except Exception as e: response.serialize_error = str(e) return response except Exception as e: response.runtime_error = str(e) return response
def _AddMergeFromStringMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" def MergeFromString(self, serialized): length = len(serialized) try: if self._InternalParse(serialized, 0, length) != length: # The only reason _InternalParse would return early is if it # encountered an end-group tag. raise message_mod.DecodeError('Unexpected end-group tag.') except IndexError: raise message_mod.DecodeError('Truncated message.') except struct.error as e: raise message_mod.DecodeError(e) return length # Return this for legacy reasons. cls.MergeFromString = MergeFromString local_ReadTag = decoder.ReadTag local_SkipField = decoder.SkipField decoders_by_tag = cls._decoders_by_tag def InternalParse(self, buffer, pos, end): self._Modified() field_dict = self._fields while pos != end: (tag_bytes, new_pos) = local_ReadTag(buffer, pos) field_decoder = decoders_by_tag.get(tag_bytes) if field_decoder is None: new_pos = local_SkipField(buffer, new_pos, end, tag_bytes) if new_pos == -1: return pos pos = new_pos else: pos = field_decoder(buffer, new_pos, end, self, field_dict) return pos cls._InternalParse = InternalParse
def _fetch_current_frame(self): path = '{}/{}'.format(self.PLUGIN_LOGDIR, SUMMARY_FILENAME) try: frame = read_tensor_summary(path).astype(np.uint8) self.most_recent_frame = frame return frame except (message.DecodeError, IOError, tf.errors.NotFoundError): return self.most_recent_frame
def read_tensor_summary(path): with tf.gfile.Open(path, 'rb') as summary_file: summary_string = summary_file.read() if not summary_string: raise message.DecodeError('Empty summary.') summary_proto = tf.Summary() summary_proto.ParseFromString(summary_string) tensor_proto = summary_proto.value[0].tensor array = tf.make_ndarray(tensor_proto) return array
def _parse_response(proto, response): """Parses the content from a validator response Message. """ try: content = proto() content.ParseFromString(response.content) return content except (DecodeError, AttributeError): LOGGER.error('Validator response was not parsable: %s', response) raise errors.ValidatorResponseInvalid()
def handle(self, connection_id, message_content): response_proto = client_batch_submit_pb2.ClientBatchSubmitResponse def make_response(out_status): return HandlerResult( status=HandlerStatus.RETURN, message_out=response_proto(status=out_status), message_type=Message.CLIENT_BATCH_SUBMIT_RESPONSE) try: request = client_batch_submit_pb2.ClientBatchSubmitRequest() request.ParseFromString(message_content) for batch in request.batches: if batch.trace: LOGGER.debug("TRACE %s: %s", batch.header_signature, self.__class__.__name__) if not all( self._verifier.check_off_chain_batch_roles(batch) for batch in request.batches): return make_response(response_proto.INVALID_BATCH) if not all( self._verifier.is_batch_signer_authorized(batch) for batch in request.batches): return make_response(response_proto.INVALID_BATCH) except DecodeError: return make_response(response_proto.INTERNAL_ERROR) return HandlerResult(status=HandlerStatus.PASS)
def _AddMergeFromStringMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" def MergeFromString(self, serialized): length = len(serialized) try: if self._InternalParse(serialized, 0, length) != length: # The only reason _InternalParse would return early is if it # encountered an end-group tag. raise message_mod.DecodeError('Unexpected end-group tag.') except (IndexError, TypeError): # Now ord(buf[p:p+1]) == ord('') gets TypeError. raise message_mod.DecodeError('Truncated message.') except struct.error as e: raise message_mod.DecodeError(e) return length # Return this for legacy reasons. cls.MergeFromString = MergeFromString local_ReadTag = decoder.ReadTag local_SkipField = decoder.SkipField decoders_by_tag = cls._decoders_by_tag is_proto3 = message_descriptor.syntax == "proto3" def InternalParse(self, buffer, pos, end): self._Modified() field_dict = self._fields unknown_field_list = self._unknown_fields while pos != end: (tag_bytes, new_pos) = local_ReadTag(buffer, pos) field_decoder, field_desc = decoders_by_tag.get(tag_bytes, (None, None)) if field_decoder is None: value_start_pos = new_pos new_pos = local_SkipField(buffer, new_pos, end, tag_bytes) if new_pos == -1: return pos if not is_proto3: if not unknown_field_list: unknown_field_list = self._unknown_fields = [] unknown_field_list.append( (tag_bytes, buffer[value_start_pos:new_pos])) pos = new_pos else: pos = field_decoder(buffer, new_pos, end, self, field_dict) if field_desc: self._UpdateOneofState(field_desc) return pos cls._InternalParse = InternalParse
def _AddMergeFromStringMethod(message_descriptor, cls): """Helper for _AddMessageMethods().""" def MergeFromString(self, serialized): length = len(serialized) try: if self._InternalParse(serialized, 0, length) != length: # The only reason _InternalParse would return early is if it # encountered an end-group tag. raise message_mod.DecodeError('Unexpected end-group tag.') except (IndexError, TypeError): # Now ord(buf[p:p+1]) == ord('') gets TypeError. raise message_mod.DecodeError('Truncated message.') except struct.error, e: raise message_mod.DecodeError(e) return length # Return this for legacy reasons. cls.MergeFromString = MergeFromString local_ReadTag = decoder.ReadTag local_SkipField = decoder.SkipField decoders_by_tag = cls._decoders_by_tag def InternalParse(self, buffer, pos, end): self._Modified() field_dict = self._fields unknown_field_list = self._unknown_fields while pos != end: (tag_bytes, new_pos) = local_ReadTag(buffer, pos) field_decoder, field_desc = decoders_by_tag.get(tag_bytes, (None, None)) if field_decoder is None: value_start_pos = new_pos new_pos = local_SkipField(buffer, new_pos, end, tag_bytes) if new_pos == -1: return pos if not unknown_field_list: unknown_field_list = self._unknown_fields = [] unknown_field_list.append((tag_bytes, buffer[value_start_pos:new_pos])) pos = new_pos else: pos = field_decoder(buffer, new_pos, end, self, field_dict) if field_desc: self._UpdateOneofState(field_desc) return pos cls._InternalParse = InternalParse
def load_trace(self): filename = QtWidgets.QFileDialog.getOpenFileName()[0] filepath = Path(filename) if filepath.exists() and filepath.isfile(): trace = Trace(filename) try: # ==== Gui stuff self.loading_stat.setVisible(True) self.progressbar_loading.setVisible(True) self.progressbar_loading.reset() self.progressbar_loading.setMaximum(filepath.getsize()) newtable = QtWidgets.QTableWidget(self) newtable.verticalHeader().setVisible(False) newtable.setColumnCount(len(self.trace_header_table)) newtable.setHorizontalHeaderLabels(self.trace_header_table) newtable.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch) newtable.currentItemChanged.connect(self.update_instruction_informations) newtable.itemDoubleClicked.connect(self.go_to_instruction) index = self.traces_tab.addTab(newtable, filepath.name) id = self.parent.add_trace(trace) self.id_map[index] = id self.index_map[index] = newtable self.traces_tab.setCurrentIndex(index) # ===== total_instr = 0 nb_row = 0 current_size = 0 for chk, sz_chk, i, j, sz in trace.parse_file_generator(filename): total_instr += j-i current_size += sz self.loading_stat.setText("Chunk nb:"+str(chk)+" | Instr nb:"+str(total_instr)) self.loading_stat.adjustSize() self.progressbar_loading.setValue(current_size) newtable.setRowCount(nb_row+sz_chk) self.add_chunk_trace_table(newtable, trace, i, nb_row) nb_row += sz_chk newtable.scrollToBottom() self.trace_switch(index) # ===== Gui stuff newtable.scrollToTop() self.loading_stat.setVisible(False) self.progressbar_loading.setVisible(False) # ============ except DecodeError: print "Fail to parse the given trace" else: print "File not existing or not a file"
def submit_batches(self, request): """Accepts a binary encoded BatchList and submits it to the validator. Request: body: octet-stream BatchList of one or more Batches Response: status: - 202: Batches submitted and pending link: /batches or /batch_statuses link for submitted batches """ timer_ctx = self._post_batches_total_time.time() self._post_batches_count.inc() # Parse request if request.headers['Content-Type'] != 'application/octet-stream': LOGGER.debug( 'Submission headers had wrong Content-Type: %s', request.headers['Content-Type']) self._post_batches_error.inc() raise errors.SubmissionWrongContentType() body = await request.read() if not body: LOGGER.debug('Submission contained an empty body') self._post_batches_error.inc() raise errors.NoBatchesSubmitted() try: batch_list = BatchList() batch_list.ParseFromString(body) except DecodeError: LOGGER.debug('Submission body could not be decoded: %s', body) self._post_batches_error.inc() raise errors.BadProtobufSubmitted() # Query validator error_traps = [error_handlers.BatchInvalidTrap] validator_query = client_batch_submit_pb2.ClientBatchSubmitRequest( batches=batch_list.batches) with self._post_batches_validator_time.time(): await self._query_validator( Message.CLIENT_BATCH_SUBMIT_REQUEST, client_batch_submit_pb2.ClientBatchSubmitResponse, validator_query, error_traps) # Build response envelope id_string = ','.join(b.header_signature for b in batch_list.batches) status = 202 link = self._build_url(request, path='/batch_statuses', id=id_string) retval = self._wrap_response( request, metadata={'link': link}, status=status) timer_ctx.stop() return retval