HEX
Server: Apache
System: Linux pdx1-shared-a1-29 6.18.39-grsec-noble+ #4 SMP Thu Jul 23 07:53:29 UTC 2026 x86_64
User: dh_bj99yp (22285841)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/genshi/template/__pycache__/eval.cpython-312.pyc
�

EMabYQ���dZddlmZddlmZddlZddlmZddlm	Z	ddl
mZmZm
Z
ddlmZdd	lmZdd
lmZmZmZmZmZmZmZgd�ZdZGd
�de�ZGd�de�ZGd�de�Z e�Z!Gd�de�Z"Gd�de�Z#Gd�de�Z$Gd�de$�Z%Gd�de$�Z&d&d�Z'		d'd�Z(d�Z)ejTjW�Z,e,j[e	e#d ��e.gd!��Z/Gd"�d#e�Z0Gd$�d%e0�Z1y)(z4Support for "safe" evaluation of Python expressions.�)�dedent)�CodeTypeN)�builtins)�Markup)�ASTTransformer�ASTCodeGenerator�parse)�TemplateRuntimeError)�flatten)�ast�
_ast_Constant�get_code_params�build_code_chunk�isstring�
IS_PYTHON2�_ast_Str)�Code�
Expression�Suite�
LenientLookup�StrictLookup�	Undefined�UndefinedErrorzrestructuredtext enc�H�eZdZdZgd�Z		dd�Zd�Zd�Zd�Zd�Z	d	�Z
d
�Zy)rz=Abstract base class for the `Expression` and `Suite` classes.)�source�coder�_globalsNc�D�t|tj�r||_t	||j
��}nrt|tj�s
Jd|z��d|_|j
dk(rt
j�}||_	nt
j�}|g|_	||_t||j|j
|||��|_
|�t}n*t|tj�rttd�|}|j |_y)aCreate the code object, either from a string, or from an AST node.
        
        :param source: either a string containing the source code, or an AST
                       node
        :param filename: the (preferably absolute) name of the file containing
                         the code
        :param lineno: the number of the line on which the code was found
        :param lookup: the lookup class that defines how variables are looked
                       up in the context; can be either "strict" (the default),
                       "lenient", or a custom lookup class
        :param xform: the AST transformer that should be applied to the code;
                      if `None`, the appropriate transformation is chosen
                      depending on the mode
        )�modez'Expected string or AST node, but got %r�?�eval)r�filename�lineno�xformN)�lenient�strict)�
