commit 0ac0afb457fd3dd609ed4d4ea05dad2c85c2c1b5 Author: David Malcolm dmalcolm@redhat.com Date: Fri Sep 2 15:28:00 2011 -0400
cpychecker: implement PyErr_SetFromErrno[WithFilename]
libcpychecker/refcounts.py | 23 +++++++++++ .../refcounts/PyErr_SetFromErrno/correct/input.c | 42 ++++++++++++++++++++ .../refcounts/PyErr_SetFromErrno/correct/script.py | 22 ++++++++++ .../PyErr_SetFromErrno/correct/stdout.txt | 19 +++++++++ 4 files changed, 106 insertions(+), 0 deletions(-) --- diff --git a/libcpychecker/refcounts.py b/libcpychecker/refcounts.py index f56f029..343d233 100644 --- a/libcpychecker/refcounts.py +++ b/libcpychecker/refcounts.py @@ -881,6 +881,29 @@ class MyState(State): t_next.dest.exception_rvalue = make_null_pyobject_ptr(stmt) return [t_next]
+ def impl_PyErr_SetFromErrno(self, stmt): + # API docs: + # http://docs.python.org/c-api/exceptions.html#PyErr_SetFromErrno + # + args = self.eval_stmt_args(stmt) + v_exc = args[0] + # It always returns NULL: + t_next = self.mktrans_assignment(stmt.lhs, + make_null_pyobject_ptr(stmt), + 'PyErr_SetFromErrno()') + t_next.dest.exception_rvalue = v_exc + return [t_next] + + def impl_PyErr_SetFromErrnoWithFilename(self, stmt): + args = self.eval_stmt_args(stmt) + v_exc = args[0] + # It always returns NULL: + t_next = self.mktrans_assignment(stmt.lhs, + make_null_pyobject_ptr(stmt), + 'PyErr_SetFromErrnoWithFilename()') + t_next.dest.exception_rvalue = v_exc + return [t_next] + def impl_PyErr_SetString(self, stmt): # Declared in pyerrors.h: # PyAPI_FUNC(void) PyErr_SetString(PyObject *, const char *); diff --git a/tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/input.c b/tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/input.c new file mode 100644 index 0000000..5116119 --- /dev/null +++ b/tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/input.c @@ -0,0 +1,42 @@ +/* + 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 usage of PyErr_SetFromErrno +*/ + +PyObject * +test(PyObject *self, PyObject *args) +{ + return PyErr_SetFromErrno(PyExc_IOError); +} +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/PyErr_SetFromErrno/correct/script.py b/tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/script.py new file mode 100644 index 0000000..fdd5ba3 --- /dev/null +++ b/tests/cpychecker/refcounts/PyErr_SetFromErrno/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/PyErr_SetFromErrno/correct/stdout.txt b/tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/stdout.txt new file mode 100644 index 0000000..d1ade08 --- /dev/null +++ b/tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/stdout.txt @@ -0,0 +1,19 @@ +Trace 0: + Transitions: + 'PyErr_SetFromErrno()' + 'returning' + Return value: + repr(): ConcreteValue(gcctype='struct PyObject *', loc=gcc.Location(file='tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/input.c', line=29), value=0) + str(): (struct PyObject *)0 from tests/cpychecker/refcounts/PyErr_SetFromErrno/correct/input.c:29 + 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/PyErr_SetFromErrno/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/PyErr_SetFromErrno/correct/input.c', line=27), region=Region("region-for-type-of-arg-gcc.ParmDecl('args')")) + Exception: + unknown struct PyObject * from /usr/include/python2.7/pyerrors.h:131
gcc-python-plugin-commits@lists.stg.fedorahosted.org