|
3 | 3 | // copyright notice appears in all copies. This software is provided |
4 | 4 | // "as is" without express or implied warranty, and with no claim as |
5 | 5 | // to its suitability for any purpose. |
| 6 | +#include <boost/python/scope.hpp> |
6 | 7 | #include <boost/python/converter/registry.hpp> |
7 | 8 | #include <boost/python/object/class.hpp> |
8 | 9 | #include <boost/python/object/find_instance.hpp> |
9 | 10 | #include <boost/python/object/pickle_support.hpp> |
10 | 11 | #include <boost/python/detail/map_entry.hpp> |
11 | 12 | #include <boost/python/object.hpp> |
| 13 | +#include <boost/python/object_protocol.hpp> |
12 | 14 | #include <boost/detail/binary_search.hpp> |
13 | 15 | #include <boost/python/self.hpp> |
14 | 16 | #include <boost/bind.hpp> |
@@ -264,17 +266,30 @@ namespace objects |
264 | 266 | PyTuple_SET_ITEM(bases.get(), i - 1, upcast<PyObject>(c.release())); |
265 | 267 | } |
266 | 268 |
|
| 269 | + object module_name = PyObject_IsInstance(scope().ptr(), upcast<PyObject>(&PyModule_Type)) |
| 270 | + ? scope().attr("__name__") |
| 271 | + : getattr(scope(), "__module__", object("")) |
| 272 | + ; |
| 273 | + |
| 274 | + if (module_name) |
| 275 | + module_name += '.'; |
| 276 | + |
267 | 277 | // Build the (name, bases, dict) tuple for creating the new class |
268 | 278 | handle<> args(PyTuple_New(3)); |
269 | | - PyTuple_SET_ITEM(args.get(), 0, incref(python::object(name).ptr())); |
| 279 | + PyTuple_SET_ITEM(args.get(), 0, incref((module_name + name).ptr())); |
270 | 280 | PyTuple_SET_ITEM(args.get(), 1, bases.release()); |
271 | 281 | handle<> d(PyDict_New()); |
272 | 282 | PyTuple_SET_ITEM(args.get(), 2, d.release()); |
273 | 283 |
|
274 | 284 | // Call the class metatype to create a new class |
275 | 285 | PyObject* c = PyObject_CallObject(upcast<PyObject>(class_metatype().get()), args.get()); |
276 | 286 | assert(PyType_IsSubtype(c->ob_type, &PyType_Type)); |
277 | | - return object(python::detail::new_reference(c)); |
| 287 | + object result = object(python::detail::new_reference(c)); |
| 288 | + |
| 289 | + if (scope().ptr() != Py_None) |
| 290 | + scope().attr(name) = result; |
| 291 | + |
| 292 | + return result; |
278 | 293 | } |
279 | 294 | } |
280 | 295 |
|
|
0 commit comments