U
    aÊ¹j'  ã                   @   sÌ   d dl Z d dlZd dlmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ dd	lmZ dd
lmZ d dlm  m	Z d dlmZ dejkZG dd„ dƒZG dd„ dƒZdd„ ZdS )é    N)Úcache_from_source)Úpath)Ú	build_extÚget_abi3_suffix)ÚDistribution)ÚCompileError)Ú	Extensioné   )Úenvironment)ÚDALS)Úget_config_varZ__pypy__c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTestBuildExtc                 C   s@   t ƒ }t|ƒ}d|jd< | d¡}tj |d¡}||ks<t‚dS )z…
        Setuptools needs to give back the same
        result as distutils, even if the fullname
        is not in ext_map.
        Ú zfoo/barZfooN)r   r   Úext_mapÚget_ext_filenameÚorigÚAssertionError)ÚselfÚdistÚcmdÚresÚwanted© r   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/setuptools/tests/test_build_ext.pyÚtest_get_ext_filename   s    

z"TestBuildExt.test_get_ext_filenamec                 C   s–   t tƒ ƒ tddgdd�}tt|gd�ƒ}t|ƒ}| ¡  d|jksHt‚| 	d¡}tƒ sl| 
tdƒ¡s’t‚n&tjdkr†| 
d¡s’t‚nd	|ks’t‚d
S )z}
        Filename needs to be loadable by several versions
        of Python 3 if 'is_abi3' is truthy on Extension()
        ú	spam.eggsúeggs.cT©Zpy_limited_api©Úext_modulesÚ
EXT_SUFFIXÚwin32zeggs.pydÚabi3N)Úprintr   r   r   Údictr   Úfinalize_optionsr   r   r   Úendswithr   ÚsysÚplatform)r   Ú	extensionr   r   r   r   r   r   Útest_abi3_filename$   s    


zTestBuildExt.test_abi3_filenamec                 C   sŒ   t ƒ }t|ƒ}tddgdd� |jd< }d|_ts>| d¡}nd}z:dtjd< | d	¡}d
|ksdt	‚| d¡}||kszt	‚W 5 tjd= X dS )zd
        SETUPTOOLS_EXT_SUFFIX variable always overrides
        default extension options.
        Zfor_abi3zs.cTr   Fzfor_abi3.test-suffixÚSETUPTOOLS_EXT_SUFFIXz.test-suffixÚnormalznormal.test-suffixN)
r   r   r   r   Z_links_to_dynamicÚIS_PYPYr   ÚosÚenvironr   )r   r   r   ÚextÚexpectr   r   r   r   Útest_ext_suffix_override9   s$    ü


z%TestBuildExt.test_ext_suffix_overridec                 C   sf   ddddiiidddiidœ}t dd	gƒ}t d
dgƒ}t ddgƒ}t |¡ td|||gddidœƒS )NZmypkgZsubpkgzext2.cr   Úext1zmain.c)Úsrczc-extensionsz
mypkg.ext1zc-extensions/ext1/main.czmypkg.subpkg.ext2zsrc/mypkg/subpkg/ext2.cÚext3zc-extension/ext3.cz%test%r4   )Úscript_namer   Úpackage_dir)r   r   Úbuildr   )r   Úfilesr3   Zext2r5   r   r   r   Údist_with_exampleY   s    
þ
ýzTestBuildExt.dist_with_examplec                 C   sê   |  dd¡ | dd¡ |  ¡ }| d¡}d|_| ¡  |j tj	d¡}dd„ | 
¡ D ƒ}||› d	�|› d
�|› d�gks~t‚| ¡ i ksŽt‚| d¡ d|_| ¡  dd„ | ¡  ¡ D ƒ}||› d	�d|› d
�d|› d�diksæt‚d S )Nr+   ú.mp3ú&setuptools.command.build_ext.use_stubsFr   ú/c                 S   s   g | ]}|  tjd ¡‘qS ©r=   ©Úreplacer.   Úsep)Ú.0Úxr   r   r   Ú
<listcomp>t   s     z1TestBuildExt.test_get_outputs.<locals>.<listcomp>ú	/ext3.mp3ú/mypkg/ext1.mp3ú/mypkg/subpkg/ext2.mp3Tc                 S   s*   i | ]"\}}|  tjd ¡|  tjd ¡“qS r>   r?   ©rB   ÚkÚvr   r   r   Ú
<dictcomp>€   s   ÿ z1TestBuildExt.test_get_outputs.<locals>.<dictcomp>úsrc/ext3.mp3úsrc/mypkg/ext1.mp3úsrc/mypkg/subpkg/ext2.mp3)ÚsetenvÚsetattrr:   Úget_command_objÚeditable_modeÚensure_finalizedÚ	build_libr@   r.   rA   Úget_outputsr   Úget_output_mappingÚreinitialize_commandÚitems)r   Ú
tmpdir_cwdÚmonkeypatchr   r   rT   ÚoutputsÚmappingr   r   r   Útest_get_outputsj   s8    
ý


þ   ýzTestBuildExt.test_get_outputsc           
      C   sL  |  dd¡ | dd¡ |  ¡ }| d¡}d|_| ¡  |jD ]}| |dd¡ q>|j t	j
d¡}dd	„ | ¡  ¡ D ƒ}d
d„ }|||› d�ƒ|dƒ|› d�d||› d�ƒ|dƒ|› d�d||› d�ƒ|dƒ|› d�diksàt‚|› d�|ksòt‚|› d�|k�st‚||› d�ƒ}	|	|k�s"t‚|	 |› d�¡�s8t‚|	 d¡�sHt‚d S )Nr+   r;   r<   Tr   Z_needs_stubr=   c                 S   s*   i | ]"\}}|  tjd ¡|  tjd ¡“qS r>   r?   rH   r   r   r   rK   —   s   ÿ zBTestBuildExt.test_get_output_mapping_with_stub.<locals>.<dictcomp>c                 S   s   t | ƒ tjd¡S )zDMake it possible to do comparisons and tests in a OS-independent wayr=   )Ú_compiled_file_namer@   r.   rA   )Úfiler   r   r   ÚCœ   s    z9TestBuildExt.test_get_output_mapping_with_stub.<locals>.Cz/ext3.pyzsrc/ext3.pyrE   rL   z/mypkg/ext1.pyzsrc/mypkg/ext1.pyrF   rM   z/mypkg/subpkg/ext2.pyzsrc/mypkg/subpkg/ext2.pyrG   rN   z/mypkg/__pycache__/ext1z.pyc)rO   rP   r:   rQ   rR   rS   Ú
extensionsrT   r@   r.   rA   rV   rX   r   Ú
startswithr&   )
r   rY   rZ   r   r   r0   rT   r\   r`   Zexample_stubr   r   r   Ú!test_get_output_mapping_with_stubŠ   sB    


þ      ú

z.TestBuildExt.test_get_output_mapping_with_stubN)	Ú__name__Ú
__module__Ú__qualname__r   r*   r2   r:   r]   rc   r   r   r   r   r      s     r   c                   @   s2   e Zd Zedœdd„Zdd„ Zdd„ Zdd	„ Zd
S )ÚTestBuildExtInplace©Úoptionalc                 K   sn   di i dœdœ}t  |¡ tddg|d�}tt|gd�ƒ}d|_t|ƒ}t|ƒjf d	d
dœ|—Ž | 	¡  |S )Nz#include missingheader.h
)ÚlibÚtmp)r   z.buildr   r   rh   r   úsetup.pyz
.build/libz
.build/tmp)rT   Ú
build_temp)
r   r8   r   r   r$   r6   r   ÚvarsÚupdaterS   )r   ri   Úoptsr9   r)   r   r   r   r   r   Úget_build_ext_cmdµ   s    þ
z%TestBuildExtInplace.get_build_ext_cmdc                 C   s"   |  ¡ }|j ¡ |j ¡  |j S )z½
        Historically, distutils "logged" by printing to sys.std*.
        Later versions adopted the logging framework. Grab
        messages regardless of how they were captured.
        )Z
readouterrÚoutÚ
splitlinesÚerrÚmessages)r   ÚcaplogÚcapsysZstdr   r   r   Úget_log_messagesÃ   s    z$TestBuildExtInplace.get_log_messagesc                 C   s8   | j ddd�}| ¡  tdd„ |  ||¡D ƒƒs4t‚dS )z‚
        If optional extensions fail to build, setuptools should show the error
        in the logs but not fail to build
        T©ri   Zinplacec                 s   s   | ]
}d V  qdS )z0build_ext: building extension "spam.eggs" failedNr   )rB   Úmsgr   r   r   Ú	<genexpr>Ó   s   ÿz4TestBuildExtInplace.test_optional.<locals>.<genexpr>N)rq   ÚrunÚanyrx   r   )r   rY   rv   rw   r   r   r   r   Útest_optionalÌ   s
    
þz!TestBuildExtInplace.test_optionalc              	   C   s0   | j ddd�}t t¡� | ¡  W 5 Q R X d S )NFTry   )rq   ÚpytestZraisesr   r|   )r   rY   r   r   r   r   Útest_non_optionalÙ   s    z%TestBuildExtInplace.test_non_optionalN)rd   re   rf   Úboolrq   rx   r~   r€   r   r   r   r   rg   ´   s   	rg   c                 C   sN   t dƒt dƒt dƒdœ}t |¡ tjdgdd�\}}|dksJtd	| ƒ‚d S )
NzÚ
            from setuptools import Extension, setup
            setup(
                name='foo',
                version='0.0.0',
                ext_modules=[Extension('foo', ['foo.c'])],
            )
            aÜ  
            #include "Python.h"

            #if PY_MAJOR_VERSION >= 3

            static struct PyModuleDef moduledef = {
                    PyModuleDef_HEAD_INIT,
                    "foo",
                    NULL,
                    0,
                    NULL,
                    NULL,
                    NULL,
                    NULL,
                    NULL
            };

            #define INITERROR return NULL

            PyMODINIT_FUNC PyInit_foo(void)

            #else

            #define INITERROR return

            void initfoo(void)

            #endif
            {
            #if PY_MAJOR_VERSION >= 3
                PyObject *module = PyModule_Create(&moduledef);
            #else
                PyObject *module = Py_InitModule("extension", NULL);
            #endif
                if (module == NULL)
                    INITERROR;
            #if PY_MAJOR_VERSION >= 3
                return module;
            #endif
            }
            zD
            [build]
            build_base = foo_build
            )rl   zfoo.cz	setup.cfgr8   )r   é   )r   Zdata_streamr   z
STDOUT:
%s
STDERR:
%s)r   r   r8   r
   Zrun_setup_pyr   )rY   r9   ÚcodeÚoutputr   r   r   Útest_build_ext_config_handlingà   s     ÿ
ÿ+ÿÊ=
þ
r…   )r.   r'   Zimportlib.utilr   r^   r   Zjaracor   Zsetuptools.command.build_extr   r   Zsetuptools.distr   Zsetuptools.errorsr   Zsetuptools.extensionr   r   r
   Útextwrapr   Zdistutils.command.build_extÚcommandr   Údistutils.sysconfigr   Úbuiltin_module_namesr-   r   rg   r…   r   r   r   r   Ú<module>   s"   
 ,