Skip to content

Commit 49a344e

Browse files
skirpichevcmaloney
andauthored
gh-162350: Use AC for array.array constructor (#162419)
This fixes it's signature and adds also support for Vectorcall protocol. Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
1 parent 806f101 commit 49a344e

2 files changed

Lines changed: 214 additions & 117 deletions

File tree

‎Modules/arraymodule.c‎

Lines changed: 77 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ enum machine_format_code {
133133
* Must come after arrayobject, arrayiterobject,
134134
* and enum machine_code_type definitions.
135135
*/
136+
#define clinic_state() (find_array_state_by_type(type))
136137
#include "clinic/arraymodule.c.h"
138+
#undef clinic_state
137139

138140
#define array_Check(op, state) PyObject_TypeCheck(op, state->ArrayType)
139141

@@ -757,9 +759,9 @@ static const struct arraydescr descriptors[] = {
757759
Implementations of array object methods.
758760
****************************************************************************/
759761
/*[clinic input]
760-
class array.array "arrayobject *" "ArrayType"
762+
class array.array "arrayobject *" "clinic_state()->ArrayType"
761763
[clinic start generated code]*/
762-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a5c29edf59f176a3]*/
764+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f7199c1c2db75905]*/
763765

764766
static PyObject *
765767
newarrayobject_untracked(PyTypeObject *type, Py_ssize_t size,
@@ -2166,47 +2168,6 @@ typecode_to_mformat_code(const char *typecode)
21662168
}
21672169
}
21682170

2169-
/* Forward declaration. */
2170-
static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2171-
2172-
/*
2173-
* Internal: This function wraps the array constructor--i.e., array_new()--to
2174-
* allow the creation of array objects from C code without having to deal
2175-
* directly the tuple argument of array_new(). The typecode argument is a
2176-
* string, like "i" or "f" for example, representing an array
2177-
* type code. The items argument is a bytes or a list object from which
2178-
* contains the initial value of the array.
2179-
*
2180-
* On success, this functions returns the array object created. Otherwise,
2181-
* NULL is returned to indicate a failure.
2182-
*/
2183-
static PyObject *
2184-
make_array(PyTypeObject *arraytype, const char *typecode, PyObject *items)
2185-
{
2186-
PyObject *new_args;
2187-
PyObject *array_obj;
2188-
PyObject *typecode_obj;
2189-
2190-
assert(arraytype != NULL);
2191-
assert(items != NULL);
2192-
2193-
typecode_obj = PyUnicode_FromString(typecode);
2194-
if (typecode_obj == NULL)
2195-
return NULL;
2196-
2197-
new_args = _PyTuple_FromPairSteal(typecode_obj, Py_NewRef(items));
2198-
if (new_args == NULL) {
2199-
return NULL;
2200-
}
2201-
2202-
array_obj = array_new(arraytype, new_args, NULL);
2203-
Py_DECREF(new_args);
2204-
if (array_obj == NULL)
2205-
return NULL;
2206-
2207-
return array_obj;
2208-
}
2209-
22102171
/*
22112172
* This functions is a special constructor used when unpickling an array. It
22122173
* provides a portable way to rebuild an array from its memory representation.
@@ -2273,7 +2234,7 @@ array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
22732234
/* Fast path: No decoding has to be done. */
22742235
if (mformat_code == typecode_to_mformat_code(typecode) ||
22752236
mformat_code == UNKNOWN_FORMAT) {
2276-
return make_array(arraytype, typecode, items);
2237+
return array_new_impl(arraytype, typecode, items);
22772238
}
22782239

