• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

llnl / dftracer-utils / 28356348514

29 Jun 2026 07:40AM UTC coverage: 52.174% (-0.1%) from 52.278%
28356348514

Pull #83

github

web-flow
Merge 278203630 into 2efed6649
Pull Request #83: refactor and improve code QoL

37276 of 92891 branches covered (40.13%)

Branch coverage included in aggregate %.

671 of 1173 new or added lines in 58 files covered. (57.2%)

66 existing lines in 30 files now uncovered.

33619 of 42991 relevant lines covered (78.2%)

20387.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.42
/src/dftracer/utils/python/py_runtime_mixin.h
1
#ifndef DFTRACER_UTILS_PYTHON_PY_RUNTIME_MIXIN_H
2
#define DFTRACER_UTILS_PYTHON_PY_RUNTIME_MIXIN_H
3

4
#include <Python.h>
5
#include <dftracer/utils/python/runtime.h>
6

7
#include <string>
8

9
// Shared implementation for utility objects whose layout is exactly:
10
//     typedef struct { PyObject_HEAD PyObject *runtime_obj; } XObject;
11
// Each binding kept re-rolling an identical runtime-resolution / tp_new /
12
// tp_dealloc / tp_init quartet; these templates single-source it.
13

14
// Resolve the backing Runtime: the explicitly-bound one, else the default.
15
template <typename T>
16
dftracer::utils::Runtime *resolve_runtime(T *self) {
120✔
17
    if (self->runtime_obj)
120!
NEW
18
        return ((RuntimeObject *)self->runtime_obj)->runtime.get();
×
19
    return get_default_runtime();
120✔
20
}
60✔
21

22
template <typename T>
23
PyObject *runtime_backed_new(PyTypeObject *type, PyObject *, PyObject *) {
120✔
24
    T *self = (T *)type->tp_alloc(type, 0);
120✔
25
    if (self) self->runtime_obj = NULL;
120✔
26
    return (PyObject *)self;
120✔
27
}
28

29
template <typename T>
30
void runtime_backed_dealloc(T *self) {
120✔
31
    Py_XDECREF(self->runtime_obj);
120✔
32
    Py_TYPE(self)->tp_free((PyObject *)self);
120✔
33
}
120✔
34

35
// Parse an optional `runtime=` kwarg (a Runtime instance, an object exposing a
36
// `_native` Runtime, or None) and bind it into self->runtime_obj.
37
template <typename T>
38
int runtime_backed_init(T *self, PyObject *args, PyObject *kwds) {
120✔
39
    static const char *kwlist[] = {"runtime", NULL};
40
    PyObject *runtime_arg = NULL;
120✔
41
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", (char **)kwlist,
120!
42
                                     &runtime_arg)) {
NEW
43
        return -1;
×
44
    }
45
    if (runtime_arg && runtime_arg != Py_None) {
120!
NEW
46
        if (PyObject_TypeCheck(runtime_arg, &RuntimeType)) {
×
NEW
47
            Py_INCREF(runtime_arg);
×
NEW
48
            self->runtime_obj = runtime_arg;
×
49
        } else {
NEW
50
            PyObject *native = PyObject_GetAttrString(runtime_arg, "_native");
×
NEW
51
            if (native && PyObject_TypeCheck(native, &RuntimeType)) {
×
NEW
52
                self->runtime_obj = native;
×
53
            } else {
NEW
54
                Py_XDECREF(native);
×
NEW
55
                PyErr_SetString(PyExc_TypeError,
×
56
                                "runtime must be a Runtime instance or None");
NEW
57
                return -1;
×
58
            }
59
        }
60
    }
61
    return 0;
120✔
62
}
60✔
63

64
// Run a blocking C++ body with the GIL released, translating any C++ exception
65
// into a Python RuntimeError. Returns true on success; on failure the Python
66
// error is set and the caller should return its error sentinel (NULL or -1).
67
// The body must not touch Python objects (the GIL is not held while it runs).
68
template <typename F>
69
bool run_blocking(F &&body) {
106✔
70
    std::string error_msg;
106✔
71
    Py_BEGIN_ALLOW_THREADS try { body(); } catch (const std::exception &e) {
106!
NEW
72
        error_msg = e.what();
×
NEW
73
    } catch (...) {
×
NEW
74
        error_msg = "unknown C++ exception";
×
75
    }
×
76
    Py_END_ALLOW_THREADS if (!error_msg.empty()) {
106!
NEW
77
        PyErr_SetString(PyExc_RuntimeError, error_msg.c_str());
×
NEW
78
        return false;
×
79
    }
80
    return true;
106✔
81
}
106✔
82

83
#endif  // DFTRACER_UTILS_PYTHON_PY_RUNTIME_MIXIN_H
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc