o
    àý°j„  ã                   @   sF  d Z ddlZddlZddlZddlZddlmZ dd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zejeƒ dd�Zejdd�Zejddd�Zejddd d!� ejd"ejd#d$d%�d&d'� ejd(d)d� ejd*d+d�Zejd,d-d� ejd.ejd#d$d%�d/d'� ejd0ejd#d$d%�d1d'� ejd(d)d� d4d2d3„ZdS )5aê  Implementation of the ``cffi-gen-src`` command-line tool.

This module is private; the command line is the only supported
interface. Two subcommands:

``exec-python``
    Execute a Python script that constructs a :class:`cffi.FFI`
    (the same kind of script that the CFFI docs' "Main mode of usage"
    describes) and emit the generated C source.

``read-sources``
    Create the :class:`cffi.FFI` from a separate ``cdef`` file and C
    source prelude, then emit the generated C source.
é    Né   )ÚFFIc                 C   s   t | |dd�}t|||ƒ d S )NÚexec)ÚsourceÚfilenameÚmode)Úcompiler   )Úpysrcr   ÚglobsÚcompiled© r   ú…/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/cffi/_cffi_gen_src.pyÚ	_execfile3   s   r   c              
   C   sº   t j |¡}d|dœ}tjdd… }tj dt j |¡¡ z7t| ||ƒ ||vr/td|f ƒ‚|| }t|t	ƒs?t
|ƒr?|ƒ }t|t	ƒsKtd|f ƒ‚|W |tjdd…< S |tjdd…< w )a
  Execute ``pysrc`` and return the :class:`FFI` object it defines.

    The script is executed with ``__name__`` set to ``"cffi.gen_src"``,
    so a trailing ``if __name__ == "__main__": ffibuilder.compile()``
    block in the script is skipped.

    ``ffivar`` is the name bound by the script to the :class:`FFI`
    object, or to a callable that returns one.

    Raises :class:`NameError` if the name is not bound by the script,
    or :class:`TypeError` if the name does not resolve to an
    :class:`FFI` instance.
    zcffi.gen_src)Ú__name__Ú__file__Nr   zGExpected to find the FFI object with the name %r, but it was not found.zKFound an object with the name %r but it was not an instance of cffi.api.FFI)ÚosÚpathÚabspathÚsysÚinsertÚdirnamer   Ú	NameErrorÚ
isinstancer   ÚcallableÚ	TypeError)r	   r   Úffivarr
   Úold_pathÚffir   r   r   Úfind_ffi_in_python_script8   s.   
ÿÿ
ÿÿ r   c                 C   s    t ƒ }| |¡ | | |¡ |S )zACreate an :class:`FFI` from ``cdef`` text and a C source prelude.)r   ÚcdefÚ
set_source)Ú
modulenamer   ÚcsrcÚ
ffibuilderr   r   r   Úmake_ffi_from_sources_   s   
r$   c                 C   s   t  ¡ }|  |¡ | ¡ S )z=Return the C source that :meth:`FFI.emit_c_code` would write.)ÚioÚStringIOÚemit_c_codeÚgetvalue)r   Úoutputr   r   r   Úgenerate_c_sourceg   s   
r*   c                 C   sV   | dkrt j |¡ d S t| ddd��}| |¡ W d   ƒ d S 1 s$w   Y  d S )Nú-Úwúutf-8©Úencoding)r   ÚstdoutÚwriteÚopen)r)   Ú	generatedÚfr   r   r   Úwrite_c_sourcen   s   "ÿr5   c                 C   s6   | |u rdS z
t j | j|j¡W S  ty   Y dS w )NTF)r   r   ÚsamefileÚnameÚOSError)Úfile1Úfile2r   r   r   Úsame_input_filev   s   ÿr;   c                 C   sL   |� t | ¡ |j|ƒ}W d   ƒ n1 sw   Y  t|ƒ}t| |ƒ d S ©N)r   Úreadr7   r*   r5   )r)   ÚpyfileÚffi_varr   r3   r   r   r   Úexec_python   s
   ÿr@   c              	   C   sz   |�" |� |  ¡ }|  ¡ }W d   ƒ n1 sw   Y  W d   ƒ n1 s'w   Y  t|||ƒ}t|ƒ}t| |ƒ d S r<   )r=   r$   r*   r5   )r)   Úmodule_nameÚ
cdef_inputÚ
csrc_inputr"   r   r   r3   r   r   r   Úread_sources†   s   
þ€ rD   c                  C   s.   t jrtj t jd ¡nd} |  d¡rdS dS )Nr   Ú zcffi-gen-srczpython -m cffi.gen_src)r   Úargvr   r   ÚbasenameÚ
startswith)Úargv0r   r   r   Ú_prog�   s   
rJ   z-Generate CFFI C source for extension modules.)ÚprogÚdescriptionr   )Údestúexec-pythonz2Execute a Python script that defines an FFI object)Úhelpz	--ffi-varr#   zFName of the FFI object in the Python script; defaults to 'ffibuilder'.)ÚdefaultrO   r>   Úrr-   r.   zPath to the Python script)ÚtyperO   r)   zOutput path for the C sourceúread-sourcesz>Read cdef and C source prelude files that define an FFI objectrA   z5Full name of the generated module, including packagesr   zFile containing C definitionsr"   z File containing C source preludec                 C   s€   t j| d�} | jdkrt| j| j| jd� n#| jdkr4t| j| j	ƒr't  
d¡ t| j| j| j| j	d� nt  
d¡ t  d¡ d S )	N©ÚargsrN   )r)   r>   r?   rS   z1cdef and csrc are the same file and should not be)r)   rA   rB   rC   z5a subcommand is required: exec-python or read-sourcesr   )ÚparserÚ
parse_argsr   r@   r)   r>   r?   r;   r   r"   ÚerrorrD   rA   ÚexitrT   r   r   r   ÚrunÉ   s   


ü
rZ   r<   )Ú__doc__Úargparser%   r   r   Úapir   r   r   r$   r*   r5   r;   r@   rD   rJ   ÚArgumentParserrV   Úadd_subparsersÚ
subparsersÚ
add_parserÚexec_python_parserÚadd_argumentÚFileTypeÚread_sources_parserrZ   r   r   r   r   Ú<module>   sz   '			þþýýþþþýýþ