Sindbad~EG File Manager
�
'Μg~] � � � d dl Z d dlZd dlZd dlZd dlZd dlmZ dZ G d� dej � Z deiZ
d� Zedk( r ej � yy) � N)�
BrokenItera�
########### Tests borrowed from or inspired by test_genexps.py ############
Test simple loop with conditional
>>> sum([i*i for i in range(100) if i&1 == 1])
166650
Test simple nesting
>>> [(i,j) for i in range(3) for j in range(4)]
[(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)]
Test nesting with the inner expression dependent on the outer
>>> [(i,j) for i in range(4) for j in range(i)]
[(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)]
Test the idiom for temporary variable assignment in comprehensions.
>>> [j*j for i in range(4) for j in [i+1]]
[1, 4, 9, 16]
>>> [j*k for i in range(4) for j in [i+1] for k in [j+1]]
[2, 6, 12, 20]
>>> [j*k for i in range(4) for j, k in [(i+1, i+2)]]
[2, 6, 12, 20]
Not assignment
>>> [i*i for i in [*range(4)]]
[0, 1, 4, 9]
>>> [i*i for i in (*range(4),)]
[0, 1, 4, 9]
Make sure the induction variable is not exposed
>>> i = 20
>>> sum([i*i for i in range(100)])
328350
>>> i
20
Verify that syntax error's are raised for listcomps used as lvalues
>>> [y for y in (1,2)] = 10 # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SyntaxError: ...
>>> [y for y in (1,2)] += 10 # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
SyntaxError: ...
########### Tests borrowed from or inspired by test_generators.py ############
Make a nested list comprehension that acts like range()
>>> def frange(n):
... return [i for i in range(n)]
>>> frange(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Same again, only as a lambda expression instead of a function definition
>>> lrange = lambda n: [i for i in range(n)]
>>> lrange(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Generators can call other generators:
>>> def grange(n):
... for x in [i for i in range(n)]:
... yield x
>>> list(grange(5))
[0, 1, 2, 3, 4]
Make sure that None is a valid return value
>>> [None for i in range(10)]
[None, None, None, None, None, None, None, None, None, None]
c � � � e Zd Zddddefd�Zd� Zd� Zd� Z� fd�Zd� Z d � Z
d
� Zd� Zd� Z
d
� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Z d � Z!d!� Z"d"� Z#d#� Z$d$� Z%d%� Z&d&� Z'd'� Z(d(� Z)d)� Z*d*� Z+d+� Z,d,� Z-d-� Z.d.� Z/d/� Z0d0� Z1d1� Z2d2� Z3d3� Z4d4� Z5d5� Z6d6� Z7d7� Z8d8� Z9d9� Z:d:� Z;d;� Z<d<� Z=d=� Z>d>� Z?� xZ@S )?�ListComprehensionTestN� c � � t j |� }|xs g d�}|D ]� }| j |�� 5 |dk( r=t j d� j t j |d� �� }d� } nG|dk( r=t j d � j t j |d� �� }d
� } n|}d� } |r|j � ni }
|||
� |xs i j
� D ] \ }}| j | |
|� ||� �! d d d � �� y # |$ r%}
| j t |
� |� Y d }
~
�0d }
~
ww xY w# 1 sw Y ��6xY w)N)�module�class�function)�scoper zZ
class _C:
{code}
z )�codec � � t | d |� S )N�_C)�getattr��moddict�names �0/usr/local/lib/python3.12/test/test_listcomps.py�
get_outputz:ListComprehensionTest._check_in_scopes.<locals>.get_outputn s � �&�w�t�}�d�;�;� r
z�
def _f():
{code}
return locals()
_out = _f()
c � � | d | S )N�_outr r s r r z:ListComprehensionTest._check_in_scopes.<locals>.get_outputw s � �&�v��t�4�4r c � � | | S �Nr r s r r z:ListComprehensionTest._check_in_scopes.<locals>.get_output{ s � �&�t�}�,r )
�textwrap�dedent�subTest�format�indent�copy�items�assertEqual�assertIs�type)�selfr �outputs�ns�scopes�raises� exec_funcr �newcoder �newns�k�v�es r �_check_in_scopesz&ListComprehensionTest._check_in_scopesc sD � ����t�$���:�:���E����E��*��G�#�&�o�o� /� � ��X�_�_�T�6�%B��C� �<��j�(�&�o�o� /� �
��X�_�_�T�6�%B��C� �5� #�G�-�%'���� �R��E��g�u�-�
")��B� 5� 5� 7���1��(�(��E�1�)=�q�!�D� !8�9 +�*� ��2 � 3��M�M�$�q�'�6�2�2��3��1 +�*�s6 �BE� D�7E�E �$E�?E�E � E�E c �8 � d}dg d�i}| j ||� y )Nzf
items = [(lambda i=i: i) for i in range(5)]
y = [x() for x in items]
�y)r � � � � �r/ �r$ r r% s r �*test_lambdas_with_iteration_var_as_defaultz@ListComprehensionTest.test_lambdas_with_iteration_var_as_default� �% � ��� ��(�����d�G�,r c �8 � d}dg d�i}| j ||� y )Nzb
items = [(lambda: i) for i in range(5)]
y = [x() for x in items]
r1 �r5 r5 r5 r5 r5 r6 r7 s r �test_lambdas_with_free_varz0ListComprehensionTest.test_lambdas_with_free_var� r9 r c � � G d� d� }| j |j g d�� | j |� j � |� y )Nc � ��� e Zd Z�fd�Z ed� D � ��cg c] ��fd��� c}} ZeD � �cg c] } |� �� c}} Z�xZS c c}} w c c}} w )�JListComprehensionTest.test_class_scope_free_var_with_class_cell.<locals>.Cc � �� t � �S r )�super)r$ � __class__s �r �methodzQListComprehensionTest.test_class_scope_free_var_with_class_cell.<locals>.C.method� s �� ��� � r � c � �� � S r r )�is �r �<lambda>zSListComprehensionTest.test_class_scope_free_var_with_class_cell.<locals>.C.<lambda>� s �� �ar )�__name__�
__module__�__qualname__rC �ranger r1 �
__classcell__)�.0rF �xrB s 0p0@r �Cr? � s@ �� �
!� +0��(�3�(�Q�i�(�3�E�#�$�e����e�$�A�� 4��$s
�A�A rO r; )r! r1 r"