Skip to content

Commit

Permalink
Add class name to module name, if available
Browse files Browse the repository at this point in the history
  • Loading branch information
bertwesarg committed Nov 20, 2023
1 parent 7ec85af commit a24dda1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions scorep/_instrumenters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def get_module_name(frame):
modulename = "numpy.__array_function__"
else:
modulename = "unkown"
typeobject = frame.f_locals.get("self", None)
if typeobject is not None:
return f"{modulename}.{type(typeobject).__name__}"
return modulename


Expand Down
21 changes: 20 additions & 1 deletion src/scorepy/pythonHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,36 @@
#include "pathUtils.hpp"
#include "pythoncapi_compat.h"

#include <sstream>
#include <stdlib.h>

namespace scorepy
{
std::string get_module_name(PyFrameObject& frame)
{
const char* self_name = nullptr;
PyObject* locals = PyFrame_GetLocals(&frame);
PyObject* self = PyDict_GetItemString(locals, "self");
if (self)
{
Py_INCREF(self);
PyTypeObject* type = Py_TYPE(self);
self_name = _PyType_Name(type);
Py_DECREF(self);
}
Py_DECREF(locals);

PyObject* globals = PyFrame_GetGlobals(&frame);
PyObject* module_name = PyDict_GetItemString(globals, "__name__");
Py_DECREF(globals);
if (module_name)
return std::move(std::string(compat::get_string_as_utf_8(module_name)));
{
std::stringstream result;
result << compat::get_string_as_utf_8(module_name);
if (self_name)
result << '.' << self_name;
return std::move(result).str();
}

// this is a NUMPY special situation, see NEP-18, and Score-P issue #63
// TODO: Use string_view/C-String to avoid creating 2 std::strings
Expand Down

0 comments on commit a24dda1

Please sign in to comment.