Skip to content

Commit 0226f3e

Browse files
authored
Let the argument clinic do the type checking for heapq (GH-20284)
1 parent 8b62644 commit 0226f3e

File tree

2 files changed

+117
-46
lines changed

2 files changed

+117
-46
lines changed

Modules/_heapqmodule.c

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ siftup(PyListObject *heap, Py_ssize_t pos)
113113
/*[clinic input]
114114
_heapq.heappush
115115
116-
heap: object
116+
heap: object(subclass_of='&PyList_Type')
117117
item: object
118118
/
119119
@@ -122,13 +122,8 @@ Push item onto heap, maintaining the heap invariant.
122122

123123
static PyObject *
124124
_heapq_heappush_impl(PyObject *module, PyObject *heap, PyObject *item)
125-
/*[clinic end generated code: output=912c094f47663935 input=7913545cb5118842]*/
125+
/*[clinic end generated code: output=912c094f47663935 input=7c69611f3698aceb]*/
126126
{
127-
if (!PyList_Check(heap)) {
128-
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
129-
return NULL;
130-
}
131-
132127
if (PyList_Append(heap, item))
133128
return NULL;
134129

@@ -143,11 +138,6 @@ heappop_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
143138
PyObject *lastelt, *returnitem;
144139
Py_ssize_t n;
145140

146-
if (!PyList_Check(heap)) {
147-
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
148-
return NULL;
149-
}
150-
151141
/* raises IndexError if the heap is empty */
152142
n = PyList_GET_SIZE(heap);
153143
if (n == 0) {
@@ -177,15 +167,15 @@ heappop_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
177167
/*[clinic input]
178168
_heapq.heappop
179169
180-
heap: object
170+
heap: object(subclass_of='&PyList_Type')
181171
/
182172
183173
Pop the smallest item off the heap, maintaining the heap invariant.
184174
[clinic start generated code]*/
185175

186176
static PyObject *
187-
_heapq_heappop(PyObject *module, PyObject *heap)
188-
/*[clinic end generated code: output=e1bbbc9866bce179 input=9bd36317b806033d]*/
177+
_heapq_heappop_impl(PyObject *module, PyObject *heap)
178+
/*[clinic end generated code: output=96dfe82d37d9af76 input=91487987a583c856]*/
189179
{
190180
return heappop_internal(heap, siftup);
191181
}
@@ -195,11 +185,6 @@ heapreplace_internal(PyObject *heap, PyObject *item, int siftup_func(PyListObjec
195185
{
196186
PyObject *returnitem;
197187

198-
if (!PyList_Check(heap)) {
199-
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
200-
return NULL;
201-
}
202-
203188
if (PyList_GET_SIZE(heap) == 0) {
204189
PyErr_SetString(PyExc_IndexError, "index out of range");
205190
return NULL;
@@ -219,7 +204,7 @@ heapreplace_internal(PyObject *heap, PyObject *item, int siftup_func(PyListObjec
219204
/*[clinic input]
220205
_heapq.heapreplace
221206
222-
heap: object
207+
heap: object(subclass_of='&PyList_Type')
223208
item: object
224209
/
225210
@@ -236,15 +221,15 @@ this routine unless written as part of a conditional replacement:
236221

237222
static PyObject *
238223
_heapq_heapreplace_impl(PyObject *module, PyObject *heap, PyObject *item)
239-
/*[clinic end generated code: output=82ea55be8fbe24b4 input=e57ae8f4ecfc88e3]*/
224+
/*[clinic end generated code: output=82ea55be8fbe24b4 input=719202ac02ba10c8]*/
240225
{
241226
return heapreplace_internal(heap, item, siftup);
242227
}
243228

244229
/*[clinic input]
245230
_heapq.heappushpop
246231
247-
heap: object
232+
heap: object(subclass_of='&PyList_Type')
248233
item: object
249234
/
250235
@@ -256,16 +241,11 @@ a separate call to heappop().
256241

257242
static PyObject *
258243
_heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item)
259-
/*[clinic end generated code: output=67231dc98ed5774f input=eb48c90ba77b2214]*/
244+
/*[clinic end generated code: output=67231dc98ed5774f input=5dc701f1eb4a4aa7]*/
260245
{
261246
PyObject *returnitem;
262247
int cmp;
263248

264-
if (!PyList_Check(heap)) {
265-
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
266-
return NULL;
267-
}
268-
269249
if (PyList_GET_SIZE(heap) == 0) {
270250
Py_INCREF(item);
271251
return item;
@@ -367,11 +347,6 @@ heapify_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
367347
{
368348
Py_ssize_t i, n;
369349

370-
if (!PyList_Check(heap)) {
371-
PyErr_SetString(PyExc_TypeError, "heap argument must be a list");
372-
return NULL;
373-
}
374-
375350
/* For heaps likely to be bigger than L1 cache, we use the cache
376351
friendly heapify function. For smaller heaps that fit entirely
377352
in cache, we prefer the simpler algorithm with less branching.
@@ -396,15 +371,15 @@ heapify_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
396371
/*[clinic input]
397372
_heapq.heapify
398373
399-
heap: object
374+
heap: object(subclass_of='&PyList_Type')
400375
/
401376
402377
Transform list into a heap, in-place, in O(len(heap)) time.
403378
[clinic start generated code]*/
404379

405380
static PyObject *
406-
_heapq_heapify(PyObject *module, PyObject *heap)
407-
/*[clinic end generated code: output=11483f23627c4616 input=872c87504b8de970]*/
381+
_heapq_heapify_impl(PyObject *module, PyObject *heap)
382+
/*[clinic end generated code: output=e63a636fcf83d6d0 input=53bb7a2166febb73]*/
408383
{
409384
return heapify_internal(heap, siftup);
410385
}
@@ -508,23 +483,23 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
508483
/*[clinic input]
509484
_heapq._heappop_max
510485
511-
heap: object
486+
heap: object(subclass_of='&PyList_Type')
512487
/
513488
514489
Maxheap variant of heappop.
515490
[clinic start generated code]*/
516491

517492
static PyObject *
518-
_heapq__heappop_max(PyObject *module, PyObject *heap)
519-
/*[clinic end generated code: output=acd30acf6384b13c input=62ede3ba9117f541]*/
493+
_heapq__heappop_max_impl(PyObject *module, PyObject *heap)
494+
/*[clinic end generated code: output=9e77aadd4e6a8760 input=362c06e1c7484793]*/
520495
{
521496
return heappop_internal(heap, siftup_max);
522497
}
523498

524499
/*[clinic input]
525500
_heapq._heapreplace_max
526501
527-
heap: object
502+
heap: object(subclass_of='&PyList_Type')
528503
item: object
529504
/
530505
@@ -534,23 +509,23 @@ Maxheap variant of heapreplace.
534509
static PyObject *
535510
_heapq__heapreplace_max_impl(PyObject *module, PyObject *heap,
536511
PyObject *item)
537-
/*[clinic end generated code: output=8ad7545e4a5e8adb input=6d8f25131e0f0e5f]*/
512+
/*[clinic end generated code: output=8ad7545e4a5e8adb input=f2dd27cbadb948d7]*/
538513
{
539514
return heapreplace_internal(heap, item, siftup_max);
540515
}
541516

542517
/*[clinic input]
543518
_heapq._heapify_max
544519
545-
heap: object
520+
heap: object(subclass_of='&PyList_Type')
546521
/
547522
548523
Maxheap variant of heapify.
549524
[clinic start generated code]*/
550525

551526
static PyObject *
552-
_heapq__heapify_max(PyObject *module, PyObject *heap)
553-
/*[clinic end generated code: output=1c6bb6b60d6a2133 input=cdfcc6835b14110d]*/
527+
_heapq__heapify_max_impl(PyObject *module, PyObject *heap)
528+
/*[clinic end generated code: output=2cb028beb4a8b65e input=c1f765ee69f124b8]*/
554529
{
555530
return heapify_internal(heap, siftup_max);
556531
}

Modules/clinic/_heapqmodule.c.h

Lines changed: 97 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)