isinstance�six�string_typesr�_parser�_ast�ASTr�body�Moduler�_compilerrr�globalsr)�selfrr"r#�lookupr$�nodes       �6/usr/lib/python3/dist-packages/genshi/template/eval.py�__init__z
Code.__init__(s��� �f�c�.�.�/� �D�K��&�t�y�y�1�D��f�d�h�h�/�
C�9�F�B�
C�/��D�K��y�y�F�"����(��"��	��{�{�}��#�H��	�����T�4�;�;�T�Y�Y�&.�v�U�L��	��>�"�F�
��� 0� 0�
1�!.�,�G��O�F�����
�c���t|jd�r|jj}n|jj}|j|j
|d�}t
|j�|d<|S)N�__self__)rrr2r)�hasattrrr8�im_selfrrrr)r1�	lookup_fn�states   r4�__getstate__zCode.__getstate__OsY���4�=�=�*�-��
�
�.�.�I��
�
�-�-�I��;�;�t�x�x�9�M��'��	�	�2��f�
��r6c�|�|d|_|d|_tdg|d���|_|dj|_y)Nrrrrr2)rrrrr0r)r1r<s  r4�__setstate__zCode.__setstate__Ys>���H�o�����<����Q�/��v��/��	��h��/�/��
r6c�f�t|�t|�k(xr|j|jk(S�N)�typer�r1�others  r4�__eq__zCode.__eq___s'���U��t�D�z�)�H��	�	�U�Z�Z�0G�Hr6c�,�t|j�SrA)�hashr�r1s r4�__hash__z
Code.__hash__bs���D�I�I��r6c��||k(SrA�rCs  r4�__ne__zCode.__ne__es���5�=� � r6c�L�t|�j�d|j�d�S)N�(�))rB�__name__rrHs r4�__repr__z
Code.__repr__hs����:�.�.����<�<r6)N���r&N)rP�
__module__�__qualname__�__doc__�	__slots__r5r=r?rErIrLrQrKr6r4rr$s5��G�5�I�@H��%'�N�0�I��!�=r6rc��eZdZdZgZdZd�Zy)ra�Evaluates Python expressions used in templates.

    >>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
    >>> Expression('test').evaluate(data)
    'Foo'

    >>> Expression('items[0]').evaluate(data)
    1
    >>> Expression('items[-1]').evaluate(data)
    3
    >>> Expression('dict["some"]').evaluate(data)
    'thing'
    
    Similar to e.g. Javascript, expressions in templates can use the dot
    notation for attribute access to access items in mappings:
    
    >>> Expression('dict.some').evaluate(data)
    'thing'
    
    This also works the other way around: item access can be used to access
    any object attribute:
    
    >>> class MyClass(object):
    ...     myattr = 'Bar'
    >>> data = dict(mine=MyClass(), key='myattr')
    >>> Expression('mine.myattr').evaluate(data)
    'Bar'
    >>> Expression('mine["myattr"]').evaluate(data)
    'Bar'
    >>> Expression('mine[key]').evaluate(data)
    'Bar'
    
    All of the standard Python operators are available to template expressions.
    Built-in functions such as ``len()`` are also available in template
    expressions:
    
    >>> data = dict(items=[1, 2, 3])
    >>> Expression('len(items)').evaluate(data)
    3
    r!c�Z�d}|j|�}t|j|d|i�S)z�Evaluate the expression against the given data dictionary.
        
        :param data: a mapping containing the data to evaluate against
        :return: the result of the evaluation
        �before_and_this�__data__)rr!r�r1�data�__traceback_hide__rs    r4�evaluatezExpression.evaluate�s0��/���=�=��&���D�I�I�x�*�d�);�<�<r6N)rPrSrTrUrVrr^rKr6r4rrls��'�P�I��D�=r6rc��eZdZdZgZdZd�Zy)rz�Executes Python statements used in templates.

    >>> data = dict(test='Foo', items=[1, 2, 3], dict={'some': 'thing'})
    >>> Suite("foo = dict['some']").execute(data)
    >>> data['foo']
    'thing'
    �execc�l�d}|j|�}tj|j||�y)z�Execute the suite in the given data dictionary.
        
        :param data: a mapping containing the data to execute in
        rYN)rr(�exec_rr[s    r4�executez
Suite.execute�s,��
/���=�=��&���	�	�$�)�)�X�t�,r6N)rPrSrTrUrVrrcrKr6r4rr�s����I��D�-r6rc��eZdZdZefd�Zy)rz�Exception thrown when a template expression attempts to access a variable
    not defined in the context.
    
    :see: `LenientLookup`, `StrictLookup`
    c�n�|turt|��d|�d�}nd|z}tj||�y)Nz has no member named "�"z"%s" not defined)�	UNDEFINED�reprr
r5)r1�name�owner�messages    r4r5zUndefinedError.__init__�s2���	�!�7;�E�{�D�I�G�(�4�/�G��%�%�d�G�4r6N)rPrSrTrUrgr5rKr6r4rr�s���
$-�5r6rc�T�eZdZdZddgZefd�Zd�Zd�ZeZ	d�Z
d�Zd	�ZexZ
xZZd
Zy
)ra�Represents a reference to an undefined variable.
    
    Unlike the Python runtime, template expressions can refer to an undefined
    variable without causing a `NameError` to be raised. The result will be an
    instance of the `Undefined` class, which is treated the same as ``False`` in
    conditions, but raise an exception on any other operation:
    
    >>> foo = Undefined('foo')
    >>> bool(foo)
    False
    >>> list(foo)
    []
    >>> print(foo)
    undefined
    
    However, calling an undefined variable, or trying to access an attribute
    of that variable, will raise an exception that includes the name used to
    reference that undefined variable.
    
    >>> try:
    ...     foo('bar')
    ... except UndefinedError as e:
    ...     print(e.msg)
    "foo" not defined

    >>> try:
    ...     foo.bar
    ... except UndefinedError as e:
    ...     print(e.msg)
    "foo" not defined
    
    :see: `LenientLookup`
    �_name�_ownerc� �||_||_y)z�Initialize the object.
        
        :param name: the name of the reference
        :param owner: the owning object, if the variable is accessed as a member
        N)rmrn)r1rirjs   r4r5zUndefined.__init__�s����
