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

llnl / dftracer-utils / 28496595030

01 Jul 2026 05:50AM UTC coverage: 50.727% (-1.6%) from 52.278%
28496595030

Pull #83

github

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

31872 of 80367 branches covered (39.66%)

Branch coverage included in aggregate %.

770 of 1591 new or added lines in 85 files covered. (48.4%)

5070 existing lines in 182 files now uncovered.

32742 of 47009 relevant lines covered (69.65%)

9887.52 hits per line

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

29.03
/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) {
60✔
20
    if (self->runtime_obj)
60!
NEW
21
        return ((RuntimeObject *)self->runtime_obj)->runtime.get();
×
22
    return get_default_runtime();
60✔
23
}
60✔
24

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

32
template <typename T>
33
void runtime_backed_dealloc(T *self) {
60✔
34
    Py_XDECREF(self->runtime_obj);
60✔
35
    Py_TYPE(self)->tp_free((PyObject *)self);
60✔
36
}
60✔
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) {
60✔
42
    static const char *kwlist[] = {"runtime", NULL};
43
    PyObject *runtime_arg = NULL;
60✔
44
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", (char **)kwlist,
60!
45
                                     &runtime_arg)) {
NEW
46
        return -1;
×
47
    }
48
    if (runtime_arg && runtime_arg != Py_None) {
60!
NEW
49
        if (PyObject_TypeCheck(runtime_arg, &RuntimeType)) {
×
NEW
50
            Py_INCREF(runtime_arg);
×
NEW
51
            self->runtime_obj = runtime_arg;
×
NEW
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;
×
NEW
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
        }
NEW
63
    }
×
64
    return 0;
60✔
65
}
60✔
66

67
// Run a blocking C++ body with the GIL released
68
template <typename F>
69
bool run_blocking(F &&body) {
53✔
70
    bool failed = false;
53✔
71
    std::string error_msg;
53✔
72
    PyObject *exc_type = nullptr;  // pointer read only; no Python API off-GIL
53✔
73
    Py_BEGIN_ALLOW_THREADS try {
53!
74
        body();
53!
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";
×
NEW
89
    }
×
90
    Py_END_ALLOW_THREADS if (failed) {
53!
NEW
91
        if (exc_type == nullptr) exc_type = g_dft_error;
×
NEW
92
        PyErr_SetString(exc_type ? exc_type : PyExc_RuntimeError,
×
NEW
93
                        error_msg.c_str());
×
NEW
94
        return false;
×
95
    }
96
    return true;
53✔
97
}
53✔
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