a (`+@sddlZddlZddlZddlZddlZddlmZddlmZddlm Z ej dkrhddl Z ddZ nddlZd dZ ej d krejZnGd d d ejZGd ddeZdZddZdaddZddZdddZddZddZdS)N)__version_verifier_modules__) ffiplatform)VerificationError)rcCstjjddSN) importlib machineryEXTENSION_SUFFIXESr r 3/usr/lib64/python3.9/site-packages/cffi/verifier.py_extension_suffixes sr cCsddtDS)NcSs g|]\}}}|tjkr|qSr )imp C_EXTENSION).0suffix_typer r r s z'_extension_suffixes..)rZ get_suffixesr r r r r srcseZdZfddZZS)NativeIOcs(t|tr|d}tt||dS)Nascii) isinstanceZunicodeencodesuperrwrite)selfs __class__r r rs  zNativeIO.write)__name__ __module__ __qualname__r __classcell__r r rr rsrc@szeZdZdddZd ddZd d Zd d Zd dZddZddZ ddZ ddZ ddZ d!ddZ ddZddZdS)"VerifierNF.cc Ksv|jjrtd|jjf||_||_|s4t| } t||} | ||_|j | | |_ | | | |_ |rz|rxt dndtjddt|| g|j}tjdkr|d}tt|dddd @}|d d }tt|d ddd @}|d d }d||jj||f}td}|p0t|_tj|j|||_ tj|j|||_!||_"d|_#d|_$dS)NzKfeature not supported with ffi.verify(), but only with ffi.set_source(): %sz)can't specify both 'modulename' and 'tag'rrzutf-8rl0xLr0z_cffi_%s_%s%s%sF)%Z_parserZ_uses_new_featurerffipreamblerZflatten_locate_engine_class_vengineZpatch_extension_kwdsflagsmake_relative_tokwds TypeErrorjoinsysversionrZ _cdefsources version_inforhexbinasciicrc32lstriprstripZ _class_key_get_so_suffixes_caller_dir_pycachetmpdirospathsourcefilenamemodulefilename ext_package _has_source _has_module)rr,r-r?Z modulenamerDtagforce_generic_engineZsource_extensionr0 relative_tor2Zflattened_kwdsZ vengine_classkeyZk1Zk2rr r r __init__ sN         zVerifier.__init__cCsL|jj0|jr |dur td||Wdn1s>0YdS)zkWrite the C source code. It is produced in 'self.sourcefilename', which can be tweaked beforehand.Nzsource code already written)r,_lockrEr _write_sourcerfiler r r write_sourceHs  zVerifier.write_sourcecCsP|jj4|jrtd|js&||Wdn1sB0YdS)zWrite the C source code (if not done already) and compile it. This produces a dynamic link library in 'self.modulefilename'.zmodule already compiledN)r,rLrFrrErM_compile_modulerr r r compile_moduleQs  zVerifier.compile_modulecCs^|jjB|js4||js4|js,|||WdS1sP0YdS)a"Get a C module from this Verifier instance. Returns an instance of a FFILibrary class that behaves like the objects returned by ffi.dlopen(), but that delegates all operations to the C module. If necessary, the C code is written and compiled first. N)r,rLrF_locate_modulerErMrQ _load_libraryrRr r r load_library[s zVerifier.load_librarycCsBtj|j}|ddd}|dr>ttdr>|dd}|S)N.rrZ_dZgettotalrefcount)r@rAbasenamerCsplitendswithhasattrr5)rrYr r r get_module_nameks  zVerifier.get_module_namecCsnt|jsD|jj|js&|Wdn1s:0Yt|j}|}tj ||fi|j Sr) rZ_hack_at_distutilsrEr,rLrMZmaybe_relative_pathrBr] get_extensionr2)rZ sourcenamemodnamer r r r^vs & zVerifier.get_extensioncCs|jjSr)r/Z_gen_python_modulerRr r r generates_python_modulesz Verifier.generates_python_modulecsz|rvtj|rvtj||}tjD]J}||vr*||}t|ttfsZt d|ffdd|D}|||<q*|S)Nz&keyword '%s' should be a list or tuplecsg|]}tj|qSr )r@rAr4)rfndirnamer r rz-Verifier.make_relative_to..) r@rArccopyrZLIST_OF_FILE_NAMESrlisttupler3)rr2rIrJZlstr rbr r1s   zVerifier.make_relative_tocCstj|jst|jrHzt|jdddg}Wnty>YdS0|j}nd}|j | |t }|durndS||_|j d|_ dS)N__doc__T)r@rAisfilerCrD __import__ ImportError__path__r/ find_moduler]r=Z collect_typesrF)rpkgrAfilenamer r r rTs   zVerifier._locate_modulecCs*||j_z|jW|j`n|j`0dSr)r/Z_fZwrite_source_to_frNr r r _write_source_tos zVerifier._write_source_tocCs|dur||nt}|||}tj|jrvt|jd}||k }Wdqz1sj0Ynd}|rt |jt|jd}| |Wdn1s0Yd|_ dS)NrTw) rprgetvaluer@rAexistsrBopenread _ensure_dirrrE)rrOfZ source_datafpZ needs_writtenr r r rMs  . (zVerifier._write_sourcecCsntj|j}t||}zt||j}Wnt yFd}Yn0|sdt |jt ||jd|_ dS)NFT)r@rArcrBrcompiler^samefilerCOSErrorrwshutilmoverF)rr?ZoutputfilenameZsamer r r rQs   zVerifier._compile_modulecCs0|js J|jdur"|j|jS|jSdSr)rFr0r/rVrRr r r rUs  zVerifier._load_library)NNNr%Fr&NN)N)N)r r!r"rKrPrSrVr]r^r`r1rTrprMrQrUr r r r r$s  (      r$FcCsztrd}|sNdtjvrd}n2z ddl}Wnty>d}Yn0|j|urNd}|rdddlm}|jSddlm }|j SdS)NTZ__pypy__r?r) vengine_gen) vengine_cpy) _FORCE_GENERIC_ENGINEr5builtin_module_names _cffi_backendrkZ_backendr%rZVGenericEnginerZVCPythonEngine)r,rHrrrr r r r.s        r.cCsHtrtStjd}|r|Stdjj}tj tj tj |dS)NZ CFFI_TMPDIRr( __pycache__) _TMPDIRr@environgetr5 _getframef_code co_filenamerAabspathr4rc)resultror r r r>s r>cCs|adS)z:Set the temporary directory to use instead of __pycache__.N)rrbr r r set_tmpdirsrc Cs|pt}zt|}Wnty,YdS0|r8d}ntd}|D]Z}|drJ||sx|drJzttj ||WqJtyYqJ0qJtj |dg}|D]^}zDt|D]4}tj ||}tj |r| |qt|qWqtyYq0qdS)z}Clean up the temporary directory by removing all files in it called `_cffi_*.{c,so}` as well as the `build` subdirectory.Nr&rZ_cffi_Zbuild) r>r@listdirr|r=lower startswithr[unlinkrAr4isdirappend)r?Zkeep_soZfilelistrraZ clean_dirdirr r r cleanup_tmpdirs8       rcCs&t}|s"tjdkrdg}ndg}|S)Nwin32z.pydz.so)r r5platform)suffixesr r r r=$s  r=cCs*tj|}|r&tj|s&t|dSr)r@rArcrmakedirs)rorcr r r rw/s rw)NF)r5r@r9r}ior%rrerrorrr7Zimportlib.machineryrr rStringIOrBytesIOobjectr$rr.rr>rrr=rwr r r r s*(      ?