���r6c��tg�SrA)�iterrHs r4�__iter__zUndefined.__iter__�s���B�x�r6c��y)NFrKrHs r4�__bool__zUndefined.__bool__�s��r6c�N�dt|�j�d|j�d�S)N�<� �>)rBrPrmrHs r4rQzUndefined.__repr__�s�� ��J�/�/����<�<r6c��y)N�	undefinedrKrHs r4�__str__zUndefined.__str__s��r6c�F�d}t|j|j��)zRaise an `UndefinedError`.T)rrmrn)r1�args�kwargsr]s    r4�_diezUndefined._dies��!���T�Z�Z����5�5r6N)rPrSrTrUrVrgr5rrrt�__nonzero__rQr{r�__call__�__getattr__�__getitem__�__length_hint__rKr6r4rr�sT�� �B�(�#�I�#,�����K�=��6�,0�/�H�/�{�[��Or6rc�d�eZdZdZed��Zed��Zed��Zed��Zee	fd��Z
y)�
LookupBasez8Abstract base class for variable lookup implementations.c�V�||j|j|jtd�S)znConstruct the globals dictionary to use as the execution context for
        the expression or suite.
        )rZ�_lookup_name�_lookup_attr�_lookup_itemr)�lookup_name�lookup_attr�lookup_itemr)�clsr\s  r4r0zLookupBase.globalss)����O�O��O�O��O�O�,�
�	
r6c��d}|j|t�}|tur/tj||�}|tur|j|�}|S)NT)�getrg�BUILTINSrz)r�r\rir]�vals     r4r�zLookupBase.lookup_namesK��!���h�h�t�Y�'���)���,�,�t�S�)�C��i���m�m�D�)���
r6c���d}	t||�}|S#t$rKt|j|�r�	||}Y|S#tt
f$r|j
||��}YY|SwxYwwxYw)NT�rj)�getattr�AttributeErrorr9�	__class__�KeyError�	TypeErrorrz)r��obj�keyr]r�s     r4r�zLookupBase.lookup_attr)s���!��		8��#�s�#�C��
���	8��s�}�}�c�*��8��c�(�C��
��!�)�,�8��-�-��3�-�7�C��
�8��
	8�s$�� A&�;�"A"�A&�!A"�"A&c��d}t|�dk(r|d}	||S#ttttf$rS}t|tj�r3t||t�}|tur|j||��}|cYd}~S�d}~wwxYw)NT�rr�)�lenr�r��
IndexErrorr�r'r(r)r�rgrz)r�r�r�r]�er�s      r4r�zLookupBase.lookup_item8s���!���s�8�q�=��a�&�C�	��s�8�O����*�i�@�	��#�s�/�/�0��c�3�	�2���)�#��-�-��3�-�7�C��
���
	�s!��B�AB�<B�B�Bc��t�)z�Can be overridden by subclasses to specify behavior when undefined
        variables are accessed.
        
        :param key: the name of the variable
        :param owner: the owning object, if the variable is accessed as a member
        )�NotImplementedError)r�r�rjs   r4rzzLookupBase.undefinedGs
��"�!r6N)rPrSrTrU�classmethodr0r�r�r�rgrzrKr6r4r�r�sj��B��

��

