Skip to content

Commit 02fe78f

Browse files
committed
gh-162350: Use AC for array.array constructor
This fixes it's signature and adds also support for Vectorcall protocol.
1 parent 1cc5802 commit 02fe78f

2 files changed

Lines changed: 211 additions & 74 deletions

File tree

‎Modules/arraymodule.c‎

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ get_array_state(PyObject *module)
8282
(get_array_state(PyType_GetModuleByDef(tp, &arraymodule)))
8383
#define get_array_state_by_class(cls) \
8484
(get_array_state(PyType_GetModule(cls)))
85+
#define clinic_state() (find_array_state_by_type(type))
8586

8687
#define arrayobject_CAST(op) ((arrayobject *)(op))
8788
#define arrayiterobject_CAST(op) ((arrayiterobject *)(op))
@@ -757,9 +758,9 @@ static const struct arraydescr descriptors[] = {
757758
Implementations of array object methods.
758759
****************************************************************************/
759760
/*[clinic input]
760-
class array.array "arrayobject *" "ArrayType"
761+
class array.array "arrayobject *" "clinic_state()->ArrayType"
761762
[clinic start generated code]*/
762-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a5c29edf59f176a3]*/
763+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f7199c1c2db75905]*/
763764

764765
static PyObject *
765766
newarrayobject_untracked(PyTypeObject *type, Py_ssize_t size,
@@ -2930,22 +2931,80 @@ array_buffer_relbuf(PyObject *op, Py_buffer *Py_UNUSED(view))
29302931
self->ob_exports--;
29312932
}
29322933

