Example #1
0
    def __init__(self, cppclass, value_type, begin_method='begin', end_method='end', iterator_type='iterator', is_mapping=False):
        """
        :param cppclass: the L{CppClass} object that receives the container traits

        :param value_type: a ReturnValue of the element type: note,
        for mapping containers, value_type is a tuple with two
        ReturnValue's: (key, element).
        """
        self.cppclass = cppclass
        self.begin_method = begin_method
        self.end_method = end_method
        self.iterator_type = iterator_type

        self.iter_pytype = PyTypeObject()
        self._iter_pystruct = None

        if is_mapping:
            (key_type, value_type) = value_type
            self.key_type = utils.eval_retval(key_type, self)
            self.value_type = utils.eval_retval(value_type, self)
        else:
            self.key_type = None
            self.value_type = utils.eval_retval(value_type, self)
Example #2
0
    def __init__(
        self,
        function_name,
        return_value,
        parameters,
        docstring=None,
        unblock_threads=None,
        template_parameters=(),
        custom_name=None,
        deprecated=False,
        foreign_cpp_namespace=None,
        throw=(),
    ):
        """
        :param function_name: name of the C function
        :param return_value: the function return value
        :type return_value: L{ReturnValue}
        :param parameters: the function parameters
        :type parameters: list of L{Parameter}

        :param custom_name: an alternative name to give to this
           function at python-side; if omitted, the name of the
           function in the python module will be the same name as the
           function in C++ (minus namespace).

        :param deprecated: deprecation state for this API:
          - False: Not deprecated
          - True: Deprecated
          - "message": Deprecated, and deprecation warning contains the given message

        :param foreign_cpp_namespace: if set, the function is assumed
          to belong to the given C++ namespace, regardless of the C++
          namespace of the python module it will be added to.

        :param throw: list of C++ exceptions that the function may throw
        :type throw: list of L{CppException}
        """
        self.stack_where_defined = traceback.extract_stack()

        if unblock_threads is None:
            unblock_threads = settings.unblock_threads

        ## backward compatibility check
        if isinstance(return_value, str) and isinstance(function_name, ReturnValue):
            warnings.warn(
                "Function has changed API; see the API documentation (but trying to correct...)",
                DeprecationWarning,
                stacklevel=2,
            )
            function_name, return_value = return_value, function_name

        if return_value is None:
            return_value = ReturnValue.new("void")

        return_value = utils.eval_retval(return_value, self)
        parameters = [utils.eval_param(param, self) for param in parameters]

        super(Function, self).__init__(
            return_value,
            parameters,
            parse_error_return="return NULL;",
            error_return="return NULL;",
            unblock_threads=unblock_threads,
        )
        self.deprecated = deprecated
        self.foreign_cpp_namespace = foreign_cpp_namespace
        self._module = None
        function_name = utils.ascii(function_name)
        self.function_name = function_name
        self.wrapper_base_name = None
        self.wrapper_actual_name = None
        self.docstring = docstring
        self.self_parameter_pystruct = None
        self.template_parameters = template_parameters
        self.custom_name = custom_name
        self.mangled_name = utils.get_mangled_name(function_name, self.template_parameters)
        for t in throw:
            assert isinstance(t, CppException)
        self.throw = list(throw)
        self.custodians_and_wards = []  # list of (custodian, ward, postcall)
        cppclass_typehandlers.scan_custodians_and_wards(self)
    def __init__(
        self,
        method_name,
        return_value,
        parameters,
        is_static=False,
        template_parameters=(),
        is_virtual=None,
        is_const=False,
        unblock_threads=None,
        is_pure_virtual=False,
        custom_template_method_name=None,
        visibility="public",
        custom_name=None,
        deprecated=False,
        docstring=None,
        throw=(),
    ):
        """
        Create an object the generates code to wrap a C++ class method.

        :param return_value: the method return value
        :type  return_value: L{ReturnValue}

        :param method_name: name of the method

        :param parameters: the method parameters
        :type parameters: list of :class:`pybindgen.typehandlers.base.Parameter`

        :param is_static: whether it is a static method

        :param template_parameters: optional list of template parameters needed to invoke the method
        :type template_parameters: list of strings, each element a template parameter expression

        :param is_virtual: whether the method is virtual (pure or not)

        :param is_const: whether the method has a const modifier on it

        :param unblock_threads: whether to release the Python GIL
            around the method call or not.  If None or omitted, use
            global settings.  Releasing the GIL has a small
            performance penalty, but is recommended if the method is
            expected to take considerable time to complete, because
            otherwise no other Python thread is allowed to run until
            the method completes.

        :param is_pure_virtual: whether the method is defined as "pure
          virtual", i.e. virtual method with no default implementation
          in the class being wrapped.

        :param custom_name: alternate name to give to the method, in python side.

        :param custom_template_method_name: (deprecated) same as parameter 'custom_name'.

        :param visibility: visibility of the method within the C++ class
        :type visibility: a string (allowed values are 'public', 'protected', 'private')

        :param deprecated: deprecation state for this API:
          - False: Not deprecated
          - True: Deprecated
          - "message": Deprecated, and deprecation warning contains the given message

        :param throw: list of C++ exceptions that the function may throw
        :type throw: list of L{CppException}
        """
        self.stack_where_defined = traceback.extract_stack()

        ## backward compatibility check
        if isinstance(return_value, str) and isinstance(method_name, ReturnValue):
            warnings.warn(
                "CppMethod has changed API; see the API documentation (but trying to correct...)",
                DeprecationWarning,
                stacklevel=2,
            )
            method_name, return_value = return_value, method_name

        # bug 399870
        if is_virtual is None:
            is_virtual = is_pure_virtual

        if return_value is None:
            return_value = ReturnValue.new("void")

        if unblock_threads is None:
            unblock_threads = settings.unblock_threads

        assert visibility in ["public", "protected", "private"]
        self.visibility = visibility
        self.method_name = method_name
        self.is_static = is_static
        self.is_virtual = is_virtual
        self.is_pure_virtual = is_pure_virtual
        self.is_const = is_const
        self.template_parameters = template_parameters

        self.custom_name = custom_name or custom_template_method_name

        # self.static_decl = True
        self._class = None
        self._helper_class = None
        self.docstring = docstring
        self.wrapper_base_name = None
        self.wrapper_actual_name = None
        self.return_value = None
        self.parameters = None

        return_value = utils.eval_retval(return_value, self)
        parameters = [utils.eval_param(param, self) for param in parameters]

        super(CppMethod, self).__init__(
            return_value, parameters, "return NULL;", "return NULL;", unblock_threads=unblock_threads
        )
        self.deprecated = deprecated

        for t in throw:
            assert isinstance(t, CppException)
        self.throw = list(throw)

        self.custodians_and_wards = []  # list of (custodian, ward, postcall)
        cppclass_typehandlers.scan_custodians_and_wards(self)
