summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-11-24 14:25:26 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-11-24 14:28:00 -0500
commit93fa51d1a877be7e2d7988b1163bd7e097c2a59c (patch)
treee460619d38b32f3ca993190764fc993b7eebf797
parente3696d623d81d46997b4cbd5d417bbd057d5edc5 (diff)
downloadpython-coveragepy-git-nedbat/bug1283.tar.gz
make the methods be WeakMethods. still leaksnedbat/bug1283
[skip actions]
-rw-r--r--coverage/collector.py5
-rw-r--r--coverage/ctracer/tracer.c21
2 files changed, 21 insertions, 5 deletions
diff --git a/coverage/collector.py b/coverage/collector.py
index 89ba66ba..9e4dd50a 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -5,6 +5,7 @@
import os
import sys
+import weakref
from coverage import env
from coverage.debug import short_stack
@@ -259,9 +260,9 @@ class Collector:
tracer.check_include = self.check_include
if hasattr(tracer, 'should_start_context'):
tracer.should_start_context = self.should_start_context
- tracer.switch_context = self.switch_context
+ tracer.switch_context = weakref.WeakMethod(self.switch_context)
if hasattr(tracer, 'disable_plugin'):
- tracer.disable_plugin = self.disable_plugin
+ tracer.disable_plugin = weakref.WeakMethod(self.disable_plugin)
fn = tracer.start()
self.tracers.append(tracer)
diff --git a/coverage/ctracer/tracer.c b/coverage/ctracer/tracer.c
index 839c470b..d813ac0c 100644
--- a/coverage/ctracer/tracer.c
+++ b/coverage/ctracer/tracer.c
@@ -404,12 +404,17 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
goto error;
}
if (context != Py_None) {
+ PyObject * pscmeth;
PyObject * val;
Py_DECREF(self->context);
self->context = context;
self->pcur_entry->started_context = TRUE;
STATS( self->stats.pycalls++; )
- val = PyObject_CallFunctionObjArgs(self->switch_context, context, NULL);
+ pscmeth = PyObject_CallFunctionObjArgs(self->switch_context, NULL);
+ if (pscmeth == NULL) {
+ goto error;
+ }
+ val = PyObject_CallFunctionObjArgs(pscmeth, context, NULL);
if (val == NULL) {
goto error;
}
@@ -612,11 +617,16 @@ error:
static void
CTracer_disable_plugin(CTracer *self, PyObject * disposition)
{
+ PyObject * pmeth;
PyObject * ret;
PyErr_Print();
STATS( self->stats.pycalls++; )
- ret = PyObject_CallFunctionObjArgs(self->disable_plugin, disposition, NULL);
+ pmeth = PyObject_CallFunctionObjArgs(self->disable_plugin, NULL);
+ if (pmeth == NULL) {
+ goto error;
+ }
+ ret = PyObject_CallFunctionObjArgs(pmeth, disposition, NULL);
if (ret == NULL) {
goto error;
}
@@ -771,13 +781,18 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
/* If this frame started a context, then returning from it ends the context. */
if (self->pcur_entry->started_context) {
+ PyObject * pscmeth;
PyObject * val;
Py_DECREF(self->context);
self->context = Py_None;
Py_INCREF(self->context);
STATS( self->stats.pycalls++; )
- val = PyObject_CallFunctionObjArgs(self->switch_context, self->context, NULL);
+ pscmeth = PyObject_CallFunctionObjArgs(self->switch_context, NULL);
+ if (pscmeth == NULL) {
+ goto error;
+ }
+ val = PyObject_CallFunctionObjArgs(pscmeth, self->context, NULL);
if (val == NULL) {
goto error;
}