Mercurial > cpython-lru_cache-weakref
annotate Objects/enumobject.c @ 47916:bed3dfd99281 legacy-trunk
Issue 3287: Raise correct exception for float inputs.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Thu, 10 Jul 2008 09:31:08 +0000 |
| parents | 5d1aff46cc0d |
| children | 4203b7f087c1 |
| rev | line source |
|---|---|
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /* enumerate object */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 #include "Python.h" |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 typedef struct { |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 PyObject_HEAD |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 long en_index; /* current index of enumeration */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 PyObject* en_sit; /* secondary iterator of enumeration */ |
| 29289 | 9 PyObject* en_result; /* result tuple */ |
|
43457
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
10 PyObject* en_longindex; /* index for sequences >= LONG_MAX */ |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 } enumobject; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 static PyObject * |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 { |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 enumobject *en; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 PyObject *seq = NULL; |
|
46982
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
18 PyObject *start = NULL; |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
19 static char *kwlist[] = {"sequence", "start", 0}; |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
46982
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
21 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist, |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
22 &seq, &start)) |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 return NULL; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 en = (enumobject *)type->tp_alloc(type, 0); |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 if (en == NULL) |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 return NULL; |
|
46982
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
28 if (start) { |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
29 start = PyNumber_Index(start); |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
30 if (start == NULL) { |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
31 Py_DECREF(en); |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
32 return NULL; |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
33 } |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
34 if (PyLong_Check(start)) { |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
35 en->en_index = LONG_MAX; |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
36 en->en_longindex = start; |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
37 } else { |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
38 assert(PyInt_Check(start)); |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
39 en->en_index = PyInt_AsLong(start); |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
40 en->en_longindex = NULL; |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
41 Py_DECREF(start); |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
42 } |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
43 } else { |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
44 en->en_index = 0; |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
45 en->en_longindex = NULL; |
|
47692f6c815e
#2831: add start argument to enumerate(). Patch by Scott Dial and me.
Georg Brandl <georg@python.org>
parents:
44350
diff
changeset
|
46 } |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 en->en_sit = PyObject_GetIter(seq); |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 if (en->en_sit == NULL) { |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 Py_DECREF(en); |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 return NULL; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 } |
|
30835
562143c1c7ae
Use PyTuple_Pack() to simplify enumerate().
Raymond Hettinger <python@rcn.com>
parents:
29289
diff
changeset
|
52 en->en_result = PyTuple_Pack(2, Py_None, Py_None); |
| 29289 | 53 if (en->en_result == NULL) { |
| 54 Py_DECREF(en); | |
| 55 return NULL; | |
| 56 } | |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 return (PyObject *)en; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 } |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 static void |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 enum_dealloc(enumobject *en) |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 { |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 PyObject_GC_UnTrack(en); |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 Py_XDECREF(en->en_sit); |
| 29289 | 65 Py_XDECREF(en->en_result); |
|
43457
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
66 Py_XDECREF(en->en_longindex); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43457
diff
changeset
|
67 Py_TYPE(en)->tp_free(en); |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 } |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 static int |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 enum_traverse(enumobject *en, visitproc visit, void *arg) |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 { |
|
37756
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37589
diff
changeset
|
73 Py_VISIT(en->en_sit); |
|
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37589
diff
changeset
|
74 Py_VISIT(en->en_result); |
|
43457
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
75 Py_VISIT(en->en_longindex); |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 return 0; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 } |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 static PyObject * |
|
43457
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
80 enum_next_long(enumobject *en, PyObject* next_item) |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
81 { |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
82 static PyObject *one = NULL; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
83 PyObject *result = en->en_result; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
84 PyObject *next_index; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
85 PyObject *stepped_up; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
86 |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
87 if (en->en_longindex == NULL) { |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
88 en->en_longindex = PyInt_FromLong(LONG_MAX); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
89 if (en->en_longindex == NULL) |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
90 return NULL; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
91 } |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
92 if (one == NULL) { |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
93 one = PyInt_FromLong(1); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
94 if (one == NULL) |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
95 return NULL; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
96 } |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
97 next_index = en->en_longindex; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
98 assert(next_index != NULL); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
99 stepped_up = PyNumber_Add(next_index, one); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
100 if (stepped_up == NULL) |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
101 return NULL; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
102 en->en_longindex = stepped_up; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
103 |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
104 if (result->ob_refcnt == 1) { |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
105 Py_INCREF(result); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
106 Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
107 Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
108 } else { |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
109 result = PyTuple_New(2); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
110 if (result == NULL) { |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
111 Py_DECREF(next_index); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
112 Py_DECREF(next_item); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
113 return NULL; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
114 } |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
115 } |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
116 PyTuple_SET_ITEM(result, 0, next_index); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
117 PyTuple_SET_ITEM(result, 1, next_item); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
118 return result; |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
119 } |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
120 |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
121 static PyObject * |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 enum_next(enumobject *en) |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 { |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 PyObject *next_index; |
|
24261
27e94cacb43b
Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
125 PyObject *next_item; |
| 29289 | 126 PyObject *result = en->en_result; |
| 127 PyObject *it = en->en_sit; | |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43457
diff
changeset
|
129 next_item = (*Py_TYPE(it)->tp_iternext)(it); |
| 29289 | 130 if (next_item == NULL) |
|
24261
27e94cacb43b
Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
131 return NULL; |
|
27e94cacb43b
Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
132 |
|
43457
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
133 if (en->en_index == LONG_MAX) |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
134 return enum_next_long(en, next_item); |
|
b12cf467b05c
enumerate() is no longer bounded to using sequences shorter than LONG_MAX. The possibility of overflow was sending some newsgroup posters into a tizzy.
Raymond Hettinger <python@rcn.com>
parents:
42333
diff
changeset
|
135 |
|
24261
27e94cacb43b
Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
136 next_index = PyInt_FromLong(en->en_index); |
|
27e94cacb43b
Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
137 if (next_index == NULL) { |
| 29289 | 138 Py_DECREF(next_item); |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 return NULL; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 } |
| 29289 | 141 en->en_index++; |
| 142 | |
| 143 if (result->ob_refcnt == 1) { | |
| 144 Py_INCREF(result); | |
| 145 Py_DECREF(PyTuple_GET_ITEM(result, 0)); | |
| 146 Py_DECREF(PyTuple_GET_ITEM(result, 1)); | |
| 147 } else { | |
| 148 result = PyTuple_New(2); | |
| 149 if (result == NULL) { | |
| 150 Py_DECREF(next_index); | |
| 151 Py_DECREF(next_item); | |
| 152 return NULL; | |
| 153 } | |
| 154 } | |
|
24261
27e94cacb43b
Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
155 PyTuple_SET_ITEM(result, 0, next_index); |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 PyTuple_SET_ITEM(result, 1, next_item); |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 return result; |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 } |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23285
diff
changeset
|
160 PyDoc_STRVAR(enum_doc, |
|
28711
820d9570edeb
Add a useful docstring to enumerate.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28320
diff
changeset
|
161 "enumerate(iterable) -> iterator for index, value of iterable\n" |
|
820d9570edeb
Add a useful docstring to enumerate.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28320
diff
changeset
|
162 "\n" |
|
820d9570edeb
Add a useful docstring to enumerate.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28320
diff
changeset
|
163 "Return an enumerate object. iterable must be an other object that supports\n" |
|
820d9570edeb
Add a useful docstring to enumerate.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28320
diff
changeset
|
164 "iteration. The enumerate object yields pairs containing a count (from\n" |
|
820d9570edeb
Add a useful docstring to enumerate.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28320
diff
changeset
|
165 "zero) and a value yielded by the iterable argument. enumerate is useful\n" |
|
820d9570edeb
Add a useful docstring to enumerate.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28320
diff
changeset
|
166 "for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ..."); |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 PyTypeObject PyEnum_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
40766
diff
changeset
|
169 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 "enumerate", /* tp_name */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 sizeof(enumobject), /* tp_basicsize */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 0, /* tp_itemsize */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 /* methods */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 (destructor)enum_dealloc, /* tp_dealloc */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 0, /* tp_print */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 0, /* tp_getattr */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 0, /* tp_setattr */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 0, /* tp_compare */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 0, /* tp_repr */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 0, /* tp_as_number */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 0, /* tp_as_sequence */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 0, /* tp_as_mapping */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 0, /* tp_hash */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 0, /* tp_call */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 0, /* tp_str */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 PyObject_GenericGetAttr, /* tp_getattro */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 0, /* tp_setattro */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 0, /* tp_as_buffer */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 enum_doc, /* tp_doc */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 (traverseproc)enum_traverse, /* tp_traverse */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 0, /* tp_clear */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 0, /* tp_richcompare */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 0, /* tp_weaklistoffset */ |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
196 PyObject_SelfIter, /* tp_iter */ |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 (iternextfunc)enum_next, /* tp_iternext */ |
|
24261
27e94cacb43b
Remove the next() method -- one is supplied automatically by
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
198 0, /* tp_methods */ |
|
23285
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 0, /* tp_members */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 0, /* tp_getset */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 0, /* tp_base */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 0, /* tp_dict */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 0, /* tp_descr_get */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 0, /* tp_descr_set */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 0, /* tp_dictoffset */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 0, /* tp_init */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 PyType_GenericAlloc, /* tp_alloc */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 enum_new, /* tp_new */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 PyObject_GC_Del, /* tp_free */ |
|
afe263672fd8
- New builtin function enumerate(x), from PEP 279. Example:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 }; |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
211 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
212 /* Reversed Object ***************************************************************/ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
213 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
214 typedef struct { |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
215 PyObject_HEAD |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
216 Py_ssize_t index; |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
217 PyObject* seq; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
218 } reversedobject; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
219 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
220 static PyObject * |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
221 reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
222 { |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
223 Py_ssize_t n; |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
224 PyObject *seq; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
225 reversedobject *ro; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
226 |
|
47096
5d1aff46cc0d
Don't allow keyword arguments to reversed().
Georg Brandl <georg@python.org>
parents:
46982
diff
changeset
|
227 if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds)) |
|
5d1aff46cc0d
Don't allow keyword arguments to reversed().
Georg Brandl <georg@python.org>
parents:
46982
diff
changeset
|
228 return NULL; |
|
5d1aff46cc0d
Don't allow keyword arguments to reversed().
Georg Brandl <georg@python.org>
parents:
46982
diff
changeset
|
229 |
|
5d1aff46cc0d
Don't allow keyword arguments to reversed().
Georg Brandl <georg@python.org>
parents:
46982
diff
changeset
|
230 if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) ) |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
231 return NULL; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
232 |
|
31466
f04ccc288542
* Fix ref counting in extend() and extendleft().
Raymond Hettinger <python@rcn.com>
parents:
30874
diff
changeset
|
233 if (PyObject_HasAttrString(seq, "__reversed__")) |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
234 return PyObject_CallMethod(seq, "__reversed__", NULL); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
235 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
236 if (!PySequence_Check(seq)) { |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
237 PyErr_SetString(PyExc_TypeError, |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
238 "argument to reversed() must be a sequence"); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
239 return NULL; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
240 } |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
241 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
242 n = PySequence_Size(seq); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
243 if (n == -1) |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
244 return NULL; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
245 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
246 ro = (reversedobject *)type->tp_alloc(type, 0); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
247 if (ro == NULL) |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
248 return NULL; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
249 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
250 ro->index = n-1; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
251 Py_INCREF(seq); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
252 ro->seq = seq; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
253 return (PyObject *)ro; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
254 } |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
255 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
256 static void |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
257 reversed_dealloc(reversedobject *ro) |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
258 { |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
259 PyObject_GC_UnTrack(ro); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
260 Py_XDECREF(ro->seq); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43457
diff
changeset
|
261 Py_TYPE(ro)->tp_free(ro); |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
262 } |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
263 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
264 static int |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
265 reversed_traverse(reversedobject *ro, visitproc visit, void *arg) |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
266 { |
|
37756
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37589
diff
changeset
|
267 Py_VISIT(ro->seq); |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
268 return 0; |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
269 } |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
270 |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
271 static PyObject * |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
272 reversed_next(reversedobject *ro) |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
273 { |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
274 PyObject *item; |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
275 Py_ssize_t index = ro->index; |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
276 |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
277 if (index >= 0) { |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
278 item = PySequence_GetItem(ro->seq, index); |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
279 if (item != NULL) { |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
280 ro->index--; |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
281 return item; |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
282 } |
|
31908
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
283 if (PyErr_ExceptionMatches(PyExc_IndexError) || |
|
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
284 PyErr_ExceptionMatches(PyExc_StopIteration)) |
|
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
285 PyErr_Clear(); |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
286 } |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
287 ro->index = -1; |
|
33386
c2c49ee727d0
SF patch #1020188: Use Py_CLEAR where necessary to avoid crashes
Raymond Hettinger <python@rcn.com>
parents:
33273
diff
changeset
|
288 Py_CLEAR(ro->seq); |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
289 return NULL; |
|
31502
59538d3b7cd2
Make reversed() transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31478
diff
changeset
|
290 } |
|
59538d3b7cd2
Make reversed() transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31478
diff
changeset
|
291 |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
292 PyDoc_STRVAR(reversed_doc, |
| 33273 | 293 "reversed(sequence) -> reverse iterator over values of the sequence\n" |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
294 "\n" |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
295 "Return a reverse iterator"); |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
296 |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
297 static PyObject * |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
298 reversed_len(reversedobject *ro) |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
299 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
300 Py_ssize_t position, seqsize; |
|
31908
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
301 |
|
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
302 if (ro->seq == NULL) |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
303 return PyInt_FromLong(0); |
|
31908
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
304 seqsize = PySequence_Size(ro->seq); |
|
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
305 if (seqsize == -1) |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
306 return NULL; |
|
31908
058821d8aefd
* Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents:
31635
diff
changeset
|
307 position = ro->index + 1; |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
308 return PyInt_FromSsize_t((seqsize < position) ? 0 : position); |
|
31635
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
309 } |
|
04eb2ebb46e2
Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents:
31634
diff
changeset
|
310 |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
36002
diff
changeset
|
311 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
312 |
|
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
313 static PyMethodDef reversediter_methods[] = { |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
36002
diff
changeset
|
314 {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc}, |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
315 {NULL, NULL} /* sentinel */ |
|
31478
f30aaac00195
Let reversed() work with itself.
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
316 }; |
|
f30aaac00195
Let reversed() work with itself.
Raymond Hettinger <python@rcn.com>
parents:
31466
diff
changeset
|
317 |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
318 PyTypeObject PyReversed_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
40766
diff
changeset
|
319 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
320 "reversed", /* tp_name */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
321 sizeof(reversedobject), /* tp_basicsize */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
322 0, /* tp_itemsize */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
323 /* methods */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
324 (destructor)reversed_dealloc, /* tp_dealloc */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
325 0, /* tp_print */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
326 0, /* tp_getattr */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
327 0, /* tp_setattr */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
328 0, /* tp_compare */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
329 0, /* tp_repr */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
330 0, /* tp_as_number */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
331 0, /* tp_as_sequence */ |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
332 0, /* tp_as_mapping */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
333 0, /* tp_hash */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
334 0, /* tp_call */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
335 0, /* tp_str */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
336 PyObject_GenericGetAttr, /* tp_getattro */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
337 0, /* tp_setattro */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
338 0, /* tp_as_buffer */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
339 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
340 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
341 reversed_doc, /* tp_doc */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
342 (traverseproc)reversed_traverse,/* tp_traverse */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
343 0, /* tp_clear */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
344 0, /* tp_richcompare */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
345 0, /* tp_weaklistoffset */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
346 PyObject_SelfIter, /* tp_iter */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
347 (iternextfunc)reversed_next, /* tp_iternext */ |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
33386
diff
changeset
|
348 reversediter_methods, /* tp_methods */ |
|
30865
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
349 0, /* tp_members */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
350 0, /* tp_getset */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
351 0, /* tp_base */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
352 0, /* tp_dict */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
353 0, /* tp_descr_get */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
354 0, /* tp_descr_set */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
355 0, /* tp_dictoffset */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
356 0, /* tp_init */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
357 PyType_GenericAlloc, /* tp_alloc */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
358 reversed_new, /* tp_new */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
359 PyObject_GC_Del, /* tp_free */ |
|
41c4e143e6e6
Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents:
30835
diff
changeset
|
360 }; |