Example #4
0
    def __init__(self,
                 name,
                 value_type,
                 container_type,
                 outer_class=None,
                 custom_name=None):
        """
        :param name: C++ type name of the container, e.g. std::vector<int> or MyIntList

        :param value_type: a ReturnValue of the element type: note,
            for mapping containers, value_type is a tuple with two
            ReturnValue's: (key, element).

        :param container_type: a string with the type of container,
            one of 'list', 'deque', 'queue', 'priority_queue',
            'vector', 'stack', 'set', 'multiset', 'hash_set',
            'hash_multiset', 'map'

        :param outer_class: if the type is defined inside a class, must be a reference to the outer class
        :type outer_class: None or L{CppClass}

        :param custom_name: alternative name to register with in the Python module

        """
        if '<' in name or '::' in name:
            self.name = utils.mangle_name(name)
            self.full_name = name
            self._full_name_is_definitive = True
        else:
            self._full_name_is_definitive = False
            self.full_name = None
            self.name = name
        self._module = None
        self.outer_class = outer_class
        self.mangled_name = None
        self.mangled_full_name = None
        self.container_traits = container_traits_list[container_type]
        self.custom_name = custom_name
        self._pystruct = None
        self.pytypestruct = "***GIVE ME A NAME***"
        self.pytype = PyTypeObject()

        self.iter_pytypestruct = "***GIVE ME A NAME***"
        self.iter_pytype = PyTypeObject()
        self._iter_pystruct = None

        if self.container_traits.is_mapping:
            (key_type, value_type) = value_type
            self.key_type = utils.eval_retval(key_type, self)
            self.value_type = utils.eval_retval(value_type, self)
        else:
            self.key_type = None
            self.value_type = utils.eval_retval(value_type, self)
        self.python_to_c_converter = None

        if name != 'dummy':
            ## register type handlers

            class ThisContainerParameter(ContainerParameter):
                """Register this C++ container as pass-by-value parameter"""
                CTYPES = []
                container_type = self

            self.ThisContainerParameter = ThisContainerParameter
            try:
                param_type_matcher.register(name, self.ThisContainerParameter)
            except ValueError:
                pass

            class ThisContainerRefParameter(ContainerRefParameter):
                """Register this C++ container as pass-by-value parameter"""
                CTYPES = []
                container_type = self

            self.ThisContainerRefParameter = ThisContainerRefParameter
            try:
                param_type_matcher.register(name + '&',
                                            self.ThisContainerRefParameter)
            except ValueError:
                pass

            class ThisContainerPtrParameter(ContainerPtrParameter):
                """Register this C++ container as pass-by-ptr parameter"""
                CTYPES = []
                container_type = self

            self.ThisContainerPtrParameter = ThisContainerPtrParameter
            try:
                param_type_matcher.register(name + '*',
                                            self.ThisContainerPtrParameter)
            except ValueError:
                pass

            class ThisContainerReturn(ContainerReturnValue):
                """Register this C++ container as value return"""
                CTYPES = []
                container_type = self

            self.ThisContainerReturn = ThisContainerReturn
            self.ThisContainerRefReturn = ThisContainerReturn
            try:
                return_type_matcher.register(name, self.ThisContainerReturn)
                return_type_matcher.register(name, self.ThisContainerRefReturn)
            except ValueError:
                pass
    def __init__(self, name, value_type, container_type, outer_class=None, custom_name=None):
        """
        :param name: C++ type name of the container, e.g. std::vector<int> or MyIntList

        :param value_type: a ReturnValue of the element type: note,
            for mapping containers, value_type is a tuple with two
            ReturnValue's: (key, element).

        :param container_type: a string with the type of container,
            one of 'list', 'deque', 'queue', 'priority_queue',
            'vector', 'stack', 'set', 'multiset', 'hash_set',
            'hash_multiset', 'map'

        :param outer_class: if the type is defined inside a class, must be a reference to the outer class
        :type outer_class: None or L{CppClass}

        :param custom_name: alternative name to register with in the Python module

        """
        if '<' in name or '::' in name:
            self.name = utils.mangle_name(name)
            self.full_name = name
            self._full_name_is_definitive = True
        else:
            self._full_name_is_definitive = False
            self.full_name = None
            self.name = name
        self._module = None
        self.outer_class = outer_class
        self.mangled_name = None
        self.mangled_full_name = None
        self.container_traits = container_traits_list[container_type]
        self.custom_name = custom_name
        self._pystruct = None
        self.pytypestruct = "***GIVE ME A NAME***"
        self.pytype = PyTypeObject()

        self.iter_pytypestruct = "***GIVE ME A NAME***"
        self.iter_pytype = PyTypeObject()
        self._iter_pystruct = None

        if self.container_traits.is_mapping:
            (key_type, value_type) = value_type
            self.key_type = utils.eval_retval(key_type, self)
            self.value_type = utils.eval_retval(value_type, self)
        else:
            self.key_type = None
            self.value_type = utils.eval_retval(value_type, self)
        self.python_to_c_converter = None

        if name != 'dummy':
            ## register type handlers

            class ThisContainerParameter(ContainerParameter):
                """Register this C++ container as pass-by-value parameter"""
                CTYPES = []
                container_type = self
            self.ThisContainerParameter = ThisContainerParameter
            try:
                param_type_matcher.register(name, self.ThisContainerParameter)
            except ValueError:
                pass

            class ThisContainerRefParameter(ContainerRefParameter):
                """Register this C++ container as pass-by-value parameter"""
                CTYPES = []
                container_type = self
            self.ThisContainerRefParameter = ThisContainerRefParameter
            try:
                param_type_matcher.register(name+'&', self.ThisContainerRefParameter)
            except ValueError:
                pass

            class ThisContainerPtrParameter(ContainerPtrParameter):
                """Register this C++ container as pass-by-ptr parameter"""
                CTYPES = []
                container_type = self
            self.ThisContainerPtrParameter = ThisContainerPtrParameter
            try:
                param_type_matcher.register(name+'*', self.ThisContainerPtrParameter)
            except ValueError:
                pass

            class ThisContainerReturn(ContainerReturnValue):
                """Register this C++ container as value return"""
                CTYPES = []
                container_type = self
            self.ThisContainerReturn = ThisContainerReturn
            self.ThisContainerRefReturn = ThisContainerReturn
            try:
                return_type_matcher.register(name, self.ThisContainerReturn)
                return_type_matcher.register(name, self.ThisContainerRefReturn)
            except ValueError:
                pass
