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

llnl / dftracer-utils / 28423703495

30 Jun 2026 05:59AM UTC coverage: 51.998% (-0.3%) from 52.278%
28423703495

Pull #83

github

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

37282 of 93303 branches covered (39.96%)

Branch coverage included in aggregate %.

801 of 1525 new or added lines in 78 files covered. (52.52%)

98 existing lines in 37 files now uncovered.

33674 of 43157 relevant lines covered (78.03%)

20306.85 hits per line

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

27.74
/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/core/common/error.h>
6
#include <dftracer/utils/python/py_errors.h>
7
#include <dftracer/utils/python/runtime.h>
8

9
#include <stdexcept>
10
#include <string>
11

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

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

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

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

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

67
// Run a blocking C++ body with the GIL released
68
template <typename F>
69
bool run_blocking(F &&body) {
106✔
70
    bool failed = false;
106✔
71
    std::string error_msg;
106✔
72
    PyObject *exc_type = nullptr;  // pointer read only; no Python API off-GIL
106✔
73
    Py_BEGIN_ALLOW_THREADS try {
106!
74
        body();
106!
75
    } catch (const dftracer::utils::DFTUtilsException &e) {
53!
NEW
76
        failed = true;
×
NEW
77
        error_msg = e.what();
×
NEW
78
        exc_type = py_error_type_for(e.code());
×
NEW
79
    } catch (const std::invalid_argument &e) {
×
NEW
80
        failed = true;
×
NEW
81
        error_msg = e.what();
×
NEW
82
        exc_type = g_dft_value_error;
×
NEW
83
    } catch (const std::exception &e) {
×
NEW
84
        failed = true;
×
NEW
85
        error_msg = e.what();
×
NEW
86
    } catch (...) {
×
NEW
87
        failed = true;
×
NEW
88
        error_msg = "unknown C++ exception";
×
89
    }
×
90
    Py_END_ALLOW_THREADS if (failed) {
106!
NEW
91
        if (exc_type == nullptr) exc_type = g_dft_error;
×
NEW
92
        PyErr_SetString(exc_type ? exc_type : PyExc_RuntimeError,
×
93
                        error_msg.c_str());
NEW
94
        return false;
×
95
    }
96
    return true;
106✔
97
}
106✔
98

99
#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