arguments.pyx.pxi 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. cdef class _GrpcArgWrapper:
  15. cdef grpc_arg arg
  16. cdef tuple _wrap_grpc_arg(grpc_arg arg):
  17. wrapped = _GrpcArgWrapper()
  18. wrapped.arg = arg
  19. return ("grpc.python._cygrpc._GrpcArgWrapper", wrapped)
  20. cdef grpc_arg _unwrap_grpc_arg(tuple wrapped_arg):
  21. cdef _GrpcArgWrapper wrapped = wrapped_arg[1]
  22. return wrapped.arg
  23. cdef class _ChannelArg:
  24. cdef void c(self, argument, references) except *:
  25. key, value = argument
  26. cdef bytes encoded_key = _encode(key)
  27. if encoded_key is not key:
  28. references.append(encoded_key)
  29. self.c_argument.key = encoded_key
  30. if isinstance(value, int):
  31. self.c_argument.type = GRPC_ARG_INTEGER
  32. self.c_argument.value.integer = value
  33. elif isinstance(value, (bytes, str, unicode,)):
  34. self.c_argument.type = GRPC_ARG_STRING
  35. encoded_value = _encode(value)
  36. if encoded_value is not value:
  37. references.append(encoded_value)
  38. self.c_argument.value.string = encoded_value
  39. elif isinstance(value, _GrpcArgWrapper):
  40. self.c_argument = (<_GrpcArgWrapper>value).arg
  41. elif hasattr(value, '__int__'):
  42. # Pointer objects must override __int__() to return
  43. # the underlying C address (Python ints are word size). The
  44. # lifecycle of the pointer is fixed to the lifecycle of the
  45. # python object wrapping it.
  46. self.c_argument.type = GRPC_ARG_POINTER
  47. self.c_argument.value.pointer.vtable = &default_vtable
  48. self.c_argument.value.pointer.address = <void*>(<intptr_t>int(value))
  49. else:
  50. raise TypeError(
  51. 'Expected int, bytes, or behavior, got {}'.format(type(value)))
  52. cdef class _ChannelArgs:
  53. def __cinit__(self, arguments):
  54. self._arguments = () if arguments is None else tuple(arguments)
  55. self._channel_args = []
  56. self._references = []
  57. self._c_arguments.arguments_length = len(self._arguments)
  58. if self._c_arguments.arguments_length != 0:
  59. self._c_arguments.arguments = <grpc_arg *>gpr_malloc(
  60. self._c_arguments.arguments_length * sizeof(grpc_arg))
  61. for index, argument in enumerate(self._arguments):
  62. channel_arg = _ChannelArg()
  63. channel_arg.c(argument, self._references)
  64. self._c_arguments.arguments[index] = channel_arg.c_argument
  65. self._channel_args.append(channel_arg)
  66. cdef grpc_channel_args *c_args(self) except *:
  67. return &self._c_arguments
  68. def __dealloc__(self):
  69. if self._c_arguments.arguments != NULL:
  70. gpr_free(self._c_arguments.arguments)