enable .pop() optimisation also for typed lists
authorStefan Behnel <scoder@users.berlios.de>
Tue, 4 May 2010 05:55:20 +0000 (07:55 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Tue, 4 May 2010 05:55:20 +0000 (07:55 +0200)
Cython/Compiler/Optimize.py
tests/run/list_pop.pyx

index 42fb3ecd4a561c0292c15b92891fef10fe0c8b6d..3a36989d9086c0ce54fdfcf4c3bf6be97687469a 100644 (file)
@@ -1693,6 +1693,8 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
                 
         return node
 
+    _handle_simple_method_list_pop = _handle_simple_method_object_pop
+
     PyList_Append_func_type = PyrexTypes.CFuncType(
         PyrexTypes.c_int_type, [
             PyrexTypes.CFuncTypeArg("list", PyrexTypes.py_object_type, None),
index f1f5f71f67f122aebf43f9fe128c79a5c30a61da..15161867516776254f93c0f1283a443a6bb4cd18 100644 (file)
@@ -32,6 +32,30 @@ def simple_pop(L):
     """
     return L.pop()
 
+@cython.test_assert_path_exists('//PythonCapiCallNode')
+@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
+def simple_pop_typed(list L):
+    """
+    >>> L = list(range(10))
+    >>> simple_pop_typed(L)
+    9
+    >>> simple_pop_typed(L)
+    8
+    >>> L
+    [0, 1, 2, 3, 4, 5, 6, 7]
+    >>> while L:
+    ...    _ = simple_pop_typed(L)
+    
+    >>> L
+    []
+    >>> simple_pop_typed(L)
+    Traceback (most recent call last):
+    ...
+    IndexError: pop from empty list
+    """
+    return L.pop()
+
+
 @cython.test_assert_path_exists('//PythonCapiCallNode')
 @cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
 def index_pop(L, int i):
@@ -68,6 +92,40 @@ def index_pop(L, int i):
     """
     return L.pop(i)
 
+@cython.test_assert_path_exists('//PythonCapiCallNode')
+@cython.test_fail_if_path_exists('//SimpleCallNode/AttributeNode')
+def index_pop_typed(list L, int i):
+    """
+    >>> L = list(range(10))
+    >>> index_pop_typed(L, 2)
+    2
+    >>> index_pop_typed(L, -2)
+    8
+    >>> L
+    [0, 1, 3, 4, 5, 6, 7, 9]
+    >>> index_pop_typed(L, 100)
+    Traceback (most recent call last):
+    ...
+    IndexError: pop index out of range
+    >>> index_pop_typed(L, -100)
+    Traceback (most recent call last):
+    ...
+    IndexError: pop index out of range
+    
+    >>> while L:
+    ...    _ = index_pop_typed(L, 0)
+    
+    >>> L
+    []
+    
+    >>> index_pop_typed(L, 0)
+    Traceback (most recent call last):
+    ...
+    IndexError: pop from empty list
+    """
+    return L.pop(i)
+
+
 @cython.test_fail_if_path_exists('//PythonCapiCallNode')
 def crazy_pop(L):
     """