This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Remove unneeded "context" pointer from getters and setters
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: larry, loewis, mark.dickinson, pitrou
Priority: normal Keywords: patch

Created on 2009-04-29 16:15 by larry, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
lch.getset.r72081.diff larry, 2009-04-29 16:15 Patch against py3k/trunk r72081.
Messages (7)
msg86812 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2009-04-29 16:15
PyGetSetDef has a "void *context" field.  This field is passed in to the
get and set functions implementing the property.  The field is almost
never used, so it adds unnecessary complexity.  Clearly, YAGNI.

There are two places in CPython where it got used: both in PyLongObject,
both using long_getN(), where it's used to store an integer constant
(not a pointer!) that is converted to a Long object and returned.  Since
there are only two of these, this was easy to fix: split long_getN()
into two functions returning a hard-coded number, long_get0() and
long_get1().

The attached patch removes the "void *context" field of PyGetSetDef,
removes the almost universally ignored context arguments to the getters
and setters, and removes the data declarations for the context pointer
in static PyGetSetDef structs.  With the patch applied py3k/trunk passes
all expected regression tests.

Martin: I added you to the nosy list 'cause we looked in to this at the
sprints, so I figured you'd be interested.
msg86813 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2009-04-29 16:16
Whoops, forgot to categorize the issue before submitting it.
msg86814 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-04-29 16:29
I haven't looked at this patch in detail, but I'd be delighted to see 
those uses of the context in longobject.c disappear.
msg86938 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-05-02 11:39
Doesn't it also break binary compatibility with extension modules?
Perhaps this should be quickly discussed on python-dev, although I agree
that it looks sensible.
msg86961 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-05-02 18:03
I've removed those gratuitous context uses in Objects/longobject.c
in r72202 (trunk) and r72203 (py3k).

I'm not sure it's a good idea to remove the context field completely.  
Apart from the compatibility issues that Antoine pointed out, it
doesn't seem improbable that some getters and setters might need it.

A Google code search for PyGetSetDef turned up at least one non-null
use of the context field (in something called pyephem).  I didn't
look at it closely, though.
msg87107 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2009-05-04 08:43
Mark Dickinson: no extension module would ever *need* it.  One could
reproduce the functionality with stub intermediary functions, as follows:

PyObject *my_getter_with_context(PyObject *self, void *context) {
  /* ... /
}

PyObject *my_getter_A(PyObject *self) {
  return my_getter_with_context(self, "A");
}


PyObject *my_getter_B(PyObject *self) {
  return my_getter_with_context(self, "B");
}

/* etc. */

The body of Python extension code shows that this facility is rarely
needed.  And folks that need it can roll their own.

It's a minor thing, but I think it's worth doing, if we're willing to
break compatibility in this way.

Anyway, I'm gonna float it on python-dev.
msg87108 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2009-05-04 08:57
Oh, and, I looked at pyephem.  It uses the context field to... store an
integer.  In this case, the offset in a structure where it's going to
pull out a value (float or double).  This code would probably be clearer
if forced to make do without the "closure".
History
Date User Action Args
2022-04-11 14:56:48adminsetgithub: 50130
2020-11-16 22:14:22iritkatrielsetversions: + Python 3.9, Python 3.10, - Python 3.3
2011-03-26 12:20:20mark.dickinsonsettitle: test___all__ -> Remove unneeded "context" pointer from getters and setters
2011-03-26 12:19:58mark.dickinsonsettitle: Remove unneeded "context" pointer from getters and setters -> test___all__
2011-03-09 02:04:50terry.reedysetnosy: loewis, mark.dickinson, pitrou, larry
versions: + Python 3.3, - Python 3.1
2009-05-04 08:57:50larrysetmessages: + msg87108
2009-05-04 08:43:29larrysetmessages: + msg87107
2009-05-02 18:03:17mark.dickinsonsetmessages: + msg86961
2009-05-02 11:39:35pitrousetnosy: + pitrou
messages: + msg86938
2009-04-29 16:29:15mark.dickinsonsetnosy: + mark.dickinson
messages: + msg86814
2009-04-29 16:16:01larrysettype: enhancement
messages: + msg86813
components: + Interpreter Core
versions: + Python 3.1
2009-04-29 16:15:23larrycreate