Skip to content

Commit 4c6f40f

Browse files
jvansantenstefanseefeld
authored andcommitted
Add generated docstrings to property fget/fset
1 parent d1910f3 commit 4c6f40f

File tree

6 files changed

+63
-22
lines changed

6 files changed

+63
-22
lines changed

include/boost/python/class.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,23 @@ class class_ : public objects::class_base
372372
{
373373
typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
374374

375-
return this->make_fn_impl(
375+
return objects::add_doc(
376+
this->make_fn_impl(
376377
detail::unwrap_wrapper((W*)0)
377378
, f, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer<F>()
378-
);
379+
), NULL);
379380
}
380381

381382
template <class F>
382383
object make_setter(F f)
383384
{
384385
typedef typename api::is_object_operators<F>::type is_obj_or_proxy;
385386

386-
return this->make_fn_impl(
387+
return objects::add_doc(
388+
this->make_fn_impl(
387389
detail::unwrap_wrapper((W*)0)
388390
, f, is_obj_or_proxy(), (int*)0, detail::is_data_member_pointer<F>()
389-
);
391+
), NULL);
390392
}
391393

392394
template <class T, class F>

include/boost/python/object/add_to_namespace.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ BOOST_PYTHON_DECL void add_to_namespace(
1818
BOOST_PYTHON_DECL void add_to_namespace(
1919
object const& name_space, char const* name, object const& attribute, char const* doc);
2020

21+
BOOST_PYTHON_DECL object const& add_doc(object const& attribute, char const* doc);
22+
2123
}}} // namespace boost::python::objects
2224

2325
#endif // ADD_TO_NAMESPACE_DWA200286_HPP

include/boost/python/object/function.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ struct BOOST_PYTHON_DECL function : PyObject
3535
static void add_to_namespace(
3636
object const& name_space, char const* name, object const& attribute, char const* doc);
3737

38+
static object const& add_doc(object const& attribute, char const* doc);
39+
3840
object const& doc() const;
3941
void doc(object const& x);
4042

src/object/function.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,30 @@ namespace detail
419419
extern char cpp_signature_tag[];
420420
}
421421

422+
object const& function::add_doc(object const& attribute, char const* doc)
423+
{
424+
str _doc;
425+
426+
if (docstring_options::show_py_signatures_)
427+
{
428+
_doc += str(const_cast<const char*>(detail::py_signature_tag));
429+
}
430+
if (doc != 0 && docstring_options::show_user_defined_)
431+
_doc += doc;
432+
433+
if (docstring_options::show_cpp_signatures_)
434+
{
435+
_doc += str(const_cast<const char*>(detail::cpp_signature_tag));
436+
}
437+
if(_doc)
438+
{
439+
object mutable_attribute(attribute);
440+
mutable_attribute.attr("__doc__")= _doc;
441+
}
442+
443+
return attribute;
444+
}
445+
422446
void function::add_to_namespace(
423447
object const& name_space, char const* name_, object const& attribute, char const* doc)
424448
{
@@ -545,24 +569,7 @@ void function::add_to_namespace(
545569
"C++ signature:", f->signature(true)));
546570
}
547571
*/
548-
str _doc;
549-
550-
if (docstring_options::show_py_signatures_)
551-
{
552-
_doc += str(const_cast<const char*>(detail::py_signature_tag));
553-
}
554-
if (doc != 0 && docstring_options::show_user_defined_)
555-
_doc += doc;
556-
557-
if (docstring_options::show_cpp_signatures_)
558-
{
559-
_doc += str(const_cast<const char*>(detail::cpp_signature_tag));
560-
}
561-
if(_doc)
562-
{
563-
object mutable_attribute(attribute);
564-
mutable_attribute.attr("__doc__")= _doc;
565-
}
572+
add_doc(attribute, doc);
566573
}
567574

568575
BOOST_PYTHON_DECL void add_to_namespace(
@@ -577,6 +584,11 @@ BOOST_PYTHON_DECL void add_to_namespace(
577584
function::add_to_namespace(name_space, name, attribute, doc);
578585
}
579586

587+
BOOST_PYTHON_DECL object const& add_doc(object const& attribute, char const* doc)
588+
{
589+
return function::add_doc(attribute, doc);
590+
}
591+
580592

581593
namespace
582594
{

test/properties.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ BOOST_PYTHON_MODULE(properties_ext)
6464
class_<X>("X", init<int>() )
6565
//defining read only property
6666
.add_property( "value_r", &X::get_value )
67+
.add_property( "value_r_f", make_function(&X::get_value) )
6768
.add_property( "value_r_ds", &X::get_value, "value_r_ds is read-only")
6869
//defining read \ write property
6970
.add_property( "value_rw", &X::get_value, &X::set_value )

test/properties.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
>>> x1.value_r
2121
1
2222
23+
>>> x1.value_r_f
24+
1
25+
2326
value read - write
2427
>>> x1.value_rw
2528
1
@@ -84,8 +87,27 @@ class instance count from object:
8487
8588
>>> assert properties.X.value_rw_ds.__doc__ == "value_rw_ds is read-write"
8689
90+
>>> properties.X.value_r_f.fget.__doc__.strip().split("\\n")[0]
91+
'None( (properties_ext.X)arg1) -> int :'
92+
93+
>>> properties.X.value_rw_ds.fget.__doc__.strip().split("\\n")[0]
94+
'None( (properties_ext.X)arg1) -> int :'
95+
96+
>>> properties.X.value_rw_ds.fset.__doc__.strip().split("\\n")[0]
97+
'None( (properties_ext.X)arg1, (int)arg2) -> None :'
98+
99+
>>> properties.X.value_rw_ds.fget.__doc__.strip().split("\\n")[0]
100+
'None( (properties_ext.X)arg1) -> int :'
101+
102+
>>> properties.X.value_direct.fset.__doc__.strip().split("\\n")[0]
103+
'None( (properties_ext.X)arg1, (int)arg2) -> None :'
104+
105+
>>> properties.X.value_direct.fget.__doc__.strip().split("\\n")[0]
106+
'None( (properties_ext.X)arg1) -> int :'
87107
"""
88108

109+
# FIXME: cases to cover: pointer-to-member, preconstructed function
110+
89111
#import sys; sys.path.append(r'P:\Actimize4.0\smart_const\py_smart_const___Win32_Debug')
90112
import properties_ext as properties
91113

0 commit comments

Comments
 (0)