Skip to content

Commit 24eb6fd

Browse files
committed
Move disable_plugin to Python
1 parent 987ceb9 commit 24eb6fd

File tree

3 files changed

+21
-47
lines changed

3 files changed

+21
-47
lines changed

coverage/collector.py

+11
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ def _start_tracer(self):
256256
if hasattr(tracer, 'should_start_context'):
257257
tracer.should_start_context = self.should_start_context
258258
tracer.switch_context = self.switch_context
259+
if hasattr(tracer, 'disable_plugin'):
260+
tracer.disable_plugin = self.disable_plugin
259261

260262
fn = tracer.start()
261263
self.tracers.append(tracer)
@@ -381,6 +383,15 @@ def switch_context(self, new_context):
381383
context = new_context
382384
self.covdata.set_context(context)
383385

386+
def disable_plugin(self, disposition):
387+
"""Disable the plugin mentioned in `disposition`."""
388+
file_tracer = disposition.file_tracer
389+
plugin = file_tracer._coverage_plugin
390+
plugin_name = plugin._coverage_plugin_name
391+
self.warn("Disabling plug-in {!r} due to previous exception".format(plugin_name))
392+
plugin._coverage_enabled = False
393+
disposition.trace = False
394+
384395
def cached_mapped_file(self, filename):
385396
"""A locally cached version of file names mapped through file_mapper."""
386397
key = (type(filename), filename)

coverage/ctracer/tracer.c

+9-47
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ CTracer_dealloc(CTracer *self)
103103
Py_XDECREF(self->should_start_context);
104104
Py_XDECREF(self->switch_context);
105105
Py_XDECREF(self->context);
106+
Py_XDECREF(self->disable_plugin);
106107

107108
DataStack_dealloc(&self->stats, &self->data_stack);
108109
if (self->data_stacks) {
@@ -570,66 +571,24 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
570571
static void
571572
CTracer_disable_plugin(CTracer *self, PyObject * disposition)
572573
{
573-
PyObject * file_tracer = NULL;
574-
PyObject * plugin = NULL;
575-
PyObject * plugin_name = NULL;
576-
PyObject * msg = NULL;
577-
PyObject * ignored = NULL;
578-
574+
PyObject * ret;
579575
PyErr_Print();
580576

581-
file_tracer = PyObject_GetAttr(disposition, str_file_tracer);
582-
if (file_tracer == NULL) {
583-
goto error;
584-
}
585-
if (file_tracer == Py_None) {
586-
/* This shouldn't happen... */
587-
goto ok;
588-
}
589-
plugin = PyObject_GetAttr(file_tracer, str__coverage_plugin);
590-
if (plugin == NULL) {
591-
goto error;
592-
}
593-
plugin_name = PyObject_GetAttr(plugin, str__coverage_plugin_name);
594-
if (plugin_name == NULL) {
595-
goto error;
596-
}
597-
msg = MyText_FromFormat(
598-
"Disabling plug-in '%s' due to previous exception",
599-
MyText_AsString(plugin_name)
600-
);
601-
if (msg == NULL) {
602-
goto error;
603-
}
604577
STATS( self->stats.pycalls++; )
605-
ignored = PyObject_CallFunctionObjArgs(self->warn, msg, NULL);
606-
if (ignored == NULL) {
607-
goto error;
608-
}
609-
610-
/* Disable the plugin for future files, and stop tracing this file. */
611-
if (PyObject_SetAttr(plugin, str__coverage_enabled, Py_False) < 0) {
612-
goto error;
613-
}
614-
if (PyObject_SetAttr(disposition, str_trace, Py_False) < 0) {
578+
ret = PyObject_CallFunctionObjArgs(self->disable_plugin, disposition, NULL);
579+
if (ret == NULL) {
615580
goto error;
616581
}
582+
Py_DECREF(ret);
617583

618-
goto ok;
584+
return;
619585

620586
error:
621587
/* This function doesn't return a status, so if an error happens, print it,
622588
* but don't interrupt the flow. */
623589
/* PySys_WriteStderr is nicer, but is not in the public API. */
624590
fprintf(stderr, "Error occurred while disabling plug-in:\n");
625591
PyErr_Print();
626-
627-
ok:
628-
Py_XDECREF(file_tracer);
629-
Py_XDECREF(plugin);
630-
Py_XDECREF(plugin_name);
631-
Py_XDECREF(msg);
632-
Py_XDECREF(ignored);
633592
}
634593

635594

@@ -1121,6 +1080,9 @@ CTracer_members[] = {
11211080
{ "switch_context", T_OBJECT, offsetof(CTracer, switch_context), 0,
11221081
PyDoc_STR("Function for switching to a new context.") },
11231082

1083+
{ "disable_plugin", T_OBJECT, offsetof(CTracer, disable_plugin), 0,
1084+
PyDoc_STR("Function for disabling a plugin.") },
1085+
11241086
{ NULL }
11251087
};
11261088

coverage/ctracer/tracer.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef struct CTracer {
2727
PyObject * trace_arcs;
2828
PyObject * should_start_context;
2929
PyObject * switch_context;
30+
PyObject * disable_plugin;
3031

3132
/* Has the tracer been started? */
3233
BOOL started;

0 commit comments

Comments
 (0)