1 # Copyright 2017 gRPC authors.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 class InitialMetadataFlags:
19 used_mask = GRPC_INITIAL_METADATA_USED_MASK
20 wait_for_ready = GRPC_INITIAL_METADATA_WAIT_FOR_READY
21 wait_for_ready_explicitly_set = GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET
24 _Metadatum = collections.namedtuple('_Metadatum', ('key', 'value',))
27 cdef void _store_c_metadata(
28 metadata, grpc_metadata **c_metadata, size_t *c_count) except *:
33 metadatum_count = len(metadata)
34 if metadatum_count == 0:
38 c_count[0] = metadatum_count
39 c_metadata[0] = <grpc_metadata *>gpr_malloc(
40 metadatum_count * sizeof(grpc_metadata))
41 for index, (key, value) in enumerate(metadata):
42 encoded_key = _encode(key)
43 encoded_value = value if encoded_key[-4:] == b'-bin' else _encode(value)
44 if not isinstance(encoded_value, bytes):
45 raise TypeError('Binary metadata key="%s" expected bytes, got %s' % (
49 c_metadata[0][index].key = _slice_from_bytes(encoded_key)
50 c_metadata[0][index].value = _slice_from_bytes(encoded_value)
53 cdef void _release_c_metadata(grpc_metadata *c_metadata, int count) except *:
55 for index in range(count):
56 grpc_slice_unref(c_metadata[index].key)
57 grpc_slice_unref(c_metadata[index].value)
61 cdef tuple _metadatum(grpc_slice key_slice, grpc_slice value_slice):
62 cdef bytes key = _slice_bytes(key_slice)
63 cdef bytes value = _slice_bytes(value_slice)
64 return <tuple>_Metadatum(
65 _decode(key), value if key[-4:] == b'-bin' else _decode(value))
68 cdef tuple _metadata(grpc_metadata_array *c_metadata_array):
71 c_metadata_array.metadata[index].key,
72 c_metadata_array.metadata[index].value)
73 for index in range(c_metadata_array.count))