1# Copyright 2016 gRPC authors.
2#
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
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
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.
14
15import logging
16
17logging.basicConfig()
18_LOGGER = logging.getLogger(__name__)
19
20# This function will ascii encode unicode string inputs if neccesary.
21# In Python3, unicode strings are the default str type.
22cdef bytes str_to_bytes(object s):
23  if s is None or isinstance(s, bytes):
24    return s
25  elif isinstance(s, unicode):
26    return s.encode('ascii')
27  else:
28    raise TypeError('Expected bytes, str, or unicode, not {}'.format(type(s)))
29
30
31# TODO(https://github.com/grpc/grpc/issues/13782): It would be nice for us if
32# the type of metadata that we accept were exactly the same as the type of
33# metadata that we deliver to our users (so "str" for this function's
34# parameter rather than "object"), but would it be nice for our users? Right
35# now we haven't yet heard from enough users to know one way or another.
36cdef bytes _encode(object string_or_none):
37  if string_or_none is None:
38    return b''
39  elif isinstance(string_or_none, (bytes,)):
40    return <bytes>string_or_none
41  elif isinstance(string_or_none, (unicode,)):
42    return string_or_none.encode('ascii')
43  else:
44    raise TypeError('Expected str, not {}'.format(type(string_or_none)))
45
46
47cdef str _decode(bytes bytestring):
48    if isinstance(bytestring, (str,)):
49        return <str>bytestring
50    else:
51        try:
52            return bytestring.decode('utf8')
53        except UnicodeDecodeError:
54            _LOGGER.exception('Invalid encoding on %s', bytestring)
55            return bytestring.decode('latin1')
56