Example #6
0
    def __init__(self,
                 function_name,
                 return_value,
                 parameters,
                 docstring=None,
                 unblock_threads=None,
                 template_parameters=(),
                 custom_name=None,
                 deprecated=False,
                 foreign_cpp_namespace=None,
                 throw=()):
        """
        :param function_name: name of the C function
        :param return_value: the function return value
        :type return_value: L{ReturnValue}
        :param parameters: the function parameters
        :type parameters: list of L{Parameter}

        :param custom_name: an alternative name to give to this
           function at python-side; if omitted, the name of the
           function in the python module will be the same name as the
           function in C++ (minus namespace).

        :param deprecated: deprecation state for this API:
          - False: Not deprecated
          - True: Deprecated
          - "message": Deprecated, and deprecation warning contains the given message

        :param foreign_cpp_namespace: if set, the function is assumed
          to belong to the given C++ namespace, regardless of the C++
          namespace of the python module it will be added to.

        :param throw: list of C++ exceptions that the function may throw
        :type throw: list of L{CppException}
        """
        self.stack_where_defined = traceback.extract_stack()

        if unblock_threads is None:
            unblock_threads = settings.unblock_threads

        ## backward compatibility check
        if isinstance(return_value, str) and isinstance(
                function_name, ReturnValue):
            warnings.warn(
                "Function has changed API; see the API documentation (but trying to correct...)",
                DeprecationWarning,
                stacklevel=2)
            function_name, return_value = return_value, function_name

        if return_value is None:
            return_value = ReturnValue.new('void')

        return_value = utils.eval_retval(return_value, self)
        parameters = [utils.eval_param(param, self) for param in parameters]

        super(Function, self).__init__(return_value,
                                       parameters,
                                       parse_error_return="return NULL;",
                                       error_return="return NULL;",
                                       unblock_threads=unblock_threads)
        self.deprecated = deprecated
        self.foreign_cpp_namespace = foreign_cpp_namespace
        self._module = None
        function_name = utils.ascii(function_name)
        self.function_name = function_name
        self.wrapper_base_name = None
        self.wrapper_actual_name = None
        self.docstring = docstring
        self.self_parameter_pystruct = None
        self.template_parameters = template_parameters
        self.custom_name = custom_name
        self.mangled_name = utils.get_mangled_name(function_name,
                                                   self.template_parameters)
        for t in throw:
            assert isinstance(t, CppException)
        self.throw = list(throw)
        self.custodians_and_wards = []  # list of (custodian, ward, postcall)
        cppclass_typehandlers.scan_custodians_and_wards(self)