��������������"+�"��"r6r�c�$�eZdZdZeefd��Zy)ra�Default variable lookup mechanism for expressions.
    
    When an undefined variable is referenced using this lookup style, the
    reference evaluates to an instance of the `Undefined` class:
    
    >>> expr = Expression('nothing', lookup='lenient')
    >>> undef = expr.evaluate({})
    >>> undef
    <Undefined 'nothing'>
    
    The same will happen when a non-existing attribute or item is accessed on
    an existing object:
    
    >>> expr = Expression('something.nil', lookup='lenient')
    >>> expr.evaluate({'something': dict()})
    <Undefined 'nil'>
    
    See the documentation of the `Undefined` class for details on the behavior
    of such objects.
    
    :see: `StrictLookup`
    c� �d}t||��S)zReturn an ``Undefined`` object.Tr�)r�r�r�rjr]s    r4rzzLenientLookup.undefinedjs��"����E�*�*r6N�rPrSrTrUr�rgrzrKr6r4rrRs���.�"+�+��+r6rc�$�eZdZdZeefd��Zy)ra�Strict variable lookup mechanism for expressions.
    
    Referencing an undefined variable using this lookup style will immediately
    raise an ``UndefinedError``:
    
    >>> expr = Expression('nothing', lookup='strict')
    >>> try:
    ...     expr.evaluate({})
    ... except UndefinedError as e:
    ...     print(e.msg)
    "nothing" not defined
    
    The same happens when a non-existing attribute or item is accessed on an
    existing object:
    
    >>> expr = Expression('something.nil', lookup='strict')
    >>> try:
    ...     expr.evaluate({'something': dict()})
    ... except UndefinedError as e:
    ...     print(e.msg)
    {} has no member named "nil"
    c� �d}t||���)z(Raise an ``UndefinedError`` immediately.Tr�)rr�s    r4rzzStrictLookup.undefined�s��"���S��.�.r6Nr�rKr6r4rrqs���.�"+�/��/r6rc�X�|j�}|dk(r�|j�D�cgc]}|j���}}|r�|d}tdj	|dd��j�}|j�j
d�rC|dj�s0dj	|j�D�cgc]}d|z��	c}�}dj	||g�}t|tj�rd|zjd�}t||�Scc}wcc}w)	Nr`r�
r��:z    %su�utf-8)
�strip�
splitlines�
expandtabsr�join�rstrip�endswith�isspacer'r(�	text_type�encoder	)rr�line�lines�first�rests      r4r*r*�s���
�\�\�^�F��v�~�/5�/@�/@�/B�C�t����"�C��C���!�H�E��$�)�)�E�!�"�I�.�/�6�6�8�D��|�|�~�&�&�s�+�D��G�O�O�4E��y�y�d�o�o�>O�!P�d�(�T�/�!P�Q���Y�Y��t�}�-�F��&�#�-�-�(��f�$�,�,�W�5��������D��
"Qs�D"�D'c�D�|sd}tr-t|tj�r?|j	dd�}n,t|tj�s|jdd�}|dkrd}|�dtij|t�}|�j|�}|dk(r
d|xsdz}n2|j�}|sd	}	n|d}	t|�dkDr|	d
z
}	d|	z}t|�j}
t|
||�}	t||||�S#t $r|cYSwxYw)Nz<string>r��replacerr�r!z<Expression %r>r �z ...z
<Suite %r>)rr'r(r�r��decode�ExpressionASTTransformerr��TemplateASTTransformer�visitr�r�rr�compiler�RuntimeError)r3rrr"r#r$�treerir��extract�
new_sourcers            r4r/r/�s(�������h��
�
�.����w�	�:�H��(�C�M�M�2����w�	�:�H�
��{����}��,�
�

�#�d�*�
+�	��7�=�=���D��v�~� �F�M�c�2���!�!�#����G��A�h�G��u�:��>��v��G��w�'��!�$�'�,�,�J��:�x��.�D�� ��h��f�=�=�������s�
D�D�Dc��|�}t|j|�D]!\}}||vrtd��t|||��#|D]\}}t|||��|S)Nz!Field set both in args and kwargs)�zip�_fields�
ValueError�setattr)�class_r}r~�ret�attr�values      r4�_newr��sl��
�(�C��3�;�;��-�"���e��6�>��@�A�A���T�5�!�"��"���e���T�5�!�"��Jr6)rr)�False�True�None�NotImplemented�Ellipsisc�\�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�ZeZ
d�Zd�Zd
�Zy)r��pConcrete AST transformer that implements the AST transformations needed
    for code embedded in templates.
    c��tg|_yrA)�	CONSTANTS�localsrHs r4r5zTemplateASTTransformer.__init__�s�� �k��r6c�&�ts6t|tj�r|j	|j�yt|�r|j	|�yt|tj�r|j	|j�yt|tj�r*|j	|jxs|j�yt|tj�r$|jD]}|j||��yyrA)rr'r+�arg�addr�Name�id�alias�asnameri�Tuple�elts�_process)r1�namesr3�elts    r4r�zTemplateASTTransformer._process�s����j��t�x�x�8��I�I�d�h�h��
�d�^��I�I�d�O�
��d�i�i�
(��I�I�d�g�g��
��d�j�j�
)��I�I�d�k�k�.�T�Y�Y�/�
��d�j�j�
)��y�y�
*���
�
�e�S�)�
*�*r6c���t�}t|d�r�|jD]}|j||��t|d�r#|jD]}|j||��t|d�r|j||j
�t|d�r|j||j�|St|d�r#|jD]}|j||��|S)Nr}�
kwonlyargs�vararg�kwargr�)�setr9r}r�r�r�r�r�)r1r3r�r�r�s     r4�_extract_namesz%TemplateASTTransformer._extract_names�s�������4�� ��y�y�
*���
�
�e�S�)�
*��t�\�*��?�?�.�C��M�M�%��-�.��t�X�&��
�
�e�T�[�[�1��t�W�%��
�
�e�T�Z�Z�0����T�7�
#��z�z�
*���
�
�e�S�)�
*��r6c��t|jtj�s	|jj	d�|S|S#t
$r,t
t|jj	d��cYSwxYw)N�asciir�)r'�sr(r�r�r�r�r�r1r3s  r4�	visit_Strz TemplateASTTransformer.visit_Strs_���$�&�&�#�-�-�0�
>����
�
�g�&���t����
>��H�d�f�f�m�m�G�&<�=�=�
>�s�A�2A:�9A:c�h�t|j�dkDr(|jdj|j�|jj	t��	t
j||�|jj�S#|jj�wxYw�Nr�rR)	r�r�r�ri�appendr�r�visit_ClassDef�popr�s  r4r�z%TemplateASTTransformer.visit_ClassDefss���t�{�{��a���K�K��O����	�	�*������3�5�!�	�!�0�0��t�<��K�K�O�O���D�K�K�O�O��s�%B�B1c��t|j�dkDr-|jdj|j|��t	j
||�Sr�)r�r��updater�r�visit_Importr�s  r4r�z#TemplateASTTransformer.visit_ImportsG���t�{�{��a���K�K��O�"�"�4�#6�#6�t�#<�=��*�*�4��6�6r6c��|jD�cgc]}|j��c}dgk(r|St|j�dkDr-|jdj	|j|��t
j||�Scc}w)N�*r�rR)r�rir�r�r�r�r�visit_ImportFrom)r1r3�as   r4r�z'TemplateASTTransformer.visit_ImportFromsn�� �J�J�'�q�A�F�F�'�C�5�0��K��t�{�{��a���K�K��O�"�"�4�#6�#6�t�#<�=��.�.�t�T�:�:��	(s�Bc��t|j�dkDr(|jdj|j�|jj	|j|j��	tj||�|jj�S#|jj�wxYwr�)
r�r�r�rir�r�r}r�visit_FunctionDefr�r�s  r4r�z(TemplateASTTransformer.visit_FunctionDefs����t�{�{��a���K�K��O����	�	�*������4�.�.�t�y�y�9�:�	�!�3�3�D�$�?��K�K�O�O���D�K�K�O�O��s�6B&�&Cc�.�g}|jD]�}|jjt��t	t
j|j|j�|j|j�|jD�cgc]}|j|���c}�}|j|���t	|j|j|j�|�}|jt|j�d�=|Scc}wrA)�
generatorsr�r�r�r�r+�
comprehensionr��targetrq�ifsr�r�r�)r1r3�gens�	generator�if_�genr�s       r4�visit_GeneratorExpz)TemplateASTTransformer.visit_GeneratorExp(s��������	�I��K�K���s�u�%��t�)�)�4�:�:�i�6F�6F�+G��z�z�)�.�.�1�3<�=�=�A�C��
�
�3��A�C�C�
�K�K���
	��4�>�>�4�:�:�d�h�h�#7��>���K�K��T�_�_�-�-�.�/��
��Bs�Dc�
�|jj|j|j��	t	j
||�|jj
�S#|jj
�wxYwrA)r�r�r�r}r�visit_Lambdar�r�s  r4rz#TemplateASTTransformer.visit_Lambda;sU�������4�.�.�t�y�y�9�:�	�!�.�.�t�T�:��K�K�O�O���D�K�K�O�O��s�A&�&Bc�F�|j|j�|_|SrA)r�r�r�s  r4�
visit_Starredz$TemplateASTTransformer.visit_StarredCs���Z�Z��
�
�+��
��r6c��t|jtj�r�|jt|j�vr�ttjdtj��}ttjdtj��}tt|j�}ttj|||gg�}|St|jtj�r@t|j�dkDr(|jdj|j�|S)Nr�rZr�rR)r'�ctxr+�Loadr�rr�r�r�r�Call�Storer�r�)r1r3ri�namearg�strargs     r4�
visit_Namez!TemplateASTTransformer.visit_NameGs����d�h�h��	�	�*����w�t�{�{�3�3���	�	�>�4�9�9�;�?�D��4�9�9�j�$�)�)�+�>�G��(�D�G�G�,�F���	�	�4�'�6�):�B�?�D�
��	����$�*�*�
-��4�;�;��!�#����B��#�#�D�G�G�,��r6N)rPrSrTrUr5r�r�r�r�r�r�r�r	�visit_ListComprr
rrKr6r4r�r��sJ���"�*��"��7�
;���"(�N���r6r�c��eZdZdZd�Zd�Zy)r�r�c�r�t|jtj�st	j
||�St
tjdtj��}|j|j�t
t|j�g}t
tj||g�S)Nr�)
r'rr+rr�visit_Attributer�r�r�r�rr�r)r1r3�funcr}s    r4rz(ExpressionASTTransformer.visit_Attribute]sv���$�(�(�D�I�I�.�!�1�1�$��=�=��D�I�I�~�t�y�y�{�;���
�
�4�:�:�&��X�t�y�y�(A�B���D�I�I�t�T�2�.�.r6c��t|jtj�rHt|jtj
ttjtjf�stj||�St|jtjtjf�r
|j}n|jj}ttjdtj��}|j|j�ttj|j|�ftj��g}ttj||g�S)Nr�)r'rr+r�slice�Indexr
r�rr�visit_Subscriptr�r�r�r�)r1r3�slice_valuerr}s     r4rz(ExpressionASTTransformer.visit_Subscriptes����$�(�(�D�I�I�.��t�z�z�D�J�J�
�t�y�y�RV�R[�R[�+\�]�!�1�1�$��=�=��d�j�j�4�9�9�d�i�i�"8�9��*�*�K��*�*�*�*�K��D�I�I�~�t�y�y�{�;���J�J�t�z�z�"�����d�j�j��5�7�����E�
���D�I�I�t�T�2�.�.r6N)rPrSrTrUrrrKr6r4r�r�Xs���/�/r6r�)r!)Nr!NrRN)2rU�textwrapr�typesrr(�	six.movesr�genshi.corer�genshi.template.astutilrrr	�genshi.template.baser
�genshi.utilr�
genshi.compatrr+r
rrrrr�__all__�
__docformat__�objectrrrrgrrr�rrr*r/r��__dict__�copyr�r��	frozensetr�r�r�rKr6r4�<module>r.s��;���
���K�K�5��K�K�K�*��%�
�E=�6�E=�P4=��4=�n-�D�-�*
�H�	�5�)�5�C��C�L@"��@"�F+�J�+�>/�:�/�>�DF��(�V����!�!�#�����6�	�:�;��M�N�	�{�^�{�|/�5�/r6