# HG changeset patch # User Mike Miller # Date 1491233420 25200 # Node ID 668fcb0f68ef6361156f8a4836cb8b73a5ba0179 # Parent 132fec49e438f257a68f856ae6cfc19e6df71911 Fix extraction of class name for Python types without a module (fixes issue #79) * oct-py-util.cc (pytave::py_object_class_name): Handle object types with __module__ set to None, just return the value of __name__. * __py_struct_from_dict__.cc (F__py_class_name__): Add test case. diff -r 132fec49e438 -r 668fcb0f68ef __py_struct_from_dict__.cc --- a/__py_struct_from_dict__.cc Sun Apr 02 16:24:02 2017 -0700 +++ b/__py_struct_from_dict__.cc Mon Apr 03 08:30:20 2017 -0700 @@ -65,6 +65,9 @@ %!assert (__py_class_name__ (pyeval ("()")), "tuple") %!assert (__py_class_name__ (pyeval ("__import__('array').array('d')")), "array.array") +%% Test an anonymous class with its __module__ property set to None +%!assert (__py_class_name__ (pyeval ("[[t() for t.__module__ in (None,)][0] for t in (type('foo', (), {}),)][0]")), "foo") + %!error __py_class_name__ () %!error __py_class_name__ (1) %!error __py_class_name__ (1, 2) diff -r 132fec49e438 -r 668fcb0f68ef oct-py-util.cc --- a/oct-py-util.cc Sun Apr 02 16:24:02 2017 -0700 +++ b/oct-py-util.cc Mon Apr 03 08:30:20 2017 -0700 @@ -139,14 +139,14 @@ else name = PyObject_GetAttrString (type, "__name__"); - std::string mod_str = mod ? extract_py_str (mod) : ""; + std::string mod_str = (mod && mod != Py_None) ? extract_py_str (mod) : ""; std::string name_str = name ? extract_py_str (name) : ""; Py_DECREF (type); Py_XDECREF (mod); Py_XDECREF (name); - if (mod_str == py_builtins_module_name ()) + if (mod_str.empty () || mod_str == py_builtins_module_name ()) retval = name_str; else retval = mod_str + "." + name_str;