2934+
/*[clinic input]
2935+
@vectorcall
2936+
@classmethod
2937+
array.array.__new__ as array_new
2938+
typecode as s: str
2939+
initializer as initial: object(c_default="NULL") = None
2940+
/
2941+
2942+
Create array object.
2943+
2944+
Return a new array whose items are restricted by typecode, and
2945+
initialized from the optional initializer value, which must be a list
2946+
string or iterable over elements of the appropriate type.
2947+
2948+
Arrays represent basic values and behave very much like lists, except
2949+
the type of objects stored in them is constrained. The type is specified
2950+
at object creation time by using a type code, which is a string.
2951+
The following type codes are defined:
2952+
2953+
Type code C Type Minimum size in bytes
2954+
'b' signed integer 1
2955+
'B' unsigned integer 1
2956+
'w' Py_UCS4 4
2957+
'h' signed integer 2
2958+
'H' unsigned integer 2
2959+
'i' signed integer 2
2960+
'I' unsigned integer 2
2961+
'l' signed integer 4
2962+
'L' unsigned integer 4
2963+
'q' signed integer 8 (see note)
2964+
'Q' unsigned integer 8 (see note)
2965+
'e' 16-bit IEEE floats 2
2966+
'f' floating-point 4
2967+
'd' floating-point 8
2968+
'Zf' float complex 8
2969+
'Zd' double complex 16
2970+
2971+
NOTE: The 'q' and 'Q' type codes are only available if the platform
2972+
C compiler used to build Python supports 'long long', or, on Windows,
2973+
'__int64'.
2974+
2975+
Methods:
2976+
2977+
append() -- append a new item to the end of the array
2978+
buffer_info() -- return information giving the current memory info
2979+
byteswap() -- byteswap all the items of the array
2980+
count() -- return number of occurrences of an object
2981+
extend() -- extend array by appending multiple elements from an iterable
2982+
fromfile() -- read items from a file object
2983+
fromlist() -- append items from the list
2984+
frombytes() -- append items from the string
2985+
index() -- return index of first occurrence of an object
2986+
insert() -- insert a new item into the array at a provided position
2987+
pop() -- remove and return item (default last)
2988+
remove() -- remove first occurrence of an object
2989+
reverse() -- reverse the order of the items in the array
2990+
tofile() -- write all items to a file object
2991+
tolist() -- return the array converted to an ordinary list
2992+
tobytes() -- return the array converted to a string
2993+
2994+
Attributes:
2995+
2996+
typecode -- the typecode character used to create the array
2997+
itemsize -- the length in bytes of one array item
2998+
[clinic start generated code]*/
2999+
29333000
static PyObject *
2934-
array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3001+
array_new_impl(PyTypeObject *type, const char *s, PyObject *initial)
3002+
/*[clinic end generated code: output=4b6a68b7540f8f3f input=a91c3679e757d2cf]*/
29353003
{
29363004
array_state *state = find_array_state_by_type(type);
2937-
const char *s;
2938-
PyObject *initial = NULL, *it = NULL;
3005+
PyObject *it = NULL;
29393006
const struct arraydescr *descr;
29403007

2941-
if ((type == state->ArrayType ||
2942-
type->tp_init == state->ArrayType->tp_init) &&
2943-
!_PyArg_NoKeywords("array.array", kwds))
2944-
return NULL;
2945-
2946-
if (!PyArg_ParseTuple(args, "s|O:array", &s, &initial))
2947-
return NULL;
2948-
29493008
if (PySys_Audit("array.__new__", "sO",
29503009
s, initial ? initial : Py_None) < 0) {
29513010
return NULL;
@@ -3094,65 +3153,6 @@ an array of basic values: characters, integers, floating-point\n\
30943153
numbers, complex numbers. Arrays are sequence types and behave very much like lists,\n\
30953154
except that the type of objects stored in them is constrained.\n");
30963155

3097-
PyDoc_STRVAR(arraytype_doc,
3098-
"array(typecode [, initializer]) -> array\n\
3099-
\n\
3100-
Return a new array whose items are restricted by typecode, and\n\
3101-
initialized from the optional initializer value, which must be a list,\n\
3102-
string or iterable over elements of the appropriate type.\n\
3103-
\n\
3104-
Arrays represent basic values and behave very much like lists, except\n\
3105-
the type of objects stored in them is constrained. The type is specified\n\
3106-
at object creation time by using a type code, which is a string.\n\
3107-
The following type codes are defined:\n\
3108-
\n\
3109-
Type code C Type Minimum size in bytes\n\
3110-
'b' signed integer 1\n\
3111-
'B' unsigned integer 1\n\
3112-
'w' Py_UCS4 4\n\
3113-
'h' signed integer 2\n\
3114-
'H' unsigned integer 2\n\
3115-
'i' signed integer 2\n\
3116-
'I' unsigned integer 2\n\
3117-
'l' signed integer 4\n\
3118-
'L' unsigned integer 4\n\
3119-
'q' signed integer 8 (see note)\n\
3120-
'Q' unsigned integer 8 (see note)\n\
3121-
'e' 16-bit IEEE floats 2\n\
3122-
'f' floating-point 4\n\
3123-
'd' floating-point 8\n\
3124-
'Zf' float complex 8\n\
3125-
'Zd' double complex 16\n\
3126-
\n\
3127-
NOTE: The 'q' and 'Q' type codes are only available if the platform\n\
3128-
C compiler used to build Python supports 'long long', or, on Windows,\n\
3129-
'__int64'.\n\
3130-
\n\
3131-
Methods:\n\
3132-
\n\
3133-
append() -- append a new item to the end of the array\n\
3134-
buffer_info() -- return information giving the current memory info\n\
3135-
byteswap() -- byteswap all the items of the array\n\
3136-
count() -- return number of occurrences of an object\n\
3137-
extend() -- extend array by appending multiple elements from an iterable\n\
3138-
fromfile() -- read items from a file object\n\
3139-
fromlist() -- append items from the list\n\
3140-
frombytes() -- append items from the string\n\
3141-
index() -- return index of first occurrence of an object\n\
3142-
insert() -- insert a new item into the array at a provided position\n\
3143-
pop() -- remove and return item (default last)\n\
3144-
remove() -- remove first occurrence of an object\n\
3145-
reverse() -- reverse the order of the items in the array\n\
3146-
tofile() -- write all items to a file object\n\
3147-
tolist() -- return the array converted to an ordinary list\n\
3148-
tobytes() -- return the array converted to a string\n\
3149-
\n\
3150-
Attributes:\n\
3151-
\n\
3152-
typecode -- the typecode character used to create the array\n\
3153-
itemsize -- the length in bytes of one array item\n\
3154-
");
3155-
31563156
static PyObject *array_iter(PyObject *op);
31573157

31583158
static struct PyMemberDef array_members[] = {
@@ -3164,7 +3164,7 @@ static PyType_Slot array_slots[] = {
31643164
{Py_tp_dealloc, array_dealloc},
31653165
{Py_tp_repr, array_repr},
31663166
{Py_tp_getattro, PyObject_GenericGetAttr},
3167-
{Py_tp_doc, (void *)arraytype_doc},
3167+
{Py_tp_doc, (void *)array_new__doc__},
31683168
{Py_tp_richcompare, array_richcompare},
31693169
{Py_tp_iter, array_iter},
31703170
{Py_tp_methods, array_methods},
@@ -3173,6 +3173,7 @@ static PyType_Slot array_slots[] = {
31733173
{Py_tp_alloc, _PyType_AllocNoTrack},
31743174
{Py_tp_new, array_new},
31753175
{Py_tp_traverse, _PyObject_VisitType},
3176+
{Py_tp_vectorcall, array_vectorcall},
31763177

31773178
/* as sequence */
31783179
{Py_sq_length, array_length},

‎Modules/clinic/arraymodule.c.h‎

Lines changed: 137 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)