Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
5c45b258
Commit
5c45b258
authored
Nov 05, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement builtin next() to make it available in Py2
parent
2e67e464
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
0 deletions
+122
-0
Cython/Compiler/Builtin.py
Cython/Compiler/Builtin.py
+37
-0
tests/run/builtin_next.pyx
tests/run/builtin_next.pyx
+85
-0
No files found.
Cython/Compiler/Builtin.py
View file @
5c45b258
...
...
@@ -39,6 +39,8 @@ builtin_function_table = [
#('map', "", "", ""),
#('max', "", "", ""),
#('min', "", "", ""),
(
'next'
,
"O"
,
"O"
,
"__Pyx_PyIter_Next"
),
# not available in Py2 => implemented here
(
'next'
,
"OO"
,
"O"
,
"__Pyx_PyIter_Next2"
),
# not available in Py2 => implemented here
#('oct', "", "", ""),
# Not worth doing open, when second argument would become mandatory
#('open', "ss", "O", "PyFile_FromString"),
...
...
@@ -158,6 +160,40 @@ proto = """
#define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None)
"""
)
iter_next_utility_code
=
UtilityCode
(
proto
=
"""
#define __Pyx_PyIter_Next(obj) __Pyx_PyIter_Next2(obj, NULL);
static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject *, PyObject *); /*proto*/
"""
,
# copied from Py3's builtin_next()
impl
=
'''
static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject* iterator, PyObject* defval) {
PyObject* next;
if (unlikely(!PyIter_Check(iterator))) {
PyErr_Format(PyExc_TypeError,
"%.200s object is not an iterator", iterator->ob_type->tp_name);
return NULL;
}
next = (*iterator->ob_type->tp_iternext)(iterator);
if (likely(next)) {
return next;
} else if (defval) {
if (PyErr_Occurred()) {
if(!PyErr_ExceptionMatches(PyExc_StopIteration))
return NULL;
PyErr_Clear();
}
Py_INCREF(defval);
return defval;
} else if (PyErr_Occurred()) {
return NULL;
} else {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}
}
'''
)
getattr3_utility_code
=
UtilityCode
(
proto
=
"""
static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
...
...
@@ -390,6 +426,7 @@ builtin_utility_code = {
'exec'
:
pyexec_utility_code
,
'getattr3'
:
getattr3_utility_code
,
'pow'
:
pow2_utility_code
,
'next'
:
iter_next_utility_code
,
'intern'
:
intern_utility_code
,
'set'
:
py23_set_utility_code
,
'frozenset'
:
py23_set_utility_code
,
...
...
tests/run/builtin_next.pyx
0 → 100644
View file @
5c45b258
import
sys
IS_PY3
=
sys
.
version_info
[
0
]
>=
3
__doc__
=
"""
>>> it = iter([1,2,3])
>>> if not IS_PY3:
... next = type(it).next
>>> next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
StopIteration
>>> next(it)
Traceback (most recent call last):
StopIteration
>>> if IS_PY3: next(it, 123)
... else: print(123)
123
"""
def
test_single_next
(
it
):
"""
>>> it = iter([1,2,3])
>>> test_single_next(it)
1
>>> test_single_next(it)
2
>>> test_single_next(it)
3
>>> test_single_next(it)
Traceback (most recent call last):
StopIteration
>>> test_single_next(it)
Traceback (most recent call last):
StopIteration
"""
return
next
(
it
)
def
test_default_next
(
it
,
default
):
"""
>>> it = iter([1,2,3])
>>> test_default_next(it, 99)
1
>>> test_default_next(it, 99)
2
>>> test_default_next(it, 99)
3
>>> test_default_next(it, 99)
99
>>> test_default_next(it, 99)
99
"""
return
next
(
it
,
default
)
def
test_next_not_iterable
(
it
):
"""
>>> test_next_not_iterable(123)
Traceback (most recent call last):
TypeError: int object is not an iterator
"""
return
next
(
it
)
def
test_next_override
(
it
):
"""
>>> it = iter([1,2,3])
>>> test_next_override(it)
1
>>> test_next_override(it)
1
>>> test_next_override(it)
1
>>> test_next_override(it)
1
"""
def
next
(
it
):
return
1
return
next
(
it
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment