Ejemplo n.º 1
0
    def _build_methods(self):
        assert len(self.methods) == 0
        methods_temp = {}
        for i in range(capi.c_num_methods(self.space, self)):
            idx = capi.c_method_index_at(self.space, self, i)
            pyname = helper.map_operator_name(self.space,
                capi.c_method_name(self.space, self, idx),
                capi.c_method_num_args(self.space, self, idx),
                capi.c_method_result_type(self.space, self, idx))
            cppmethod = self._make_cppfunction(pyname, idx)
            methods_temp.setdefault(pyname, []).append(cppmethod)
        # the following covers the case where the only kind of operator[](idx)
        # returns are the ones that produce non-const references; these can be
        # used for __getitem__ just as much as for __setitem__, though
        if not "__getitem__" in methods_temp:
            try:
                for m in methods_temp["__setitem__"]:
                    cppmethod = self._make_cppfunction("__getitem__", m.index)
                    methods_temp.setdefault("__getitem__", []).append(cppmethod)
            except KeyError:
                pass          # just means there's no __setitem__ either

        # create the overload methods from the method sets
        for pyname, methods in methods_temp.iteritems():
            CPPMethodSort(methods).sort()
            overload = W_CPPOverload(self.space, self, methods[:])
            self.methods[pyname] = overload
Ejemplo n.º 2
0
 def _find_methods(self):
     num_methods = capi.c_num_methods(self)
     args_temp = {}
     for i in range(num_methods):
         method_name = capi.c_method_name(self, i)
         pymethod_name = helper.map_operator_name(
             method_name, capi.c_method_num_args(self, i), capi.c_method_result_type(self, i)
         )
         if not pymethod_name in self.methods:
             cppfunction = self._make_cppfunction(i)
             overload = args_temp.setdefault(pymethod_name, [])
             overload.append(cppfunction)
     for name, functions in args_temp.iteritems():
         overload = W_CPPOverload(self.space, self, functions[:])
         self.methods[name] = overload
Ejemplo n.º 3
0
def test_operator_mapping():
    assert helper.map_operator_name("operator[]", 1, "const int&")  == "__getitem__"
    assert helper.map_operator_name("operator[]", 1, "int&")        == "__setitem__"

    assert helper.map_operator_name("operator()", 1, "")  == "__call__"
    assert helper.map_operator_name("operator%", 1, "")   == "__mod__"
    assert helper.map_operator_name("operator**", 1, "")  == "__pow__"
    assert helper.map_operator_name("operator<<", 1, "")  == "__lshift__"
    assert helper.map_operator_name("operator|", 1, "")   == "__or__"

    assert helper.map_operator_name("operator*", 1, "") == "__mul__"
    assert helper.map_operator_name("operator*", 0, "") == "__deref__"

    assert helper.map_operator_name("operator+", 1, "") == "__add__"
    assert helper.map_operator_name("operator+", 0, "") == "__pos__"

    assert helper.map_operator_name("func", 0, "")        == "func"
    assert helper.map_operator_name("some_method", 0, "") == "some_method"
Ejemplo n.º 4
0
def test_operator_mapping():
    assert helper.map_operator_name(None, "operator[]", 1, "const int&")  == "__getitem__"
    assert helper.map_operator_name(None, "operator[]", 1, "int&")        == "__setitem__"

    assert helper.map_operator_name(None, "operator()", 1, "")  == "__call__"
    assert helper.map_operator_name(None, "operator%", 1, "")   == "__mod__"
    assert helper.map_operator_name(None, "operator**", 1, "")  == "__pow__"
    assert helper.map_operator_name(None, "operator<<", 1, "")  == "__lshift__"
    assert helper.map_operator_name(None, "operator|", 1, "")   == "__or__"

    assert helper.map_operator_name(None, "operator*", 1, "") == "__mul__"
    assert helper.map_operator_name(None, "operator*", 0, "") == "__deref__"

    assert helper.map_operator_name(None, "operator+", 1, "") == "__add__"
    assert helper.map_operator_name(None, "operator+", 0, "") == "__pos__"

    assert helper.map_operator_name(None, "func", 0, "")        == "func"
    assert helper.map_operator_name(None, "some_method", 0, "") == "some_method"