[Python-Dev] Highlighting reference-stealing APIs [was Re: cpython: Fix email post-commit review comments.]

Gregory P. Smith greg at krypto.org
Thu Apr 19 03:04:07 CEST 2012


On Wed, Apr 18, 2012 at 5:01 PM, David Malcolm <dmalcolm at redhat.com> wrote:

> On Thu, 2012-04-19 at 10:48 +1200, Greg Ewing wrote:
> > Antoine Pitrou wrote:
> >
> > > (and here we see why reference-stealing APIs are a nuisance: because
> > > you never know in advance whether a function will steal a reference or
> > > not, and you have to read the docs for each and every C API call you
> > > make)
> >
> > Fortunately, they're very rare, so you don't encounter
> > them often.
> >
> > Unfortunately, they're very rare, so you're all the more
> > likely to forget about them and get bitten.
> >
> > Functions with ref-stealing APIs really ought to have
> > a naming convention that makes them stand out and remind
> > you to consult the documentation.
> FWIW my refcount static analyzer adds various new compile-time
> attributes to gcc:
>
> http://gcc-python-plugin.readthedocs.org/en/latest/cpychecker.html#marking-functions-that-steal-references-to-their-arguments
> so you can write declarations like these:
>
> extern void bar(int i, PyObject *obj, int j, PyObject *other)
>  CPYCHECKER_STEALS_REFERENCE_TO_ARG(2)
>  CPYCHECKER_STEALS_REFERENCE_TO_ARG(4);
>
> There's a similar attribute for functions that return borrowed
> references:
>
>  PyObject *foo(void)
>    CPYCHECKER_RETURNS_BORROWED_REF;
>
> Perhaps we should add such attributes to the headers for Python 3.3?
> (perhaps with a different naming convention?)
>

+1  Adding these annotations and setting up a buildbot that builds using
cpychecker would be a great.

-gps


>
> Hope this is helpful
> Dave
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/greg%40krypto.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120418/6517afd5/attachment.html>


More information about the Python-Dev mailing list