commit 64acdbd75cc219a030ebf547ffc32ca71493ad4b Author: David Malcolm dmalcolm@redhat.com Date: Fri Sep 2 16:10:14 2011 -0400
cpychecker: implement PyObject_IsTrue
libcpychecker/refcounts.py | 12 ++++ .../refcounts/PyObject_IsTrue/correct/input.c | 44 +++++++++++++ .../refcounts/PyObject_IsTrue/correct/script.py | 22 ++++++ .../refcounts/PyObject_IsTrue/correct/stdout.txt | 68 ++++++++++++++++++++ 4 files changed, 146 insertions(+), 0 deletions(-) --- diff --git a/libcpychecker/refcounts.py b/libcpychecker/refcounts.py index 0cf4d52..7fd80ad 100644 --- a/libcpychecker/refcounts.py +++ b/libcpychecker/refcounts.py @@ -1056,6 +1056,18 @@ class MyState(State): ######################################################################## # PyObject_* ######################################################################## + def impl_PyObject_IsTrue(self, stmt): + # http://docs.python.org/c-api/object.html#PyObject_IsTrue + s_true = self.mkstate_concrete_return_of(stmt, 1) + s_false = self.mkstate_concrete_return_of(stmt, 0) + s_failure = self.mkstate_concrete_return_of(stmt, -1) + s_failure.set_exception('PyExc_MemoryError') # arbitrarily chosen error + + fnname = stmt.fn.operand.name + return [Transition(self, s_true, '%s() returns 1 (true)' % fnname), + Transition(self, s_false, '%s() returns 0 (false)' % fnname), + Transition(self, s_failure, '%s() returns -1 (failure)' % fnname)] + def impl__PyObject_New(self, stmt): # Declaration in objimpl.h: # PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *); diff --git a/tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c b/tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c new file mode 100644 index 0000000..bd50f28 --- /dev/null +++ b/tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c @@ -0,0 +1,44 @@ +/* + Copyright 2011 David Malcolm dmalcolm@redhat.com + Copyright 2011 Red Hat, Inc. + + This is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see + http://www.gnu.org/licenses/. +*/ + +#include <Python.h> + +/* + Test of correct reference-handling in a call to PyObject_IsTrue +*/ + +PyObject * +test(PyObject *self, PyObject *args) +{ + int is_true = PyObject_IsTrue(self); + + return PyBool_FromLong(is_true); +} +static PyMethodDef test_methods[] = { + {"test_method", test, METH_VARARGS, NULL}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +/* + PEP-7 +Local variables: +c-basic-offset: 4 +indent-tabs-mode: nil +End: +*/ diff --git a/tests/cpychecker/refcounts/PyObject_IsTrue/correct/script.py b/tests/cpychecker/refcounts/PyObject_IsTrue/correct/script.py new file mode 100644 index 0000000..fdd5ba3 --- /dev/null +++ b/tests/cpychecker/refcounts/PyObject_IsTrue/correct/script.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Copyright 2011 David Malcolm dmalcolm@redhat.com +# Copyright 2011 Red Hat, Inc. +# +# This is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see +# http://www.gnu.org/licenses/. + +from libcpychecker import main +main(verify_refcounting=True, + dump_traces=True, + show_traces=False) diff --git a/tests/cpychecker/refcounts/PyObject_IsTrue/correct/stdout.txt b/tests/cpychecker/refcounts/PyObject_IsTrue/correct/stdout.txt new file mode 100644 index 0000000..f3da23e --- /dev/null +++ b/tests/cpychecker/refcounts/PyObject_IsTrue/correct/stdout.txt @@ -0,0 +1,68 @@ +Trace 0: + Transitions: + 'PyObject_IsTrue() returns 1 (true)' + 'PyBool_FromLong() returns' + 'returning' + Return value: + repr(): PointerToRegion(gcctype='struct PyObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31), region=RegionOnHeap('PyBool_FromLong', gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31))) + str(): (struct PyObject *)&RegionOnHeap('PyBool_FromLong', gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31)) from tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c:31 + r->ob_refcnt: refs: 1 + N where N >= 0 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31), region=Region('PyTypeObject for PyBool_FromLong')) + Region("region-for-arg-gcc.ParmDecl('self')"): + repr(): Region("region-for-arg-gcc.ParmDecl('self')") + str(): Region("region-for-arg-gcc.ParmDecl('self')") + r->ob_refcnt: refs: 0 + N where N >= 1 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=27), region=Region("region-for-type-of-arg-gcc.ParmDecl('self')")) + Region("region-for-arg-gcc.ParmDecl('args')"): + repr(): Region("region-for-arg-gcc.ParmDecl('args')") + str(): Region("region-for-arg-gcc.ParmDecl('args')") + r->ob_refcnt: refs: 0 + N where N >= 1 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=27), region=Region("region-for-type-of-arg-gcc.ParmDecl('args')")) + Exception: + (struct PyObject *)0 from tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c:28 + +Trace 1: + Transitions: + 'PyObject_IsTrue() returns 0 (false)' + 'PyBool_FromLong() returns' + 'returning' + Return value: + repr(): PointerToRegion(gcctype='struct PyObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31), region=RegionOnHeap('PyBool_FromLong', gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31))) + str(): (struct PyObject *)&RegionOnHeap('PyBool_FromLong', gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31)) from tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c:31 + r->ob_refcnt: refs: 1 + N where N >= 0 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31), region=Region('PyTypeObject for PyBool_FromLong')) + Region("region-for-arg-gcc.ParmDecl('self')"): + repr(): Region("region-for-arg-gcc.ParmDecl('self')") + str(): Region("region-for-arg-gcc.ParmDecl('self')") + r->ob_refcnt: refs: 0 + N where N >= 1 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=27), region=Region("region-for-type-of-arg-gcc.ParmDecl('self')")) + Region("region-for-arg-gcc.ParmDecl('args')"): + repr(): Region("region-for-arg-gcc.ParmDecl('args')") + str(): Region("region-for-arg-gcc.ParmDecl('args')") + r->ob_refcnt: refs: 0 + N where N >= 1 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=27), region=Region("region-for-type-of-arg-gcc.ParmDecl('args')")) + Exception: + (struct PyObject *)0 from tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c:28 + +Trace 2: + Transitions: + 'PyObject_IsTrue() returns -1 (failure)' + 'PyBool_FromLong() returns' + 'returning' + Return value: + repr(): PointerToRegion(gcctype='struct PyObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31), region=RegionOnHeap('PyBool_FromLong', gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31))) + str(): (struct PyObject *)&RegionOnHeap('PyBool_FromLong', gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31)) from tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c:31 + r->ob_refcnt: refs: 1 + N where N >= 0 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=31), region=Region('PyTypeObject for PyBool_FromLong')) + Region("region-for-arg-gcc.ParmDecl('self')"): + repr(): Region("region-for-arg-gcc.ParmDecl('self')") + str(): Region("region-for-arg-gcc.ParmDecl('self')") + r->ob_refcnt: refs: 0 + N where N >= 1 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=27), region=Region("region-for-type-of-arg-gcc.ParmDecl('self')")) + Region("region-for-arg-gcc.ParmDecl('args')"): + repr(): Region("region-for-arg-gcc.ParmDecl('args')") + str(): Region("region-for-arg-gcc.ParmDecl('args')") + r->ob_refcnt: refs: 0 + N where N >= 1 + r->ob_type: PointerToRegion(gcctype='struct PyTypeObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyObject_IsTrue/correct/input.c', line=27), region=Region("region-for-type-of-arg-gcc.ParmDecl('args')")) + Exception: + RegionForGlobal(gcc.VarDecl('PyExc_MemoryError'))
gcc-python-plugin-commits@lists.stg.fedorahosted.org