a b+@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 4/tmp/pip-target-98j97qn4/lib/python/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) isinstanceunicodeencodesuperrwrite)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 Ksz|jjrtd|jjf||_||_|s4t| } t||} | ||_|j | | |_ | | | |_ |rz|rxt dnddtjddt|| g|j}tjdkr|d}tt|dddd @}|d d }tt|d ddd @}|d d }d||jj||f}td}|p4t|_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'z%d.%drzutf-8rl0xLr0z_cffi_%s_%s%s%sF)$_parserZ_uses_new_featurerffipreamblerflatten_locate_engine_class_vengineZpatch_extension_kwdsflagsmake_relative_tokwds TypeErrorjoinsys version_inforZ _cdefsourcesrhexbinasciicrc32lstriprstripZ _class_key_get_so_suffixes_caller_dir_pycachetmpdirospathsourcefilenamemodulefilename ext_package _has_source _has_module)rr.r/rA modulenamerFtagforce_generic_engineZsource_extensionr3 relative_tor5Zflattened_kwdsZ vengine_classkeyZk1Zk2rr r r __init__ sP         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._lockrGr _write_sourcerfiler r r write_sourceIs  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.rOrHrrGrP_compile_modulerr r r compile_moduleRs  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.rOrH_locate_modulerGrPrT _load_libraryrUr r r load_library\s zVerifier.load_librarycCsBtj|j}|ddd}|dr>ttdr>|dd}|S)N.rrZ_dgettotalrefcount)rBrCbasenamerEsplitendswithhasattrr8)rr]r r r get_module_namels  zVerifier.get_module_namecCsnt|jsD|jj|js&|Wdn1s:0Yt|j}|}tj ||fi|j Sr) rZ_hack_at_distutilsrGr.rOrPZmaybe_relative_pathrDra get_extensionr5)rZ sourcenamemodnamer r r rbws & zVerifier.get_extensioncCs|jjSr)r2Z_gen_python_modulerUr 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 )rBrCr7)rfndirnamer r rz-Verifier.make_relative_to..) rBrCrgcopyrZLIST_OF_FILE_NAMESrlisttupler6)rr5rLrMlstr rfr r4s   zVerifier.make_relative_tocCstj|jst|jrHzt|jdddg}Wnty>YdS0|j}nd}|j | |t }|durndS||_|j d|_ dS)N__doc__T)rBrCisfilerErF __import__ ImportError__path__r2 find_modulerar?Z collect_typesrH)rpkgrCfilenamer r r rWs   zVerifier._locate_modulecCs*||j_z|jW|j`n|j`0dSr)r2_fZwrite_source_to_frQr r r _write_source_tos zVerifier._write_source_tocCs|dur||nt}|||}tj|jrvt|jd}||k }Wdqz1sj0Ynd}|rt |jt|jd}| |Wdn1s0Yd|_ dS)NrTw) rvrgetvaluerBrCexistsrDopenread _ensure_dirrrG)rrRfZ source_datafpZ needs_writtenr r r rPs  . (zVerifier._write_sourcecCsntj|j}t||}zt||j}Wnt yFd}Yn0|sdt |jt ||jd|_ dS)NFT)rBrCrgrDrcompilerbsamefilerEOSErrorr}shutilmoverH)rrAZoutputfilenameZsamer r r rTs   zVerifier._compile_modulecCs0|js J|jdur"|j|jS|jSdSr)rHr3r2rYrUr r r rXs  zVerifier._load_library)NNNr&Fr'NN)N)N)r!r"r#rNrSrVrYrarbrdr4rWrvrPrTrXr 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_ENGINEr8builtin_module_names _cffi_backendrpZ_backendr&rZVGenericEnginerZVCPythonEngine)r.rKrrrr r r r1s        r1cCsHtrtStjd}|r|Stdjj}tj tj tj |dS)NZ CFFI_TMPDIRr) __pycache__) _TMPDIRrBenvirongetr8 _getframef_code co_filenamerCabspathr7rg)resultrtr r r r@s r@cCs|adS)z:Set the temporary directory to use instead of __pycache__.N)rrfr 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_build) r@rBlistdirrr?lower startswithr_unlinkrCr7isdirappend)rAZkeep_sofilelistrreZ clean_dirdirr r r cleanup_tmpdirs8       rcCs&t}|s"tjdkrdg}ndg}|S)Nwin32z.pydz.so)r r8platform)suffixesr r r r?%s  r?cCs*tj|}|r&tj|s&t|dSr)rBrCrgrmakedirs)rtrgr r r r}0s r})NF)r8rBr;rior&rrerrorrr9importlib.machineryrr rStringIOrBytesIOobjectr%rr1rr@rrr?r}r r r r s*(      @