Example #7
0
    def __init__(self,
                 method_name,
                 return_value,
                 parameters,
                 is_static=False,
                 template_parameters=(),
                 is_virtual=None,
                 is_const=False,
                 unblock_threads=None,
                 is_pure_virtual=False,
                 custom_template_method_name=None,
                 visibility='public',
                 custom_name=None,
                 deprecated=False,
                 docstring=None,
                 throw=()):
        """
        Create an object the generates code to wrap a C++ class method.

        :param return_value: the method return value
        :type  return_value: L{ReturnValue}

        :param method_name: name of the method

        :param parameters: the method parameters
        :type parameters: list of :class:`pybindgen.typehandlers.base.Parameter`

        :param is_static: whether it is a static method

        :param template_parameters: optional list of template parameters needed to invoke the method
        :type template_parameters: list of strings, each element a template parameter expression

        :param is_virtual: whether the method is virtual (pure or not)

        :param is_const: whether the method has a const modifier on it

        :param unblock_threads: whether to release the Python GIL
            around the method call or not.  If None or omitted, use
            global settings.  Releasing the GIL has a small
            performance penalty, but is recommended if the method is
            expected to take considerable time to complete, because
            otherwise no other Python thread is allowed to run until
            the method completes.

        :param is_pure_virtual: whether the method is defined as "pure
          virtual", i.e. virtual method with no default implementation
          in the class being wrapped.

        :param custom_name: alternate name to give to the method, in python side.

        :param custom_template_method_name: (deprecated) same as parameter 'custom_name'.

        :param visibility: visibility of the method within the C++ class
        :type visibility: a string (allowed values are 'public', 'protected', 'private')

        :param deprecated: deprecation state for this API:
          - False: Not deprecated
          - True: Deprecated
          - "message": Deprecated, and deprecation warning contains the given message

        :param throw: list of C++ exceptions that the function may throw
        :type throw: list of L{CppException}
        """
        self.stack_where_defined = traceback.extract_stack()

        ## backward compatibility check
        if isinstance(return_value, str) and isinstance(
                method_name, ReturnValue):
            warnings.warn(
                "CppMethod has changed API; see the API documentation (but trying to correct...)",
                DeprecationWarning,
                stacklevel=2)
            method_name, return_value = return_value, method_name

        # bug 399870
        if is_virtual is None:
            is_virtual = is_pure_virtual

        if return_value is None:
            return_value = ReturnValue.new('void')

        if unblock_threads is None:
            unblock_threads = settings.unblock_threads

        assert visibility in ['public', 'protected', 'private']
        self.visibility = visibility
        self.method_name = method_name
        self.is_static = is_static
        self.is_virtual = is_virtual
        self.is_pure_virtual = is_pure_virtual
        self.is_const = is_const
        self.template_parameters = template_parameters

        self.custom_name = (custom_name or custom_template_method_name)

        #self.static_decl = True
        self._class = None
        self._helper_class = None
        self.docstring = docstring
        self.wrapper_base_name = None
        self.wrapper_actual_name = None
        self.return_value = None
        self.parameters = None

        return_value = utils.eval_retval(return_value, self)
        parameters = [utils.eval_param(param, self) for param in parameters]

        super(CppMethod, self).__init__(return_value,
                                        parameters,
                                        "return NULL;",
                                        "return NULL;",
                                        unblock_threads=unblock_threads)
        self.deprecated = deprecated

        for t in throw:
            assert isinstance(t, CppException)
        self.throw = list(throw)

        self.custodians_and_wards = []  # list of (custodian, ward, postcall)
        cppclass_typehandlers.scan_custodians_and_wards(self)