• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 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
15cimport cpython
16
17
18# TODO(https://github.com/grpc/grpc/issues/15662): Reform this.
19cdef void* _copy_pointer(void* pointer):
20  return pointer
21
22
23# TODO(https://github.com/grpc/grpc/issues/15662): Reform this.
24cdef void _destroy_pointer(void* pointer):
25  pass
26
27
28cdef int _compare_pointer(void* first_pointer, void* second_pointer):
29  if first_pointer < second_pointer:
30    return -1
31  elif first_pointer > second_pointer:
32    return 1
33  else:
34    return 0
35
36
37cdef class _GrpcArgWrapper:
38
39  cdef grpc_arg arg
40
41
42cdef tuple _wrap_grpc_arg(grpc_arg arg):
43  wrapped = _GrpcArgWrapper()
44  wrapped.arg = arg
45  return ("grpc.python._cygrpc._GrpcArgWrapper", wrapped)
46
47
48cdef grpc_arg _unwrap_grpc_arg(tuple wrapped_arg):
49  cdef _GrpcArgWrapper wrapped = wrapped_arg[1]
50  return wrapped.arg
51
52
53cdef class _ArgumentProcessor:
54
55  cdef void c(self, argument, grpc_arg_pointer_vtable *vtable, references):
56    key, value = argument
57    cdef bytes encoded_key = _encode(key)
58    if encoded_key is not key:
59      references.append(encoded_key)
60    self.c_argument.key = encoded_key
61    if isinstance(value, int):
62      self.c_argument.type = GRPC_ARG_INTEGER
63      self.c_argument.value.integer = value
64    elif isinstance(value, (bytes, str, unicode,)):
65      self.c_argument.type = GRPC_ARG_STRING
66      encoded_value = _encode(value)
67      if encoded_value is not value:
68        references.append(encoded_value)
69      self.c_argument.value.string = encoded_value
70    elif isinstance(value, _GrpcArgWrapper):
71      self.c_argument = (<_GrpcArgWrapper>value).arg
72    elif hasattr(value, '__int__'):
73      # Pointer objects must override __int__() to return
74      # the underlying C address (Python ints are word size). The
75      # lifecycle of the pointer is fixed to the lifecycle of the
76      # python object wrapping it.
77      self.c_argument.type = GRPC_ARG_POINTER
78      self.c_argument.value.pointer.vtable = vtable
79      self.c_argument.value.pointer.address = <void*>(<intptr_t>int(value))
80    else:
81      raise TypeError(
82          'Expected int, bytes, or behavior, got {}'.format(type(value)))
83
84
85cdef class _ArgumentsProcessor:
86
87  def __cinit__(self, arguments):
88    self._arguments = () if arguments is None else tuple(arguments)
89    self._argument_processors = []
90    self._references = []
91
92  cdef grpc_channel_args *c(self, grpc_arg_pointer_vtable *vtable):
93    self._c_arguments.arguments_length = len(self._arguments)
94    if self._c_arguments.arguments_length == 0:
95      return NULL
96    else:
97      self._c_arguments.arguments = <grpc_arg *>gpr_malloc(
98          self._c_arguments.arguments_length * sizeof(grpc_arg))
99      for index, argument in enumerate(self._arguments):
100        argument_processor = _ArgumentProcessor()
101        argument_processor.c(argument, vtable, self._references)
102        self._c_arguments.arguments[index] = argument_processor.c_argument
103        self._argument_processors.append(argument_processor)
104      return &self._c_arguments
105
106  cdef un_c(self):
107    if self._arguments:
108      gpr_free(self._c_arguments.arguments)
109