Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

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

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


dmalcolm at redhat

Apr 18, 2012, 5:01 PM

Post #1 of 5 (171 views)
Permalink
Highlighting reference-stealing APIs [was Re: cpython: Fix email post-commit review comments.]

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?)

Hope this is helpful
Dave

_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


greg at krypto

Apr 18, 2012, 6:04 PM

Post #2 of 5 (166 views)
Permalink
Re: Highlighting reference-stealing APIs [was Re: cpython: Fix email post-commit review comments.] [In reply to]

On Wed, Apr 18, 2012 at 5:01 PM, David Malcolm <dmalcolm [at] redhat> 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
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/greg%40krypto.org
>


ncoghlan at gmail

Apr 18, 2012, 6:25 PM

Post #3 of 5 (160 views)
Permalink
Re: Highlighting reference-stealing APIs [was Re: cpython: Fix email post-commit review comments.] [In reply to]

On Thu, Apr 19, 2012 at 11:04 AM, Gregory P. Smith <greg [at] krypto> wrote:
> +1  Adding these annotations and setting up a buildbot that builds using
> cpychecker would be a great.

Even without the extra annotations, running cpychecker on at least one
of the buildbots might be helpful.

I'm in the process of setting up a buildbot for RHEL 6, once I get it
up and running normally, I'll look into what it would take to install
and enable cpychecker for the builds. (Or, alternatively, I may make
it a separate cron job, similar to the daily refcount leak detection
run).

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan [at] gmail   |   Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


sam.partington at gmail

Apr 19, 2012, 3:42 AM

Post #4 of 5 (158 views)
Permalink
Re: Highlighting reference-stealing APIs [was Re: cpython: Fix email post-commit review comments.] [In reply to]

On 19 April 2012 02:20, Nick Coghlan <ncoghlan [at] gmail> wrote:
> On Thu, Apr 19, 2012 at 12:21 AM, Antoine Pitrou <solipsis [at] 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)
>
> Yeah, it would have been nice if there was an explicit hint in the API
> names when reference stealing was involved, but I guess it's far too
> late now :(

It's too late to change the fn names sure, but you could change the
argument names in question for reference stealing apis with some kind
of markup.

That would make it fairly easy to write a script that did the checking for you :

int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *stolen_o)

Or better yet would be to mark the types :

int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyStolenObject* o)
PyBorrowedObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)

PyStolenObject and PyBorrowedObject would just be typedefs to PyObject
normally. But a consenting user could define PyENABLE_CHECKED_REFS
before including Python.h which would given

#if defined(PyENABLE_CHECKED_STOLEN_REFS)
struct PyStolenObject;
struct PyBorrowedObject;
#define PyYesIKnowItsStolen(o) ((PyStolenObject*)o)
#define PyYesIKnowItsBorrowed(o) ((PyObject*)o)
#else
typedef PyStolenObject PyObject;
typedef PyBorrowedObject PyObject;
#endif

Forcing the user to use

PyTuple_SetItem(p, pos, PyYesIKnowItsStolen(o))
PyObject * ref = PyYesIKnowItsBorrowed(PyTuple_GetItem(p, pos));

Or else it would fail to compile. The user could even add her own :

PyStolenObject* IncRefBecauseIKnowItsStolen(PyObject* o) {
PyIncRef(o); return (PyStolenObject*)o; }
PyObject* IncRefBecauseIKnowItsBorrowed(PyBorrowedObject* o) {
PyIncRef(o); return (PyObject*)o; }

This would not require a special gcc build and would be available to
anyone who wanted it. I use a similar, C++ based trick in my python
extension code to avoid the whole issue of ref leaking, but I have to
be careful at the point of calling the python api, having it automatic
would be great.

On a similar note, I have just implemented a wrapper around Python.h
which runtime checks that the GIL is held around every call to the
Python API or else fails very noisily. This was done because it turns
out that wxPython had a ton of non-GIL calls to the API causing random
sporadic set faults in our app. We now use it on several of our
extensions. It doesn't require any changes to Python.h, you just
need to add an include path before the python include path. Would
there be any interest in this?

Sam


On 19 April 2012 02:25, Nick Coghlan <ncoghlan [at] gmail> wrote:
> On Thu, Apr 19, 2012 at 11:04 AM, Gregory P. Smith <greg [at] krypto> wrote:
>> +1  Adding these annotations and setting up a buildbot that builds using
>> cpychecker would be a great.
>
> Even without the extra annotations, running cpychecker on at least one
> of the buildbots might be helpful.
>
> I'm in the process of setting up a buildbot for RHEL 6, once I get it
> up and running normally, I'll look into what it would take to install
> and enable cpychecker for the builds. (Or, alternatively, I may make
> it a separate cron job, similar to the daily refcount leak detection
> run).
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan [at] gmail   |   Brisbane, Australia
> _______________________________________________
> Python-Dev mailing list
> Python-Dev [at] python
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/sam.partington%40gmail.com
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


martin at v

Apr 19, 2012, 3:59 AM

Post #5 of 5 (159 views)
Permalink
Re: Highlighting reference-stealing APIs [was Re: cpython: Fix email post-commit review comments.] [In reply to]

Am 19.04.2012 12:42, schrieb Sam Partington:
> On 19 April 2012 02:20, Nick Coghlan <ncoghlan [at] gmail> wrote:
>> On Thu, Apr 19, 2012 at 12:21 AM, Antoine Pitrou <solipsis [at] 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)
>>
>> Yeah, it would have been nice if there was an explicit hint in the API
>> names when reference stealing was involved, but I guess it's far too
>> late now :(
>
> It's too late to change the fn names sure, but you could change the
> argument names in question for reference stealing apis with some kind
> of markup.

While it may too late to change the names, it's not to late to remove
these functions entirely. It will take some time, but it would be
possible to add parallel APIs that neither borrow nor steal references,
and have them preferred over the existing APIs. Then, with Python 4,
the old APIs could go away.

Regards,
Martin
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.