1616#include < boost/detail/binary_search.hpp>
1717#include < boost/python/self.hpp>
1818#include < boost/python/dict.hpp>
19+ #include < boost/python/str.hpp>
1920#include < boost/bind.hpp>
2021#include < functional>
2122#include < vector>
@@ -192,16 +193,16 @@ namespace objects
192193 }
193194
194195
195- static PyGetSetDef instance_getsets[] = {
196- {" __dict__" , instance_get_dict, instance_set_dict, NULL },
197- {0 }
198- };
196+ static PyGetSetDef instance_getsets[] = {
197+ {" __dict__" , instance_get_dict, instance_set_dict, NULL },
198+ {0 }
199+ };
199200
200201
201- static PyMemberDef instance_members[] = {
202- {" __weakref__" , T_OBJECT, offsetof (instance<>, weakrefs), 0 },
203- {0 }
204- };
202+ static PyMemberDef instance_members[] = {
203+ {" __weakref__" , T_OBJECT, offsetof (instance<>, weakrefs), 0 },
204+ {0 }
205+ };
205206
206207 static PyTypeObject class_type_object = {
207208 PyObject_HEAD_INIT (0 ) // &class_metatype_object)
@@ -275,6 +276,20 @@ static PyMemberDef instance_members[] = {
275276 return 0 ;
276277 }
277278
279+ object module_prefix ()
280+ {
281+ object result (
282+ PyObject_IsInstance (scope ().ptr (), upcast<PyObject>(&PyModule_Type))
283+ ? object (scope ().attr (" __name__" ))
284+ : api::getattr (scope (), " __module__" , str ())
285+ );
286+
287+ if (result)
288+ result += ' .' ;
289+
290+ return result;
291+ }
292+
278293 namespace
279294 {
280295 // Find a registered class object corresponding to id. Return a
@@ -303,21 +318,17 @@ static PyMemberDef instance_members[] = {
303318 }
304319 return result;
305320 }
306- }
307321
308- // class_base constructor
309- //
310- // name - the name of the new Python class
311- //
312- // num_types - one more than the number of declared bases
313- //
314- // types - array of python::type_info, the first item
315- // corresponding to the class being created, and the
316- // rest corresponding to its declared bases.
317- //
318-
319- namespace
320- {
322+ // class_base constructor
323+ //
324+ // name - the name of the new Python class
325+ //
326+ // num_types - one more than the number of declared bases
327+ //
328+ // types - array of python::type_info, the first item
329+ // corresponding to the class being created, and the
330+ // rest corresponding to its declared bases.
331+ //
321332 inline object
322333 new_class (char const * name, std::size_t num_types, class_id const * const types)
323334 {
@@ -336,17 +347,8 @@ static PyMemberDef instance_members[] = {
336347 PyTuple_SET_ITEM (bases.get (), i - 1 , upcast<PyObject>(c.release ()));
337348 }
338349
339- object module_name (
340- PyObject_IsInstance (scope ().ptr (), upcast<PyObject>(&PyModule_Type))
341- ? object (scope ().attr (" __name__" ))
342- : api::getattr (scope (), " __module__" , object (" " ))
343- );
344-
345- if (module_name)
346- module_name += ' .' ;
347-
348350 // Call the class metatype to create a new class
349- object result = object (class_metatype ())(module_name + name, bases, dict ());
351+ object result = object (class_metatype ())(module_prefix () + name, bases, dict ());
350352 assert (PyType_IsSubtype (result.ptr ()->ob_type , &PyType_Type));
351353
352354 if (scope ().ptr () != Py_None)
0 commit comments