New version from Travis, with new specs.
This commit is contained in:
parent
0e03857cad
commit
2e742fc23a
58
pep-0357.txt
58
pep-0357.txt
|
@ -28,9 +28,10 @@ Rationale
|
||||||
In NumPy, for example, there are 8 different integer scalars
|
In NumPy, for example, there are 8 different integer scalars
|
||||||
corresponding to unsigned and signed integers of 8, 16, 32, and 64
|
corresponding to unsigned and signed integers of 8, 16, 32, and 64
|
||||||
bits. These type-objects could reasonably be used as integers in
|
bits. These type-objects could reasonably be used as integers in
|
||||||
many places where Python expects true integers. There should be
|
many places where Python expects true integers but cannot inherit from
|
||||||
some way to be able to tell Python that an object can behave like
|
the Python integer type because of incompatible memory layouts.
|
||||||
an integer.
|
There should be some way to be able to tell Python that an object can
|
||||||
|
behave like an integer.
|
||||||
|
|
||||||
It is not possible to use the nb_int (and __int__ special method)
|
It is not possible to use the nb_int (and __int__ special method)
|
||||||
for this purpose because that method is used to *coerce* objects
|
for this purpose because that method is used to *coerce* objects
|
||||||
|
@ -45,15 +46,41 @@ Proposal
|
||||||
|
|
||||||
Add an nb_index slot to PyNumberMethods, and a corresponding
|
Add an nb_index slot to PyNumberMethods, and a corresponding
|
||||||
__index__ special method. Objects could define a function to place
|
__index__ special method. Objects could define a function to place
|
||||||
in the nb_index slot that returns an appropriate C-integer for use
|
in the nb_index slot that returns an appropriate C-integer (Py_ssize_t
|
||||||
as ilow or ihigh in PySequence_GetSlice, PySequence_SetSlice, and
|
after PEP 353). This C-integer will be used whenever Python needs
|
||||||
PySequence_DelSlice. This integer could also be used elsewhere by
|
one such as in PySequence_GetSlice, PySequence_SetSlice, and
|
||||||
Python when a C-integer is required.
|
PySequence_DelSlice.
|
||||||
|
|
||||||
|
Specification:
|
||||||
|
|
||||||
|
1) The nb_index slot will have the signature
|
||||||
|
|
||||||
|
Py_ssize_t index_func (PyObject *self)
|
||||||
|
|
||||||
|
2) The __index__ special method will have the signature
|
||||||
|
|
||||||
|
def __index__(self):
|
||||||
|
return obj
|
||||||
|
|
||||||
|
Where obj must be either an int or a long or another object that has the
|
||||||
|
__index__ special method (but not self).
|
||||||
|
|
||||||
|
3) A new C-API function PyNumber_Index will be added with signature
|
||||||
|
|
||||||
|
Py_ssize_t PyNumber_index (PyObject *obj)
|
||||||
|
|
||||||
|
which will special-case integer and long integer objects but otherwise
|
||||||
|
return obj->ob_type->tp_as_number->nb_index(obj) if it is available.
|
||||||
|
A -1 will be returned and an exception set on an error.
|
||||||
|
|
||||||
|
4) A new operator.index(obj) function will be added that calls
|
||||||
|
equivalent of obj.__index__() and raises an error if obj does not implement
|
||||||
|
the special method.
|
||||||
|
|
||||||
Implementation Plan
|
Implementation Plan
|
||||||
|
|
||||||
1) Add the nb_index slot in object.h and modify typeobject.c to
|
1) Add the nb_index slot in object.h and modify typeobject.c to
|
||||||
create the __index__ method.
|
create the __index__ method
|
||||||
|
|
||||||
2) Change the ISINT macro in ceval.c to ISINDEX and alter it to
|
2) Change the ISINT macro in ceval.c to ISINDEX and alter it to
|
||||||
accomodate objects with the index slot defined.
|
accomodate objects with the index slot defined.
|
||||||
|
@ -68,7 +95,8 @@ Implementation Plan
|
||||||
5) Add PyNumber_Index C-API to return an integer from any
|
5) Add PyNumber_Index C-API to return an integer from any
|
||||||
Python Object that has the nb_index slot.
|
Python Object that has the nb_index slot.
|
||||||
|
|
||||||
6) Add an operator.index(x) function that calls x.__index__()
|
6) Add the operator.index(x) function.
|
||||||
|
|
||||||
|
|
||||||
Possible Concerns
|
Possible Concerns
|
||||||
|
|
||||||
|
@ -95,8 +123,16 @@ Possible Concerns
|
||||||
interpretations of the slot are possible. For example, the slot
|
interpretations of the slot are possible. For example, the slot
|
||||||
can be used any time Python requires an integer internally (such
|
can be used any time Python requires an integer internally (such
|
||||||
as in "mystring" * 3). The name was suggested by Guido because
|
as in "mystring" * 3). The name was suggested by Guido because
|
||||||
slicing syntax is the biggest reason for having such a slot and no
|
slicing syntax is the biggest reason for having such a slot and
|
||||||
better name emerged.
|
in the end no better name emerged.
|
||||||
|
|
||||||
|
Why return Py_ssize_t from nb_index?
|
||||||
|
|
||||||
|
The nb_index slot is primarily intended to return an integer
|
||||||
|
needed by the sequence interface. In Python 2.5 this is
|
||||||
|
Py_ssize_t. As this is the primary purpose of the slot, it
|
||||||
|
makes sense to return the C-integer directly and not wrapped
|
||||||
|
in a Python int object.
|
||||||
|
|
||||||
Reference Implementation
|
Reference Implementation
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue