Ë
     åhiJ<  ã                   óæ  — d Z ddlZddlZddlmZ ddlmZ ddlmZ  e	edej                  «      Z
defd„Zej                  ej                  z  fd	efd
„Zd	edefd„Zej$                  sdZeZdZdZdZdZd„ Znqej4                  sdZdZdZdZdZdZ ed«      Zd„ ZnMdZeZej8                  rdZndZdZej8                  rdZndZdZej:                  r ed«      Zd„ Zn ed«      Zd„ Z edez   d z   ez   d!z   ez   d"z   ez   d#z   ez   d$z   ez   d%z   ez   d&z   ez   d'z   «      Z ed(ez   d)z   ez   d*z   «      Zd+efd,„Z d-ed.ed/efd0„Z!d-ed.ed/e"fd1„Z#d.ed/e"fd2„Z$d8d3e%d.ed/e&fd4„Z'd.ed/e&fd5„Z(d6efd7„Z)y)9aX  
Tools for searching bytecode for key statements that indicate the need for additional resources, such as data files
and package metadata.

By *bytecode* I mean the ``code`` object given by ``compile()``, accessible from the ``__code__`` attribute of any
non-builtin function or, in PyInstallerLand, the ``PyiModuleGraph.node("some.module").code`` attribute. The best
guide for bytecode format I have found is the disassembler reference: https://docs.python.org/3/library/dis.html

This parser implementation aims to combine the flexibility and speed of regex with the clarity of the output of
``dis.dis(code)``. It has not achieved the 2nd, but C'est la vie...

The biggest clarity killer here is the ``EXTENDED_ARG`` opcode which can appear almost anywhere and therefore needs
to be tiptoed around at every step. If this code needs to expand significantly, I would recommend an upgrade to a
regex-based grammar parsing library such as Reparse. This way, little steps like unpacking ``EXTENDED_ARGS`` can be
defined once then simply referenced forming a nice hierarchy rather than copied everywhere its needed.
é    N)ÚCodeType)ÚPattern)ÚcompatÚ
_all_opmapÚxc                 óN   — t        j                  t        t        |    g«      «      S )zG
    Get a regex-escaped opcode byte from its human readable name.
    )ÚreÚescapeÚbytesÚopmap)r   s    úGC:\des-py\Monitor\venv\Lib\site-packages\PyInstaller/depend/bytecode.pyÚ_instruction_to_regexr   )   s   € ô �9‰9”UœE !™H˜:Ó&Ó'Ð'ó    Úpatternc                 ó„   — t        | t        «      sJ ‚t        j                  dd„ | «      } t        j                  | |¬«      S )aÂ  
    A regex-powered Python bytecode matcher.

    ``bytecode_regex`` provides a very thin wrapper around :func:`re.compile`.

      * Any opcode names wrapped in backticks are substituted for their corresponding opcode bytes.
      * Patterns are compiled in VERBOSE mode by default so that whitespace and comments may be used.

    This aims to mirror the output of :func:`dis.dis`, which is far more readable than looking at raw byte strings.
    s   `(\w+)`c                 ó:   — t        | d   j                  «       «      S )Né   )r   Údecode)Úms    r   ú<lambda>z bytecode_regex.<locals>.<lambda>@   s   € Ô'¨¨!©¯©«Ó6€ r   )Úflags)Ú
isinstancer   r	   ÚsubÚcompile)r   r   s     r   Úbytecode_regexr   0   s@   € ô �gœuÔ%Ð%Ð%ô �f‰fØÙ6Øó€Gô
 �:‰:�g UÔ+Ð+r   Ústringc              #   óø   K  — t        |t        «      sJ ‚t        |«      }| j                  |«      }	 |D ]A  }|j	                  «       dz  dk(  r|–— Œ| j                  ||j	                  «       dz   «      } n yŒH­w)a  
    Call ``pattern.finditer(string)``, but remove any matches beginning on an odd byte (i.e., matches where
    match.start() is not a multiple of 2).

    This should be used to avoid false positive matches where a bytecode pair's argument is mistaken for an opcode.
    é   r   r   N)r   r   Ú_cleanup_bytecode_stringÚfinditerÚstart)r   r   ÚmatchesÚmatchs       r   r    r    F   sˆ   è ø€ ô �fœeÔ$Ð$Ð$Ü% fÓ-€FØ×Ñ˜vÓ&€GØ
Øò 	ˆEØ�{‰{‹}˜qÑ  AÒ%à“ð "×*Ñ*¨6°5·;±;³=À1Ñ3DÓE�Ùð	ð ð ùs   ‚A8A:s   `EXTENDED_ARG`s%   `LOAD_NAME`|`LOAD_GLOBAL`|`LOAD_FAST`s   `LOAD_ATTR`|`LOAD_METHOD`s   `LOAD_CONST`s0   `CALL_FUNCTION`|`CALL_METHOD`|`CALL_FUNCTION_EX`c                 ó   — | S ©N© ©Úbytecodes    r   r   r   p   s   € Øˆr   s#   `EXTENDED_ARG`|`EXTENDED_ARG_QUICK`s"   `EXTENDED_ARG``EXTENDED_ARG_QUICK`s   `PRECALL`|`CALL_FUNCTION_EX`s   (`CACHE`.)|(..)c                 ó.   — t         j                  d| «      S ©Ns   \2©Ú_cache_instruction_filterr   r'   s    r   r   r   ƒ   s   € Ü(×,Ñ,¨V°XÓ>Ð>r   s8   `LOAD_NAME`|`LOAD_GLOBAL`|`LOAD_FAST`|`LOAD_FAST_BORROW`s   `LOAD_ATTR`s3   `LOAD_CONST`|`LOAD_SMALL_INT`|`LOAD_CONST_IMMORTAL`s   `CALL`|`CALL_FUNCTION_EX`s   (`CACHE`.)|(`PUSH_NULL`.)|(..)c                 ó.   — t         j                  d| «      S )Ns   \3r+   r'   s    r   r   r       ó   € Ü,×0Ñ0°¸ÓBÐBr   c                 ó.   — t         j                  d| «      S r*   r+   r'   s    r   r   r   ¥   r.   r   s¼   
    # Matches `global_function('some', 'constant', 'arguments')`.

    # Load the global function. In code with >256 of names, this may require extended name references.
    (
     (?:(?:s   ).)*
     (?:(?:sÎ   ).)
    )

    # For foo.bar.whizz(), the above is the 'foo', below is the 'bar.whizz' (one opcode per name component, each
    # possibly preceded by name reference extension).
    (
     (?:
       (?:(?:s   ).)*
       (?:sÈ   ).
     )*
    )

    # Load however many arguments it takes. These (for now) must all be constants.
    # Again, code with >256 constants may need extended enumeration.
    (
      (?:
        (?:(?:s   ).)*
        (?:sÉ   ).
      )*
    )

    # Call the function. If opcode is CALL_FUNCTION_EX, the parameter are flags. For other opcodes, the parameter
    # is the argument count (which may be > 256).
    (
      (?:(?:s   ).)*
      (?:s	   ).
    )
s:   (
    # Arbitrary number of EXTENDED_ARG pairs.
    (?:(?:sG   ).)*

    # Followed by some other instruction (usually a LOAD).
    [^s   ].
)Úextended_argsc                 ó:   — t         j                  | ddd…   d«      S )aQ  
    Unpack the (extended) integer used to reference names or constants.

    The input should be a bytecode snippet of the following form::

        EXTENDED_ARG    ?      # Repeated 0-4 times.
        LOAD_xxx        ?      # Any of LOAD_NAME/LOAD_CONST/LOAD_METHOD/...

    Each ? byte combined together gives the number we want.
    r   Nr   Úbig)ÚintÚ
from_bytes)r0   s    r   Úextended_argumentsr5   Û   s    € ô �>‰>˜-¨¨¨1¨Ñ-¨uÓ5Ð5r   ÚrawÚcodeÚreturnc                 ól  — t        | «      }| d   t        d   k(  r|j                  |   S | d   t        d   k(  r|j                  |   S t        j
                  r!| d   t        d   k(  r|j                  |dz	     S t        j                  r!| d   t        d   k(  r|j                  |dz	     S t        j                  r| d   t        d   k(  r|S t        j                  r| d   t        d   k(  r|j                  |   S t        j                  r| d   t        d	   k(  r|j                  |   S |j                  |   S )
z3
    Parse an (extended) LOAD_xxx instruction.
    éþÿÿÿÚ	LOAD_FASTÚ
LOAD_CONSTÚLOAD_GLOBALr   Ú	LOAD_ATTRÚLOAD_SMALL_INTÚLOAD_CONST_IMMORTALÚLOAD_FAST_BORROW)	r5   r   Úco_varnamesÚ	co_constsr   Úis_py311Úco_namesÚis_py312Úis_py314)r6   r7   Úindexs      r   ÚloadrI   é   s&  € ô
 ˜sÓ#€Eð
 ˆ2�w”%˜Ñ$Ò$à×Ñ Ñ&Ð&à
ˆ2�w”%˜Ñ%Ò%à�~‰~˜eÑ$Ð$ä‡‚˜3˜r™7¤e¨MÑ&:Ò:à�}‰}˜U a™ZÑ(Ð(Ü‡‚˜3˜r™7¤e¨KÑ&8Ò8à�}‰}˜U a™ZÑ(Ð(Ü‡‚˜3˜r™7¤eÐ,<Ñ&=Ò=àˆÜ‡‚˜3˜r™7¤eÐ,AÑ&BÒBð �~‰~˜eÑ$Ð$Ü‡‚˜3˜r™7¤eÐ,>Ñ&?Ò?ð ×Ñ Ñ&Ð&à�=‰=˜ÑÐr   c                 óf   — t         j                  | «      D �cg c]  }t        ||«      ‘Œ c}S c c}w )zµ
    Parse multiple consecutive LOAD_xxx instructions. Or load() in a for loop.

    May be used to unpack a function's parameters or nested attributes ``(foo.bar.pop.whack)``.
    )Ú_extended_arg_bytecodeÚfindallrI   )r6   r7   Úis      r   ÚloadsrN     s)   € ô $:×#AÑ#AÀ#Ó#FÖG˜aŒD��D�MÒGÐGùÒGs   ˜.c                 óà  — g }t        t        | j                  «      D ]Î  }|j                  «       \  }}}}t	        || «      }t        || «      }dj                  |g|z   «      }t        || «      }|d   t        d   k(  rBt        |«      }|dk7  rŒqt        |«      dk7  st        |d   t        «      sŒ“t        |d   «      }nt        |«      }	|	t        |«      k7  rŒ¼|j                  ||f«       ŒÐ |S )zJ
    Scan a code object for all function calls on constant arguments.
    ú.r   ÚCALL_FUNCTION_EXr   )r    Ú_call_function_bytecodeÚco_codeÚgroupsrI   rN   Újoinr   r5   Úlenr   ÚtupleÚlistÚappend)
r7   Úoutr#   Úfunction_rootÚmethodsÚargsÚfunction_callÚfunctionr   Ú	arg_counts
             r   Úfunction_callsra     sõ   € ð
 €CäÔ1°4·<±<Ó@ò %ˆØ6;·l±l³nÑ3ˆ�w  mô ˜]¨DÓ1ˆÜ˜ Ó&ˆØ—8‘8˜]˜O¨gÑ5Ó6ˆä�T˜4Ó ˆØ˜ÑœuÐ%7Ñ8Ò8Ü& }Ó5ˆEØ˜Šzàô �4‹y˜AŠ~¤Z°°Q±¼Ô%?ØÜ˜˜Q™“=‰Dä*¨=Ó9ˆIàœC ›IÒ%àà�
‰
�H˜dÐ#Õ$ð;%ð> €Jr   Úsearchc                 óŠ   — |€i }||vr: | |«      ||<   |j                   D ]   }t        |t        «      sŒt        | ||«       Œ" |S )zm
    Apply a search function to a code object, recursing into child code objects (function definitions).
    )rC   r   r   Úsearch_recursively)rb   r7   Ú_memoÚconsts       r   rd   rd   B  sS   € ð €}ØˆØ�5ÑÙ˜T“lˆˆd‰Ø—^‘^ò 	9ˆEÜ˜%¤Õ*Ü" 6¨5°%Õ8ð	9ð €Lr   c                 ó"   — t        t        | «      S )z‘
    Scan a code object for function calls on constant arguments, recursing into function definitions and bodies of
    comprehension loops.
    )rd   ra   )r7   s    r   Úrecursive_function_callsrh   P  s   € ô
 œn¨dÓ3Ð3r   Ú	full_namec              #   ój   K  — | j                  d«      }|rdj                  |«      –— |dd }|rŒyy­w)aÏ  List possible aliases of a fully qualified Python name.

        >>> list(any_alias("foo.bar.wizz"))
        ['foo.bar.wizz', 'bar.wizz', 'wizz']

    This crudely allows us to capture uses of wizz() under any of
    ::
        import foo
        foo.bar.wizz()
    ::
        from foo import bar
        bar.wizz()
    ::
        from foo.bar import wizz
        wizz()

    However, it will fail for any form of aliases and quite likely find false matches.
    rP   r   N)ÚsplitrU   )ri   Úpartss     r   Ú	any_aliasrm   X  s7   è ø€ ð& �O‰O˜CÓ €EÙ
Ø�h‰h�u‹oÒØ�a�b�	ˆô ùs   ‚.3±3r%   )*Ú__doc__Údisr	   Útypesr   Útypingr   ÚPyInstallerr   Úgetattrr   Ústrr   ÚVERBOSEÚDOTALLr   r   r    rD   Ú_OPCODES_EXTENDED_ARGÚ_OPCODES_EXTENDED_ARG2Ú_OPCODES_FUNCTION_GLOBALÚ_OPCODES_FUNCTION_LOADÚ_OPCODES_FUNCTION_ARGSÚ_OPCODES_FUNCTION_CALLr   rF   r,   rG   Úis_py313rR   rK   r5   rI   rX   rN   ra   ÚcallableÚdictrd   rh   rm   r&   r   r   ú<module>r€      sÏ  ðñó" Û 	Ý Ý å ñ
 	��\ 3§9¡9Ó-€ð(˜Só (ð *,¯©°b·i±iÑ)?ñ ,˜Eó ,ð,�gð  uó ðB ‡‚à.ÐØ2ÐØHÐØ:ÐØ-ÐØQÐóà	�Šð
 DÐØCÐØHÐØ:ÐØ-ÐØ=Ðñ
 !/Ð/BÓ CÐó?ð
 /ÐØ2ÐØ‡‚à#_Ñ à#LÐ Ø,ÐØ‡‚ð "YÑà!1ÐØ:Ðð ‡‚Ù$2Ð3UÓ$VÐ!ó	Cñ %3Ð3FÓ$GÐ!ò	Cñ
 )ðð
 'ñ'ð
*ñð *ñ*ð-ñð )ñ)ð,ñð 'ñ'ð*ñð, *ñ-*ð,-ñ-ð. (ñ/(ð.+ñ/ð< (ñ=(ð<+ñ=ð> &ñ?&ð>)ñ?!ó#Ð ñL (ðà%ñ&ð)
ñ
ð
 #ñ#ð
&ñóÐ ð6 eó 6ð$ ˆeð $ ˜8ð $ ¨ó $ ðNHˆuð H˜Hð H¨ó Hð&˜ð & dó &ñR˜xð ¨xð Èó ð4 8ð 4°ó 4ð˜ô r   