Commits

wlav committed f343cf6

put C++ signature in method doc-string

  • Participants
  • Parent commits dbf598d
  • Branches reflex-support

Comments (0)

Files changed (7)

pypy/module/cppyy/capi/__init__.py

     compilation_info=backend.eci)
 def c_method_arg_default(cppscope, method_index, arg_index):
     return charp2str_free(_c_method_arg_default(cppscope, method_index, arg_index))
+_c_method_signature = rffi.llexternal(
+    "cppyy_method_signature",
+    [C_SCOPE, rffi.INT], rffi.CCHARP,
+    threadsafe=threadsafe,
+    compilation_info=backend.eci)
+def c_method_signature(cppscope, method_index):
+    return charp2str_free(_c_method_signature(cppscope, method_index))
 
 c_get_method = rffi.llexternal(
     "cppyy_get_method",

pypy/module/cppyy/include/capi.h

     int cppyy_method_req_args(cppyy_scope_t scope, int method_index);
     char* cppyy_method_arg_type(cppyy_scope_t scope, int method_index, int arg_index);
     char* cppyy_method_arg_default(cppyy_scope_t scope, int method_index, int arg_index);
+    char* cppyy_method_signature(cppyy_scope_t scope, int method_index);
 
     cppyy_method_t cppyy_get_method(cppyy_scope_t scope, int method_index);
 

pypy/module/cppyy/interp_cppyy.py

             conv.free_argument(rffi.cast(capi.C_OBJECT, arg_i))
         capi.c_deallocate_function_args(args)
 
+    def signature(self):
+        return capi.c_method_signature(self.cpptype.handle, self.method_index)
+
     def __repr__(self):
         return "CPPFunction(%s, %s, %r, %s)" % (
             self.cpptype, self.method_index, self.executor, self.arg_defs)
 
         raise OperationError(self.space.w_TypeError, self.space.wrap(errmsg))
 
+    def signature(self):
+        sig = self.functions[0].signature()
+        for i in range(1, len(self.functions)):
+            sig += '\n'+self.functions[i].signature()
+        return self.space.wrap(sig)
+
     def __repr__(self):
         return "W_CPPOverload(%s, %s)" % (self.func_name, self.functions)
 
     'CPPOverload',
     is_static = interp2app(W_CPPOverload.is_static, unwrap_spec=['self']),
     call = interp2app(W_CPPOverload.call, unwrap_spec=['self', W_Root, 'args_w']),
+    signature = interp2app(W_CPPOverload.signature, unwrap_spec=['self']),
 )
 
 

pypy/module/cppyy/pythonify.py

     def method(self, *args):
         return cppol.call(self, *args)
     method.__name__ = meth_name
+    method.__doc__ = cppol.signature()
     return method
 
 

pypy/module/cppyy/src/reflexcwrapper.cxx

 #include "Reflex/TypeTemplate.h"
 
 #include <string>
+#include <sstream>
 #include <utility>
 #include <vector>
 
     return cppstring_to_cstring(dflt);
 }
 
+char* cppyy_method_signature(cppyy_scope_t handle, int method_index) {
+    Reflex::Scope s = scope_from_handle(handle);
+    Reflex::Member m = s.FunctionMemberAt(method_index);
+    Reflex::Type mt = m.TypeOf();
+    std::ostringstream sig;
+    sig << s.Name(Reflex::SCOPED) << "::" << m.Name() << "(";
+    int nArgs = m.FunctionParameterSize();
+    for (int iarg = 0; iarg < nArgs; ++iarg) {
+        sig << mt.FunctionParameterAt(iarg).Name(Reflex::SCOPED|Reflex::QUALIFIED);
+        if (iarg != nArgs-1)
+            sig << ", ";
+    }
+    sig << ")" << std::ends;
+    return cppstring_to_cstring(sig.str());
+}
+
 cppyy_method_t cppyy_get_method(cppyy_scope_t handle, int method_index) {
     Reflex::Scope s = scope_from_handle(handle);
     Reflex::Member m = s.FunctionMemberAt(method_index);

pypy/module/cppyy/test/fragile.h

 
 extern I gI;
 
+class J {
+public:
+    int method1(int, double) { return 0; }
+};
+
 } // namespace fragile

pypy/module/cppyy/test/test_fragile.py

 
         g = cppyy.gbl.fragile.gI
         assert not g
+
+    def test10_documentation(self):
+        """Check contents of documentation"""
+
+        import cppyy
+
+        assert cppyy.gbl.fragile == cppyy.gbl.fragile
+        fragile = cppyy.gbl.fragile
+
+        d = fragile.D()
+        try:
+            d.check(None)         # raises TypeError
+            assert 0
+        except TypeError, e:
+            assert "TypeError: wrong number of arguments" in str(e)
+
+        j = fragile.J()
+        assert fragile.J.method1.__doc__ == j.method1.__doc__
+        assert j.method1.__doc__ == "fragile::J::method1(int, double)"