22792240
/* Slow path: Decode the byte string according to the given machine
@@ -2486,7 +2447,7 @@ array__array_reconstructor_impl(PyObject *module, PyTypeObject *arraytype,
24862447
return NULL;
24872448
}
24882449

2489-
result = make_array(arraytype, typecode, converted_items);
2450+
result = array_new_impl(arraytype, typecode, converted_items);
24902451
Py_DECREF(converted_items);
24912452
return result;
24922453
}
@@ -2930,22 +2891,80 @@ array_buffer_relbuf(PyObject *op, Py_buffer *Py_UNUSED(view))
29302891
self->ob_exports--;
29312892
}
29322893

2894+
/*[clinic input]
2895+
@vectorcall
2896+
@classmethod
2897+
array.array.__new__ as array_new
2898+
typecode as s: str
2899+
initializer as initial: object(c_default="NULL") = ()
2900+
/
2901+
2902+
Create array object.
2903+
2904+
Return a new array whose items are restricted by typecode, and
2905+
initialized from the optional initializer value, which must be a list,
2906+
string or iterable over elements of the appropriate type.
2907+
2908+
Arrays represent basic values and behave very much like lists, except
2909+
the type of objects stored in them is constrained. The type is specified
2910+
at object creation time by using a type code, which is a string.
2911+
The following type codes are defined:
2912+
2913+
Type code C Type Minimum size in bytes
2914+
'b' signed integer 1
2915+
'B' unsigned integer 1
2916+
'w' Py_UCS4 4
2917+
'h' signed integer 2
2918+
'H' unsigned integer 2
2919+
'i' signed integer 2
2920+
'I' unsigned integer 2
2921+
'l' signed integer 4
2922+
'L' unsigned integer 4
2923+
'q' signed integer 8 (see note)
2924+
'Q' unsigned integer 8 (see note)
2925+
'e' 16-bit IEEE floats 2
2926+
'f' floating-point 4
2927+
'd' floating-point 8
2928+
'Zf' float complex 8
2929+
'Zd' double complex 16
2930+
2931+
NOTE: The 'q' and 'Q' type codes are only available if the platform
2932+
C compiler used to build Python supports 'long long', or, on Windows,
2933+
'__int64'.
2934+
2935+
Methods:
2936+
2937+
append() -- append a new item to the end of the array
2938+
buffer_info() -- return information giving the current memory info
2939+
byteswap() -- byteswap all the items of the array
2940+
count() -- return number of occurrences of an object
2941+
extend() -- extend array by appending multiple elements from an iterable
2942+
fromfile() -- read items from a file object
2943+
fromlist() -- append items from the list
2944+
frombytes() -- append items from the string
2945+
index() -- return index of first occurrence of an object
2946+
insert() -- insert a new item into the array at a provided position
2947+
pop() -- remove and return item (default last)
2948+
remove() -- remove first occurrence of an object
2949+
reverse() -- reverse the order of the items in the array
2950+
tofile() -- write all items to a file object
2951+
tolist() -- return the array converted to an ordinary list
2952+
tobytes() -- return the array converted to a string
2953+
2954+
Attributes:
2955+
2956+
typecode -- the typecode character used to create the array
2957+
itemsize -- the length in bytes of one array item
2958+
[clinic start generated code]*/
2959+
29332960
static PyObject *
2934-
array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2961+
array_new_impl(PyTypeObject *type, const char *s, PyObject *initial)
2962+
/*[clinic end generated code: output=4b6a68b7540f8f3f input=23a2e91f05deccff]*/
29352963
{
29362964
array_state *state = find_array_state_by_type(type);
2937-
const char *s;
2938-
PyObject *initial = NULL, *it = NULL;
2965+
PyObject *it = NULL;
29392966
const struct arraydescr *descr;
29402967

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-
29492968
if (PySys_Audit("array.__new__", "sO",
29502969
s, initial ? initial : Py_None) < 0) {
29512970
return NULL;
@@ -3094,65 +3113,6 @@ an array of basic values: characters, integers, floating-point\n\
30943113
numbers, complex numbers. Arrays are sequence types and behave very much like lists,\n\
30953114
except that the type of objects stored in them is constrained.\n");
30963115

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-
31563116
static PyObject *array_iter(PyObject *op);
31573117

31583118
static struct PyMemberDef array_members[] = {
@@ -3164,7 +3124,7 @@ static PyType_Slot array_slots[] = {
31643124
{Py_tp_dealloc, array_dealloc},
31653125
{Py_tp_repr, array_repr},
31663126
{Py_tp_getattro, PyObject_GenericGetAttr},
3167-
{Py_tp_doc, (void *)arraytype_doc},
3127+
{Py_tp_doc, (void *)array_new__doc__},
31683128
{Py_tp_richcompare, array_richcompare},
31693129
{Py_tp_iter, array_iter},
31703130
{Py_tp_methods, array_methods},
@@ -3173,6 +3133,7 @@ static PyType_Slot array_slots[] = {
31733133
{Py_tp_alloc, _PyType_AllocNoTrack},
31743134
{Py_tp_new, array_new},
31753135
{Py_tp_traverse, _PyObject_VisitType},
3136+
{Py_tp_vectorcall, array_vectorcall},
31763137

31773138
/* as sequence */
31783139
{Py_sq_length, array_length},

0 commit comments

Comments
 (0)