Author: Wim Lavrijsen <[email protected]>
Branch: reflex-support
Changeset: r54049:f343cf6ff013
Date: 2012-03-28 17:12 -0700
http://bitbucket.org/pypy/pypy/changeset/f343cf6ff013/
Log: put C++ signature in method doc-string
diff --git a/pypy/module/cppyy/capi/__init__.py
b/pypy/module/cppyy/capi/__init__.py
--- a/pypy/module/cppyy/capi/__init__.py
+++ b/pypy/module/cppyy/capi/__init__.py
@@ -285,6 +285,13 @@
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",
diff --git a/pypy/module/cppyy/include/capi.h b/pypy/module/cppyy/include/capi.h
--- a/pypy/module/cppyy/include/capi.h
+++ b/pypy/module/cppyy/include/capi.h
@@ -69,6 +69,7 @@
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);
diff --git a/pypy/module/cppyy/interp_cppyy.py
b/pypy/module/cppyy/interp_cppyy.py
--- a/pypy/module/cppyy/interp_cppyy.py
+++ b/pypy/module/cppyy/interp_cppyy.py
@@ -216,6 +216,9 @@
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)
@@ -299,6 +302,12 @@
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)
@@ -306,6 +315,7 @@
'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']),
)
diff --git a/pypy/module/cppyy/pythonify.py b/pypy/module/cppyy/pythonify.py
--- a/pypy/module/cppyy/pythonify.py
+++ b/pypy/module/cppyy/pythonify.py
@@ -57,6 +57,7 @@
def method(self, *args):
return cppol.call(self, *args)
method.__name__ = meth_name
+ method.__doc__ = cppol.signature()
return method
diff --git a/pypy/module/cppyy/src/reflexcwrapper.cxx
b/pypy/module/cppyy/src/reflexcwrapper.cxx
--- a/pypy/module/cppyy/src/reflexcwrapper.cxx
+++ b/pypy/module/cppyy/src/reflexcwrapper.cxx
@@ -11,6 +11,7 @@
#include "Reflex/TypeTemplate.h"
#include <string>
+#include <sstream>
#include <utility>
#include <vector>
@@ -355,6 +356,22 @@
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);
diff --git a/pypy/module/cppyy/test/fragile.h b/pypy/module/cppyy/test/fragile.h
--- a/pypy/module/cppyy/test/fragile.h
+++ b/pypy/module/cppyy/test/fragile.h
@@ -70,4 +70,9 @@
extern I gI;
+class J {
+public:
+ int method1(int, double) { return 0; }
+};
+
} // namespace fragile
diff --git a/pypy/module/cppyy/test/test_fragile.py
b/pypy/module/cppyy/test/test_fragile.py
--- a/pypy/module/cppyy/test/test_fragile.py
+++ b/pypy/module/cppyy/test/test_fragile.py
@@ -158,3 +158,22 @@
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)"
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit