o
    LK&hs'                     @  s   d dl mZ 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v ZG dd dZG dd dZdd Z dS )    )annotationsN)cache_from_source)path)	build_extget_abi3_suffix)Distribution)CompileError)	Extension   )environment)DALS)get_config_var__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J dS )z
        Setuptools needs to give back the same
        result as distutils, even if the fullname
        is not in ext_map.
         zfoo/barfooN)r   r   ext_mapget_ext_filenameorig)selfdistcmdreswanted r   R/var/www/html/venv/lib/python3.10/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v s$J |d}t s7|	t
ds5J d
S tjdkrE|	dsCJ d
S d	|v sKJ 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py_limited_apiext_modules
EXT_SUFFIXwin32zeggs.pydabi3N)printr   r	   r   dictr   finalize_optionsr   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s2J |d}||ks=J W tjd= dS tjd= w )zd
        SETUPTOOLS_EXT_SUFFIX variable always overrides
        default extension options.
        for_abi3zs.cTr   Fzfor_abi3.test-suffixz.test-suffixSETUPTOOLS_EXT_SUFFIXnormalznormal.test-suffixN)	r   r   r	   r   _links_to_dynamicIS_PYPYr   osenviron)r   r   r   extexpectr   r   r   r   test_ext_suffix_override;   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 )Nmypkg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%r;   )script_namer"   package_dir)r	   r   buildr   )r   filesr:   ext2r<   r   r   r   dist_with_example[   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?J | i ksGJ |d d|_|  dd |  D }|| d	d| d
d| ddikssJ d S )Nr/   .mp3&setuptools.command.build_ext.use_stubsFr   /c                 S  s   g | ]	}| tjd qS rE   replacer3   sep).0xr   r   r   
<listcomp>v   s    z1TestBuildExt.test_get_outputs.<locals>.<listcomp>	/ext3.mp3/mypkg/ext1.mp3/mypkg/subpkg/ext2.mp3Tc                 S  *   i | ]\}}| tjd | tjd qS rF   rG   rJ   kvr   r   r   
<dictcomp>       z1TestBuildExt.test_get_outputs.<locals>.<dictcomp>src/ext3.mp3src/mypkg/ext1.mp3src/mypkg/subpkg/ext2.mp3)setenvsetattrrB   get_command_objeditable_modeensure_finalized	build_librH   r3   rI   get_outputsget_output_mappingreinitialize_commanditems)r   
tmpdir_cwdmonkeypatchr   r   r^   outputsmappingr   r   r   test_get_outputsl   s2   






zTestBuildExt.test_get_outputsc           
      C  sD  | 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spJ | d|vsyJ | d|vsJ || d}	|	|v sJ |	| dsJ |	dsJ d S )Nr/   rC   rD   Tr   _needs_stubrE   c                 S  rP   rF   rG   rQ   r   r   r   rT      rU   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 wayrE   )_compiled_file_namerH   r3   rI   )filer   r   r   C   s   z9TestBuildExt.test_get_output_mapping_with_stub.<locals>.Cz/ext3.pyzsrc/ext3.pyrM   rV   z/mypkg/ext1.pyzsrc/mypkg/ext1.pyrN   rW   z/mypkg/subpkg/ext2.pyzsrc/mypkg/subpkg/ext2.pyrO   rX   z/mypkg/__pycache__/ext1z.pyc)rY   rZ   rB   r[   r\   r]   
extensionsr^   rH   r3   rI   r`   rb   
startswithr)   )
r   rc   rd   r   r   r5   r^   rf   rk   example_stubr   r   r   !test_get_output_mapping_with_stub   s6   







z.TestBuildExt.test_get_output_mapping_with_stubN)	__name__
__module____qualname__r   r-   r7   rB   rg   ro   r   r   r   r   r      s      r   c                   @  s.   e Zd ZdddZdd Zd	d
 Zdd ZdS )TestBuildExtInplaceoptionalboolreturnr   c                 K  sn   di i dd}t | tddg|d}tt|gd}d|_t|}t|jdd	d
d| |	  |S )Nz#include missingheader.h
)libtmp)r   z.buildr   r   )rt   r!   setup.pyz
.build/libz
.build/tmp)r^   
build_tempr   )
r   r?   r	   r   r'   r=   r   varsupdater]   )r   rt   optsr@   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.
        )
readouterrout
splitlineserrmessages)r   caplogcapsysstdr   r   r   get_log_messages   s   z$TestBuildExtInplace.get_log_messagesc                 C  s8   | j ddd}|  tdd | ||D sJ dS )z
        If optional extensions fail to build, setuptools should show the error
        in the logs but not fail to build
        Trt   inplacec                 s  s    | ]}d V  qdS )z0build_ext: building extension "spam.eggs" failedNr   )rJ   msgr   r   r   	<genexpr>   s
    
z4TestBuildExtInplace.test_optional.<locals>.<genexpr>N)r~   runanyr   )r   rc   r   r   r   r   r   r   test_optional   s
   
z!TestBuildExtInplace.test_optionalc                 C  sF   | j ddd}tt |  W d    d S 1 sw   Y  d S )NFTr   )r~   pytestraisesr   r   )r   rc   r   r   r   r   test_non_optional   s   
"z%TestBuildExtInplace.test_non_optionalN)rt   ru   rv   r   )rp   rq   rr   r~   r   r   r   r   r   r   r   rs      s
    
	rs   c                 C  sZ   t dt dt dd}t| tjdgdd\}\}}|dks+J d	| 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
            )ry   zfoo.cz	setup.cfgr?   )r      )r   data_streamr   z	
STDOUT:
z	
STDERR:
)r   r   r?   r   run_setup_py)rc   r@   codestdoutstderrr   r   r   test_build_ext_config_handling   s    
+
= r   )!
__future__r   r3   r*   importlib.utilr   ri   r   jaracor   setuptools.command.build_extr   r   setuptools.distr   setuptools.errorsr   setuptools.extensionr	   r   r   textwrapr   distutils.command.build_extcommandr   distutils.sysconfigr   builtin_module_namesr2   r   rs   r   r   r   r   r   <module>   s&    
 ,