[C++-sig] staticmethod diffs
Nikolay Mladenov
nickm at sitius.com
Fri Jan 17 20:09:46 CET 2003
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030117/bee9634d/attachment.htm>
-------------- next part --------------
Index: boost/python/class.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/python/class.hpp,v
retrieving revision 1.64
diff -c -r1.64 class.hpp
*** boost/python/class.hpp 20 Dec 2002 00:04:39 -0000 1.64
--- boost/python/class.hpp 17 Jan 2003 17:19:19 -0000
***************
*** 352,357 ****
--- 352,362 ----
return *this;
}
+ self& staticmethod(char const* name)
+ {
+ make_method_static(name);
+ return *this;
+ }
private: // helper functions
inline void register_() const;
Index: boost/python/object/class.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/python/object/class.hpp,v
retrieving revision 1.31
diff -c -r1.31 class.hpp
*** boost/python/object/class.hpp 3 Oct 2002 12:41:22 -0000 1.31
--- boost/python/object/class.hpp 17 Jan 2003 17:19:19 -0000
***************
*** 48,53 ****
--- 48,55 ----
// Implementation detail. Hiding this in the private section would
// require use of template friend declarations.
void enable_pickling(bool getstate_manages_dict);
+
+ void make_method_static(const char *method_name);
};
}}} // namespace boost::python::objects
Index: libs/python/doc/v2/class.html
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/doc/v2/class.html,v
retrieving revision 1.19
diff -c -r1.19 class.html
*** libs/python/doc/v2/class.html 14 Nov 2002 02:09:42 -0000 1.19
--- libs/python/doc/v2/class.html 17 Jan 2003 17:19:24 -0000
***************
*** 242,247 ****
--- 242,250 ----
template <class Fn, class A1, class A2, class A3>
class_& def(char const* name, Fn fn, A1 const&, A2 const&, A3 const&);
+ // declaring method as static
+ class_& staticmethod(char const* name);
+
// exposing operators
template <<i>unspecified</i>>
class_& def(<a href=
***************
*** 476,481 ****
--- 479,501 ----
<dt><b>Returns:</b> <code>*this</code></dt>
</dl>
+ <pre>
+ class_& staticmethod(char const* name);
+ </pre>
+
+ <dl class="function-semantics">
+ <dt><b>Requires:</b> <code>name</code> is an <a href=
+ "definitions.html#ntbs">ntbs</a> which conforms to Python's <a href=
+ "http://www.python.org/doc/current/ref/identifiers.html">identifier
+ naming rules</a>, and corresponds to a method which has already been completely overloaded.</dt>
+
+ <dt><b>Effects:</b> Specifies that the corresponding method is static and
+ therefore object instance will not be passed to it.</dt>
+
+ <dt><b>Returns:</b> <code>*this</code></dt>
+ </dl>
+ <br>
+
<pre>
template <<i>unspecified</i>>
class_& def(<a href=
Index: libs/python/src/object/class.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/src/object/class.cpp,v
retrieving revision 1.48
diff -c -r1.48 class.cpp
*** libs/python/src/object/class.cpp 25 Nov 2002 01:57:56 -0000 1.48
--- libs/python/src/object/class.cpp 17 Jan 2003 17:19:25 -0000
***************
*** 440,445 ****
--- 440,477 ----
}
}
+ namespace {
+
+ PyObject* callable_check(PyObject* callable)
+ {
+ if(PyCallable_Check(
+ expect_non_null(callable)
+ ))
+ return callable;
+
+ ::PyErr_Format(
+ PyExc_TypeError
+ , "staticmethod expects callable object; got an object of type %s, which is not callable"
+ , callable->ob_type->tp_name
+ );
+ throw_error_already_set();
+
+ return 0;
+ }
+
+ }
+ void class_base::make_method_static(const char * method_name)
+ {
+ dict d(handle<>(borrowed(downcast<PyTypeObject>(this->ptr())->tp_dict)));
+
+ object method(d[method_name]);
+
+ this->attr(method_name) = object(
+ handle<>(
+ PyStaticMethod_New(callable_check(method.ptr()) )
+ ));
+ }
+
BOOST_PYTHON_DECL type_handle registered_class_object(class_id id)
{
return query_class(id);
Index: libs/python/src/object/function.cpp
===================================================================
RCS file: /cvsroot/boost/boost/libs/python/src/object/function.cpp,v
retrieving revision 1.26
diff -c -r1.26 function.cpp
*** libs/python/src/object/function.cpp 21 Dec 2002 22:12:31 -0000 1.26
--- libs/python/src/object/function.cpp 17 Jan 2003 17:19:25 -0000
***************
*** 12,17 ****
--- 12,18 ----
#include <boost/python/object_attributes.hpp>
#include <boost/python/args.hpp>
#include <boost/python/refcount.hpp>
+ #include <boost/python/extract.hpp>
#include <algorithm>
#include <cstring>
***************
*** 262,267 ****
--- 263,279 ----
{
if (existing->ob_type == &function_type)
new_func->add_overload(existing);
+ else if (existing->ob_type == &PyStaticMethod_Type){
+ //this can probably be made a lot better
+ std::string name_space_name = extract<std::string>(name_space.attr("__name__"));
+ ::PyErr_Format(
+ PyExc_RuntimeError
+ , "Boost.Python - All overloads of \"%s\" must be exported before calling \'class_(\"%s\").staticmethod\'"
+ , name_
+ , name_space_name.c_str()
+ );
+ throw_error_already_set();
+ }
}
else if (is_binary_operator(name_))
{
More information about the Cplusplus-sig
mailing list