ÿØÿà JFIF    ÿÛ „ ( %!1!%*+...983,7(-.- __pycache__/verifier.cpython-36.opt-1.pyc000064400000021506152344566040014171 0ustar003 MZ+@sddlZddlZddlZddlZddlZddlmZddlmZddlm Z ej dkrhddl Z ddZ nddlZd dZ ej dkrejZnGd d d ejZGd d d eZdZddZdaddZddZdddZddZddZdS)N)__version_verifier_modules__) ffiplatform)VerificationErrorcCstjjddS)N) importlib machineryEXTENSION_SUFFIXESr r /usr/lib64/python3.6/verifier.py_extension_suffixes sr cCsddtjDS)NcSs g|]\}}}|tjkr|qSr )impZ C_EXTENSION).0suffix_typer r r sz'_extension_suffixes..)r Z get_suffixesr r r r r scseZdZfddZZS)NativeIOcs(t|tr|jd}tt|j|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||_||_|s4tj| } t||} | ||_|jj | | |_ |j | | |_ |rz|rxt dndjtjddt|| g|j}tjdkr|jd}ttj|dddd@}|jd jd }ttj|d ddd@}|jd jd }d ||jj||f}td}|p0t|_tjj|j|||_ tjj|j|||_!||_"d|_#d|_$dS)NzKfeature not supported with ffi.verify(), but only with ffi.set_source(): %sz)can't specify both 'modulename' and 'tag'rzutf-8rlZ0xLr0z_cffi_%s_%s%s%sF)r)%Z_parserZ_uses_new_featurerffipreamblerZflatten_locate_engine_class_vengineZpatch_extension_kwdsflagsmake_relative_tokwds TypeErrorjoinsysversionrZ _cdefsources version_inforhexbinasciiZcrc32lstriprstripZ _class_key_get_so_suffixes_caller_dir_pycachetmpdirospathsourcefilenamemodulefilename ext_package _has_source _has_module)rr'r(r9Z modulenamer>tagforce_generic_engineZsource_extensionr+ relative_tor-Zflattened_kwdsZ vengine_classkeyZk1Zk2rr r r __init__ sD           zVerifier.__init__c Cs8|jj&|jr |dkr td|j|WdQRXdS)zkWrite the C source code. It is produced in 'self.sourcefilename', which can be tweaked beforehand.Nzsource code already written)r'_lockr?r _write_source)rfiler r r write_sourceHs  zVerifier.write_sourcec Cs<|jj*|jrtd|js&|j|jWdQRXdS)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'rFr@rr?rG_compile_module)rr r r compile_moduleQs  zVerifier.compile_modulec CsF|jj4|js4|j|js4|js,|j|j|jSQRXdS)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'rFr@_locate_moduler?rGrJ _load_library)rr r r load_library[s zVerifier.load_librarycCsBtjj|j}|jddd}|jdr>ttdr>|dd}|S)N.rrZ_dgettotalrefcountr$)r:r;basenamer=splitendswithhasattrr0)rrRr r r get_module_nameks  zVerifier.get_module_namec CsVtj|js0|jj|js&|jWdQRXtj|j}|j}tj ||f|j S)N) rZ_hack_at_distutilsr?r'rFrGZmaybe_relative_pathr<rV get_extensionr-)rZ sourcenamemodnamer r r rWvs  zVerifier.get_extensioncCs|jjS)N)r*Z_gen_python_module)rr r r generates_python_modulesz Verifier.generates_python_modulecs~|rztjj|rztjj||j}xTtjD]J}||kr,||}t|ttfs\t d|ffdd|D}|||<q,W|S)Nz&keyword '%s' should be a list or tuplecsg|]}tjj|qSr )r:r;r/)rfn)dirnamer r rsz-Verifier.make_relative_to..) r:r;r[copyrZLIST_OF_FILE_NAMESrlisttupler.)rr-rCrDZlstr )r[r r,s    zVerifier.make_relative_toc Cstjj|jst|jrHyt|jdddg}Wntk r>dSX|j}nd}|jj |j |t }|dkrndS||_|jj d|_ dS)N__doc__T)r:r;isfiler=r> __import__ ImportError__path__r* find_modulerVr7Z collect_typesr@)rZpkgr;filenamer r r rLs zVerifier._locate_modulec Cs$||j_z|jjWd|j`XdS)N)r*Z_fZwrite_source_to_f)rrHr r r _write_source_toszVerifier._write_source_tocCs|dk r|j|nt}|j||j}tjj|jrbt|jd}|j|k }WdQRXnd}|rt |jt|jd}|j |WdQRXd|_ dS)NrTw) rfrgetvaluer:r;existsr<openread _ensure_dirrr?)rrHfZ source_datafpZ needs_writtenr r r rGs   zVerifier._write_sourcec Csptjj|j}tj||j}ytj||j}Wnt k rHd}YnX|sft |jt j ||jd|_ dS)NFT)r:r;r[r<rcompilerWsamefiler=OSErrorrmshutilZmover@)rr9ZoutputfilenameZsamer r r rJs  zVerifier._compile_modulecCs&|jdk r|jj|jS|jjSdS)N)r+r*rN)rr r r rMs zVerifier._load_library)NNNr!Fr"NN)N)N)rrrrErIrKrNrVrWrYr,rLrfrGrJrMr r r r r s &      r Fc Cs|trd}|sPdtjkrd}n4y ddl}Wntk r@d}YnX|j|k rPd}|rfddlm}|jSddlm }|j SdS)NTZ__pypy__r?r) vengine_gen) vengine_cpy) _FORCE_GENERIC_ENGINEr0builtin_module_names _cffi_backendrbZ_backendr!ruZVGenericEnginervZVCPythonEngine)r'rBryrurvr r r r)s       r)cCsHtrtStjjd}|r|Stjdjj}tjj tjj tjj |dS)NZ CFFI_TMPDIRr$ __pycache__) _TMPDIRr:environgetr0 _getframef_code co_filenamer;abspathr/r[)resultrer r r r8s r8cCs|adS)z:Set the temporary directory to use instead of __pycache__.N)r{)r[r r r set_tmpdirsrcCs0|pt}ytj|}Wntk r,dSX|r8d}ntdj}xd|D]\}|jjdrL|jj|sz|jjdrLytjtj j ||WqLtk rYqLXqLWtj j |dg}xn|D]f}yJxDtj|D]6}tj j ||}tj j |r|j |qtj|qWWqtk r&YqXqWdS)z}Clean up the temporary directory by removing all files in it called `_cffi_*.{c,so}` as well as the `build` subdirectory.Nz.crZ_cffi_Zbuild) r8r:listdirrrr7lower startswithrTunlinkr;r/isdirappend)r9Zkeep_soZfilelistrrZZ clean_dirdirr r r cleanup_tmpdirs2     rcCs&t}|s"tjdkrdg}ndg}|S)NZwin32z.pydz.so)r r0platform)suffixesr r r r7$s  r7cCs,tjj|}|r(tjj| r(tj|dS)N)r:r;r[rmakedirs)rer[r r r rm/s rm)rr)r)NF)r0r:r4rsior!rrerrorrr2Zimportlib.machineryrr r StringIOrBytesIOobjectr rwr)r{r8rrr7rmr r r r s*(      ?   __pycache__/cparser.cpython-36.opt-1.pyc000064400000047634152344566040014027 0ustar003 MZR@sddlmZddlmZmZddlmZmZyddlmZ Wne k rXddl Z YnXddl Z ddl Z ddl Z y(e jd#krddlZnddlZejZWne k rdZYnXdZe jd e je jBZe jd e je jBZe jd Ze jd Ze jd Ze jdZdae jde jZe jdZ e jdZ!e jdZ"e jdZ#e jdZ$e jdZ%e jdZ&ddZ'ddZ(ddZ)ddZ*dd Z+Gd!d"d"e,Z-dS)$)model) COMMON_TYPESresolve_common_type)FFIError CDefError) _pycparserNzz/\*.*?\*/|//([^\n\\]|\\.)*?$z?^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)\b((?:[^\n\\]|\\.)*?)$z=\s*\.\.\.\s*[,}]|\.\.\.\s*\}z__dotdotdot\d+__$z\[\s*\.\.\.\s*\]z\w+|\Sz-?0?x?[0-9a-f]+[lu]*$z\b(__stdcall|WINAPI)\bz[(]\s*(__stdcall|WINAPI)\bz \b__cdecl\bz9\bextern\s*"(Python|Python\s*\+\s*C|C\s*\+\s*Python)"\s*.z'[*]\s*((const|volatile|restrict)\b\s*)+z3(\b(int|long|short|signed|unsigned|char)\s*)+\.\.\.z\b(double|float)\s*\.\.\.cCstdkrtjatS)N) _parser_cache pycparserZCParserr r /usr/lib64/python3.6/cparser.py _get_parser*srcCs g}xtj|}|sP|j|d|j|jdd}|j|j|j}|jd|rn|jd|d7}d}|}x`|t|kr||}|dkr|d7}n.|dkr|dkrP|d8}n|dkr|dkrP|d7}qxW||||||d}qW|j|dj|S)N()*rrz,;=) _r_star_const_spacesearchappendstartgroupend startswithlenjoin)csourcepartsmatchclosingendposlevelicr r r _workaround_for_old_pycparser0s<          r$cCs0g}xtj|}|sP|jd}|j|d|jd|jdkrT|jdn |jd||dkr|jd|}|dkrtd|jd|d|dkrtd |j||d|||dd}nB|jd |}|dkrtd |j|||d||dd}|jd qW|j|d j |S)NrCz(void __cffi_extern_python_plus_c_start; z!void __cffi_extern_python_start; {}rz!'extern "Python" {': no '}' foundz7cannot use { } inside a block 'extern "Python" { ... }';z'extern "Python": no ';' foundz void __cffi_extern_python_stop;r) _r_extern_pythonrrrrrfindrNotImplementedErrorr)rrrr rZ semicolonr r r _preprocess_extern_pythonZs4         r,c Cs\tjd|}i}x6tj|D](}|j\}}|jddj}|||<qWtjd|}tjdkrft |}t jd|}t jd|}t jd|}t |}tjd|}ttj|}xtt|D]v\}}|j}||dkr|jd ||j}d |d||||d df}qd |d||||d df}qWtjd |}tjd|}|jd d|fS)N z\ rz2.14z volatile volatile const(z volatile volatile const z[__dotdotdotarray__]=z...z%s,__dotdotdot%d__ %sr z%s __dotdotdot%d__ %sz __dotdotdotint__ z __dotdotdotfloat__ z __dotdotdot__ ) _r_commentsub _r_definefinditergroupsreplacestripr __version__r$ _r_stdcall2 _r_stdcall1_r_cdeclr,_r_partial_arraylist_r_partial_enum enumeratereversedrr*r_r_int_dotdotdot_r_float_dotdotdot) rmacrosrZ macronameZ macrovalueZmatchesZnumberpZp2r r r _preprocesss4           rCcCstt}|jd|jd|jd|jd|jdt}d}d}d}xtj|D]}||kr|dkr|r|j||j|d}nj|dkrd }d}nX|dkr|d 7}nF|dkr|d 8}n4|dkr|r|dkr|j||j|n |j||}qXW|S) Nr(,rrtypedefFrrTr)setradd_r_wordsfindalldiscard)rZlook_for_wordsZ words_usedZ is_typedefZparenZ previous_wordZwordr r r _common_type_namess>             rKc@seZdZddZddZddZddZd8d d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZd9ddZdd Zd:d"d#Zd;d$d%Zdd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd!S)?ParsercCs:i|_t|_d|_tj|_i|_i|_g|_ d|_ dS)Nr) _declarationsrF_included_declarations_anonymous_counterweakrefWeakKeyDictionary_structnode2type_options_int_constants _recomplete_uses_new_feature)selfr r r __init__s zParser.__init__c Cs4t|\}}t|}g}x"ztypedef int %s;z@typedef int __dotdotdotint__, __dotdotdotfloat__, __dotdotdot__;z# 1 "%s" )rCrKsortedrMrrrJCDEF_SOURCE_STRINGrlockacquirerparser Zc_parserZ ParseErrorconvert_pycparser_errorrelease) rWrrAZctn typenamesname csourcelinestypenameZ fullcsourceaster r r _parses4          "  z Parser._parsecCsfd}t|}tjdtf|}|rbt|jdd}|j}d|koPt|knrb||d}|S)Nz %s:(\d+):r )strrerr\intr splitlinesr)rWrgrlinemsgrZlinenumrdr r r _convert_pycparser_errors zParser._convert_pycparser_errorcCs@|j||}t|}|r*d|j|f}n d|f}t|dS)Nzcannot parse "%s" %szparse error %s)rprjr5r)rWrgrrnror r r r`+s   zParser.convert_pycparser_errorFc Cs0|j}z|||d|_|j|Wd||_XdS)N)overridepacked dllexport)rS_internal_parse)rWrrqrrrsZ prev_optionsr r r r_5s z Parser.parsec Cs|j|\}}}|j|t|j}x|D]}|jdkr*Pq*Wd}yBd|_x4|D]*}|}t|tjj rx|j |qTt|tjj r^|jst d|d}t|j j tjjr|j j jdjdr|j|}nxt|j tjjr.t|j j tjjr.t|j j j tjjr.|j j j jdjdr.|j|}n|j|j |jdd\}}|jd |j||d qT|jjd krnqTt d |jj|qTWWnt k r} z&t| jdkr| j|f| _WYdd} ~ XnNtk r} z0|j| |} | r| jdd | f| _WYdd} ~ XnXdS)N __dotdotdot____cffi_extern_python_stopz!typedef does not declare any namerrZ __dotdotdotT)rcpartial_length_okztypedef )qualsZPragmazBunexpected <%s>: this construct is valid C but not valid in cdef()z *** Err: %sry)rh_process_macrositerextrc_inside_extern_python isinstancer c_astZDecl _parse_declZTypedefrtypeIdentifierTypenamesr_get_unknown_typePtrDeclTypeDecl_get_unknown_ptr_type_get_type_and_quals_declare __class____name__rargsrrp) rWrrfrAiteratordeclZ current_declrxZrealtypergror r r rt?sV          zParser._internal_parsecCs8||jkr*|j||krdStd|f||j|<dS)Nz%multiple declarations of constant: %s)rTr)rWkeyvalr r r _add_constantsus   zParser._add_constantscCs|jjd}|jd}|r(|dd}|jdrV|dkrV|jd rVd|dd}t|d}|rj| }|j|||jd||dS) NZul-r00xZ0orzmacro )lowerrstriprrlrr)rWrcZint_strnegZpyvaluer r r _add_integer_constant}s     zParser._add_integer_constantcCshxb|jD]V\}}|j}tj|r2|j||q |dkrL|jd||q td||||fq WdS)Nz...zmacro zonly supports one of the following syntax: #define %s ... (literally dot-dot-dot) #define %s NUMBER (with NUMBER an integer constant, decimal/hex/octal) got: #define %s %s)itemsr5_r_int_literalrrrr)rWrArvaluer r r rzs zParser._process_macroscCsX|j||}|jjdrd}n$|jdkr.d}n|jdkr>d}nd}|j||j|dS)Nrszdllexport_python Z__cffi_extern_python_startzextern_python Z!__cffi_extern_python_plus_c_startzextern_python_plus_c z function )_get_type_pointerrSgetr}rrc)rWtprxrtagr r r _declare_functions    zParser._declare_functioncCs|j}t|tjjr:|j||jd\}}|j|||nt|tjjrV|j d|nHt|tjj rr|j d|n,t|tjj r|j d|n|jst d||jr|j|dd\}}|j r|j|||n$|jot|dot|jd otj|jjr|j|j|jjn|jrxt|jtjjrx|jjd krxt|jjd rxtj|jjjrx|j|jd |jjjn||tjkr|jjd r|j|_nX|jd krt d |tj@r|j r|jd|j||dn|jd|j||ddS)N)rcstructunionenumz'construct does not declare any variableT)rwinitrrZ__cffi_extern_python_rvz|sz/Parser._parse_function_type..rxr rrZ __stdcallryryry)r;getattrrr=rrrr~rr rrrrpoprrrrxrtuple) rWrfuncnamerr"argZellipsisrresultrxabir )rWr rgs6       zParser._parse_function_typecCs6t|tjrtj|j|St|tjr.|jS|SdS)N)r~rrritemrr)rWrrxr r r rs   zParser._as_func_argc Cs,y |j|Stk rYnX|}|j}|dkrb|dk rDd|}n|jd7_d|j}d}n"|}d||f}|jj|d\}} |dkr|dkrtj|ddd}nP|dkrtj|ddd}n6|dkr|dkrt d |j ||j }nt d |f|dk r2|j ||n"|dkr2|j dk r2td ||jsD|j||jrjd |jkrj|j d |j|||j|<|dkr|S|jdkr|S|jdk rt d|g} g} g} g} x|jD]}t|jtjjrdj|jjdkr|j||q|jdkrd}n |j|j}d|_|j|jdd\}}|jrH|j||t|tjrj|jrj|j||| j |jpxd| j || j || j |qWt!| |_t!| |_"t!| |_#t!| |_$| dgt%| krt|tjr|jrtd|f|j&jd|_'|j(r(d|_(|j)j ||S)Nz$%srz$%dz%s %srrrruz!Enums cannot be declared with ...z kind = %rz^enum %s: the '{}' declaration should appear on the first time the enum is mentioned, not laterrz anonymous %sz"duplicate declaration of struct %srFT)rwz#%s: using both bitfields and '...;'rrr)NNryry)*rRKeyErrorrcrOrMrrZ StructTypeZ UnionTyper_build_enum_typevaluesAssertionErrorrr+rZforce_the_namedeclsfldnamesr~rr rrrr _make_partialbitsizer_partial_lengthrpartialrrfldtypes fldbitsizefldqualsrrSrrZ completedrU)rWkindrrcrZ force_name explicit_namerr_rrrrrrZfqualr r r rs                             z"Parser._get_struct_union_enum_typecCsBt|tjstd|f|j r8| r8td|fd|_dS)Nz%s cannot be partialz%s is partial but has no C nameT)r~rZ StructOrUnionrZ has_c_namer+r)rWrrr r r rs  zParser._make_partialcCst|tjjr|j}|jdrF|jds2|jdrsN               *27(__pycache__/lock.cpython-36.pyc000064400000000553152344566040012346 0ustar003 MZ @s|ddlZejdkrFyddlmZWqxek rBddlmZYqxXn2yddlmZWn ek rvddlmZYnXdS)N) allocate_lock)r)sys version_infoZthreadr ImportErrorZ dummy_thread_threadZ _dummy_threadrr/usr/lib64/python3.6/lock.pys __pycache__/lock.cpython-36.opt-1.pyc000064400000000553152344566040013305 0ustar003 MZ @s|ddlZejdkrFyddlmZWqxek rBddlmZYqxXn2yddlmZWn ek rvddlmZYnXdS)N) allocate_lock)r)sys version_infoZthreadr ImportErrorZ dummy_thread_threadZ _dummy_threadrr/usr/lib64/python3.6/lock.pys __pycache__/cffi_opcode.cpython-36.pyc000064400000011163152344566040013655 0ustar003 MZ\4@s<ddlmZGdddeZddZdZdZdZdZd Z d Z d Z d Z d Z dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#dZ$d Z%d Z&d Z'd!Z(d Z)d"Z*d Z+d#Z,dZ-d$Z.dZ/d%Z0dZ1d&Z2dZ3d'Z4dZ5d(Z6dZ7d)Z8dZ9d*Z:dZ;d+ZdZ?d-Z@dZAd.ZBdZCd/ZDd0ZEd1ZFd2ZGd3ZHd4ZId5ZJd6ZKd7ZLd8ZMd9ZNdDZOdEZPdFZQdGZReee!e#e%eee e"e$e&e'e(e)eJeKee*eLeMe+e,e-e.e/e0e1e2e3e4e5e6e7e8e9e:e;ee?e@eAeBeCeDeEeFeGeHeId:3ZSdZTdZUdZVdZWd"ZXeYd;d<dHDZZiZ[xDe\e]j^D]2\Z_Z`e_jadBrebe`ecre_ddCe[e`<qWdCS)I)VerificationErrorc@s,eZdZddZddZddZddZd S) CffiOpcCs||_||_dS)N)oparg)selfrrr#/usr/lib64/python3.6/cffi_opcode.py__init__szCffiOp.__init__cCs>|jdkr&t|jtstd|jfSt|j}d||jfS)Nz(_cffi_opcode_t)(%s)z_CFFI_OP(_CFFI_OP_%s, %s))r isinstancerstrAssertionError CLASS_NAME)r classnamerrr as_c_exprs    zCffiOp.as_c_exprcCsn|jdkr>|jjr>t|j}|dkr6td|jft|St|jtrZtd|jft|jd>|jBS)Nz"cannot emit %r: limited to 2**31-1zcannot emit to Python: %rl) rrisdigitint OverflowErrorformat_four_bytesr r r)rvaluerrras_python_bytess   zCffiOp.as_python_bytescCstj|j|j}d||jfS)Nz(%s %s))r getrr)rrrrr__str__szCffiOp.__str__N)__name__ __module__ __qualname__r rrrrrrrrs rcCs,d|d?d@|d?d@|d?d@|d@fS)Nz\x%02X\x%02X\x%02X\x%02Xrr)Znumrrrrs    r r!#%')rr r r "$&(*+,-./01234)3charZshortrZlongz long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long longfloatZdoublez long doublezfloat _Complexzdouble _ComplexZ_BoolZwchar_tZchar16_tZchar32_tZint8_tZuint8_tZint16_tZuint16_tZint32_tZuint32_tZint64_tZuint64_tZintptr_tZ uintptr_tZ ptrdiff_tZsize_tZssize_tZ int_least8_tZ uint_least8_tZ int_least16_tZuint_least16_tZ int_least32_tZuint_least32_tZ int_least64_tZuint_least64_tZ int_fast8_tZ uint_fast8_tZ int_fast16_tZ uint_fast16_tZ int_fast32_tZ uint_fast32_tZ int_fast64_tZ uint_fast64_tZintmax_tZ uintmax_tcCsg|]}d|t|fqS)Z_CFFI_)globals).0Z_keyrrr srTF_UNIONF_CHECK_FIELDSF_PACKED F_EXTERNALF_OPAQUEZOP_NrZ)rUrVrWrXrY)derrorrobjectrrZ OP_PRIMITIVEZ OP_POINTERZOP_ARRAYZ OP_OPEN_ARRAYZOP_STRUCT_UNIONZOP_ENUMZ OP_FUNCTIONZOP_FUNCTION_ENDZOP_NOOPZ OP_BITFIELDZ OP_TYPENAMEZOP_CPYTHON_BLTN_VZOP_CPYTHON_BLTN_NZOP_CPYTHON_BLTN_OZ OP_CONSTANTZOP_CONSTANT_INTZ OP_GLOBAL_VARZOP_DLOPEN_FUNCZOP_DLOPEN_CONSTZOP_GLOBAL_VAR_FZOP_EXTERN_PYTHONZ PRIM_VOIDZ PRIM_BOOLZ PRIM_CHARZ PRIM_SCHARZ PRIM_UCHARZ PRIM_SHORTZ PRIM_USHORTZPRIM_INTZ PRIM_UINTZ PRIM_LONGZ PRIM_ULONGZ PRIM_LONGLONGZPRIM_ULONGLONGZ PRIM_FLOATZ PRIM_DOUBLEZPRIM_LONGDOUBLEZ PRIM_WCHARZ PRIM_INT8Z PRIM_UINT8Z PRIM_INT16Z PRIM_UINT16Z PRIM_INT32Z PRIM_UINT32Z PRIM_INT64Z PRIM_UINT64Z PRIM_INTPTRZ PRIM_UINTPTRZ PRIM_PTRDIFFZ PRIM_SIZEZ PRIM_SSIZEZPRIM_INT_LEAST8ZPRIM_UINT_LEAST8ZPRIM_INT_LEAST16ZPRIM_UINT_LEAST16ZPRIM_INT_LEAST32ZPRIM_UINT_LEAST32ZPRIM_INT_LEAST64ZPRIM_UINT_LEAST64ZPRIM_INT_FAST8ZPRIM_UINT_FAST8ZPRIM_INT_FAST16ZPRIM_UINT_FAST16ZPRIM_INT_FAST32ZPRIM_UINT_FAST32ZPRIM_INT_FAST64ZPRIM_UINT_FAST64Z PRIM_INTMAXZ PRIM_UINTMAXZPRIM_FLOATCOMPLEXZPRIM_DOUBLECOMPLEXZ PRIM_CHAR16Z PRIM_CHAR32Z _NUM_PRIMZ _UNKNOWN_PRIMZ_UNKNOWN_FLOAT_PRIMZ_UNKNOWN_LONG_DOUBLEZ_IO_FILE_STRUCTZPRIMITIVE_TO_INDEXrUrVrWrXrYdictZG_FLAGSr listrRitems_nameZ_value startswithr rrrrrs  __pycache__/setuptools_ext.cpython-36.opt-1.pyc000064400000014761152344566040015464 0ustar003 MZ @snddlZddlZyeWnek r0eZYnXddZddZddZdd Zd d Z d d Z ddZ dS)NcCsddlm}||dS)Nr)DistutilsSetupError)Zdistutils.errorsr)msgrr&/usr/lib64/python3.6/setuptools_ext.pyerror s rc Cs@t|}|j}WdQRX|d7}t||d}t|||dS)N exec)openreadcompiler)filenameZglobfsrccoderrrexecfiles   rc Csddlm}t|ts*tdt|jft|}y|jd\}}Wn"t k rftd|fYnXt j j |sd}|j ddd }t j j |rd |d|f}td ||fd |d }t||y ||}Wn$tk rtd||fYnXt||s|}t||s0td|t|jft|dsJtd|f|j\} } } } |jrr| j} |j| | dkrt||| nt||| | | | dS)Nr)FFIzEargument to 'cffi_modules=...' must be a str or a list of str, not %r:z3%r must be of the form 'path/build.py:ffi_variable'./z.pyz (rewrite cffi_modules to [%r])z#%r does not name an existing file%sZ__cffi__)__name____file__z!%r: object %r not found in modulez"%r is not an FFI instance (got %r)_assigned_sourcez*%r: the set_source() method was not called)Zcffi.apir isinstance basestringrtyperstrsplit ValueErrorospathexistsreplacerKeyErrorhasattrrZ_windows_unicodecopyZ_apply_windows_unicode_add_py_module _add_c_module) distZmod_specrZbuild_file_nameZ ffi_var_nameextZ rewrittenZmod_varsffi module_namesourcesource_extensionkwdsrrradd_cffi_modulesL            r/c Cspd|krlttd rltjdkrlddl}y(t|jjdd}|dkrLd|d<Wntk rjd|d<YnX|S) a Add py_limited_api to kwds if setuptools >= 26 is in use. Do not alter the setting if it already exists. Setuptools takes care of ignoring the flag on Python 2 and PyPy. CPython itself should ignore the flag in a debugging version (by not listing .abi3.so in the extensions it supports), but it doesn't so far, creating troubles. That's why we check for "not hasattr(sys, 'gettotalrefcount')" (the 2.7 compatible equivalent of 'd' not in sys.abiflags). (http://bugs.python.org/issue28401) On Windows, it's better not to use py_limited_api until issue #355 can be resolved (by having virtualenv copy PYTHON3.DLL). See also the start of _cffi_include.h. Zpy_limited_apigettotalrefcountZwin32rNrT)r$sysplatform setuptoolsint __version__ partitionr) Extensionr.r4Zsetuptools_major_versionrrr_set_py_limited_apiIs  r9c sddlm}ddlm}ddlmddlmddlm dg}|j |j dgt ||}|f|d |d fd d |j dkrg|_ |j j|jjd |Gfd dd} | |jd <dS)Nr)r8) build_ext)mkpath)log) recompilerz $PLACEHOLDERsources)namer>cs\tjj|}jd|||dk r:|j|}|sXjd|S)Nzgenerating cffi module %rzalready up-to-date)rr joininfoZ make_c_source)Ztmpdirpre_runZc_fileupdated)r)r*r<r;r+r=r,r-rrmake_modts  z_add_c_module..make_modr:cseZdZfddZdS)z)_add_c_module..build_ext_make_modcs:jddkr,t|dd}|j|jd<j|dS)Nrz $PLACEHOLDERrB)r>getattrZ build_temprun)selfrB) base_classr)rDrrrFs z-_add_c_module..build_ext_make_mod.runN)r __module__ __qualname__rFr)rHr)rDrrbuild_ext_make_modsrK)N)Zdistutils.corer8setuptools.command.build_extr:distutils.dir_utilr; distutilsr<cffir=extendpopr9Z ext_modulesappendcmdclassget) r(r*r+r,r-r.r8r:Z allsourcesrKr) rHr)r*r<rDr;r+r=r,r-rr'gs         r'csddlmddlm}ddlm}ddlmddlm fdd|j j d |Gfd d d }||j d <|j dkrg|_ |j j |j j d |Gfd dd}||j d <dS)Nr)r;)build_py)r:)r<)r=cs>jd|tjj|j|}|s:jddS)Nzgenerating cffi module %rzalready up-to-date)rArr dirnameZmake_py_source)Zpy_filerC)r*r<r;r+r=rr generate_mods z$_add_py_module..generate_modrUcseZdZfddZdS)z)_add_py_module..build_py_make_modcs@j|jd}|dd7<tjj|jf|dS)Nrz.py)rFrrr r@Z build_lib)rG module_path)rHrWr+rrrFs  z-_add_py_module..build_py_make_mod.runN)rrIrJrFr)rHrWr+rrbuild_py_make_modsr[r:cseZdZfddZdS)z*_add_py_module..build_ext_make_modcsbj||jr^jd}dj|dd}|jd}|j|}|dd}tjj||dS)NrrXrUz.pyrYrY)rFZinplacerr@Zget_finalized_commandZget_package_dirrr )rGrZpackagerUZ package_dir file_name) base_class_2rWr+rrrFs     z._add_py_module..build_ext_make_mod.runN)rrIrJrFr)r^rWr+rrrKsrK)rMr;Zsetuptools.command.build_pyrUrLr:rNr<rOr=rSrTZ py_modulesrR)r(r*r+rUr:r[rKr)rHr^r*rWr<r;r+r=rr&s        r&cCs,t|tr|g}x|D]}t||qWdS)N)rrr/)r(attrvalueZ cffi_modulerrr cffi_moduless  ra) rr2r NameErrorrrrr/r9r'r&rarrrrs  -.1__pycache__/cparser.cpython-36.pyc000064400000050056152344566040013060 0ustar003 MZR@sddlmZddlmZmZddlmZmZyddlmZ Wne k rXddl Z YnXddl Z ddl Z ddl Z y(e jd#krddlZnddlZejZWne k rdZYnXdZe jd e je jBZe jd e je jBZe jd Ze jd Ze jd Ze jdZdae jde jZe jdZ e jdZ!e jdZ"e jdZ#e jdZ$e jdZ%e jdZ&ddZ'ddZ(ddZ)ddZ*dd Z+Gd!d"d"e,Z-dS)$)model) COMMON_TYPESresolve_common_type)FFIError CDefError) _pycparserNzz/\*.*?\*/|//([^\n\\]|\\.)*?$z?^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)\b((?:[^\n\\]|\\.)*?)$z=\s*\.\.\.\s*[,}]|\.\.\.\s*\}z__dotdotdot\d+__$z\[\s*\.\.\.\s*\]z\w+|\Sz-?0?x?[0-9a-f]+[lu]*$z\b(__stdcall|WINAPI)\bz[(]\s*(__stdcall|WINAPI)\bz \b__cdecl\bz9\bextern\s*"(Python|Python\s*\+\s*C|C\s*\+\s*Python)"\s*.z'[*]\s*((const|volatile|restrict)\b\s*)+z3(\b(int|long|short|signed|unsigned|char)\s*)+\.\.\.z\b(double|float)\s*\.\.\.cCstdkrtjatS)N) _parser_cache pycparserZCParserr r /usr/lib64/python3.6/cparser.py _get_parser*srcCs g}xtj|}|sP|j|d|j|jdd}|j|j|j}|jd|rn|jd|d7}d}|}x`|t|kr||}|dkr|d7}n.|dkr|dkrP|d8}n|dkr|dkrP|d7}qxW||||||d}qW|j|dj|S)N()*rrz,;=) _r_star_const_spacesearchappendstartgroupend startswithlenjoin)csourcepartsmatchclosingendposlevelicr r r _workaround_for_old_pycparser0s<          r$cCs0g}xtj|}|sP|jd}|j|d|jd|jdkrT|jdn |jd||dkr|jd|}|dkrtd|jd|d|dkrtd |j||d|||dd}nB|jd |}|dkrtd |j|||d||dd}|jd qW|j|d j |S)NrCz(void __cffi_extern_python_plus_c_start; z!void __cffi_extern_python_start; {}rz!'extern "Python" {': no '}' foundz7cannot use { } inside a block 'extern "Python" { ... }';z'extern "Python": no ';' foundz void __cffi_extern_python_stop;r) _r_extern_pythonrrrrrfindrNotImplementedErrorr)rrrr rZ semicolonr r r _preprocess_extern_pythonZs4         r,c Cstjd|}i}x6tj|D](}|j\}}|jddj}|||<qWtjd|}tjdkrft |}t jd|}t jd|}t jd|}t |}tjd|}ttj|}xtt|D]\}}|j}||dkr|jd ||j}||kstd |d||||d df}q|||d d ks4td |d||||d df}qWtjd |}tjd|}|jd d|fS)N z\ rz2.14z volatile volatile const(z volatile volatile const z[__dotdotdotarray__]=z...z%s,__dotdotdot%d__ %sr z%s __dotdotdot%d__ %sz __dotdotdotint__ z __dotdotdotfloat__ z __dotdotdot__ ) _r_commentsub _r_definefinditergroupsreplacestripr __version__r$ _r_stdcall2 _r_stdcall1_r_cdeclr,_r_partial_arraylist_r_partial_enum enumeratereversedrr*rAssertionError_r_int_dotdotdot_r_float_dotdotdot) rmacrosrZ macronameZ macrovalueZmatchesZnumberpZp2r r r _preprocesss8            rDcCstt}|jd|jd|jd|jd|jdt}d}d}d}xtj|D]}||kr|dkr|r|j||j|d}nj|dkrd }d}nX|dkr|d 7}nF|dkr|d 8}n4|dkr|r|dkr|j||j|n |j||}qXW|S) Nr(,rrtypedefFrrTr)setradd_r_wordsfindalldiscard)rZlook_for_wordsZ words_usedZ is_typedefZparenZ previous_wordZwordr r r _common_type_namess>             rLc@seZdZddZddZddZddZd8d d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZd9ddZdd Zd:d"d#Zd;d$d%Zdd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd!S)?ParsercCs:i|_t|_d|_tj|_i|_i|_g|_ d|_ dS)Nr) _declarationsrG_included_declarations_anonymous_counterweakrefWeakKeyDictionary_structnode2type_options_int_constants _recomplete_uses_new_feature)selfr r r __init__s zParser.__init__c Cs4t|\}}t|}g}x"ztypedef int %s;z@typedef int __dotdotdotint__, __dotdotdotfloat__, __dotdotdot__;z# 1 "%s" )rDrLsortedrNrrrKCDEF_SOURCE_STRINGrlockacquirerparser Zc_parserZ ParseErrorconvert_pycparser_errorrelease) rXrrBZctn typenamesname csourcelinestypenameZ fullcsourceaster r r _parses4          "  z Parser._parsecCsfd}t|}tjdtf|}|rbt|jdd}|j}d|koPt|knrb||d}|S)Nz %s:(\d+):r )strrerr]intr splitlinesr)rXrhrlinemsgrZlinenumrer r r _convert_pycparser_errors zParser._convert_pycparser_errorcCs@|j||}t|}|r*d|j|f}n d|f}t|dS)Nzcannot parse "%s" %szparse error %s)rqrkr5r)rXrhrrorpr r r ra+s   zParser.convert_pycparser_errorFc Cs0|j}z|||d|_|j|Wd||_XdS)N)overridepacked dllexport)rT_internal_parse)rXrrrrsrtZ prev_optionsr r r r`5s z Parser.parsec Cs$|j|\}}}|j|t|j}x |D]}|jdkr*Pq*WdsFtd}yBd|_x4|D]*}|}t|tj j r|j |q\t|tj j rf|jst d|d}t|jjtj jr|jjjdjdr|j|}nxt|jtj jr6t|jjtj jr6t|jjjtj jr6|jjjjdjdr6|j|}n|j|j|jdd\}}|jd |j||d q\|jjd krvq\t d |jj|q\WWnt k r} z&t| jdkr| j|f| _WYdd} ~ XnNtk r} z0|j| |} | r | jdd | f| _WYdd} ~ XnXdS)N __dotdotdot__r__cffi_extern_python_stopz!typedef does not declare any namerZ __dotdotdotT)rdpartial_length_okztypedef )qualsZPragmazBunexpected <%s>: this construct is valid C but not valid in cdef()z *** Err: %srz)ri_process_macrositerextrdr?_inside_extern_python isinstancer c_astZDecl _parse_declZTypedefrtypeIdentifierTypenamesr_get_unknown_typePtrDeclTypeDecl_get_unknown_ptr_type_get_type_and_quals_declare __class____name__rargsrrq) rXrrgrBiteratordeclZ current_declryZrealtyperhrpr r r ru?sX          zParser._internal_parsecCs8||jkr*|j||krdStd|f||j|<dS)Nz%multiple declarations of constant: %s)rUr)rXkeyvalr r r _add_constantsus   zParser._add_constantscCs|jjd}|jd}|r(|dd}|jdrV|dkrV|jd rVd|dd}t|d}|rj| }|j|||jd||dS) NZul-r00xZ0orzmacro )lowerrstriprrmrr)rXrdZint_strnegZpyvaluer r r _add_integer_constant}s     zParser._add_integer_constantcCshxb|jD]V\}}|j}tj|r2|j||q |dkrL|jd||q td||||fq WdS)Nz...zmacro zonly supports one of the following syntax: #define %s ... (literally dot-dot-dot) #define %s NUMBER (with NUMBER an integer constant, decimal/hex/octal) got: #define %s %s)itemsr5_r_int_literalrrrr)rXrBrvaluer r r r{s zParser._process_macroscCsX|j||}|jjdrd}n$|jdkr.d}n|jdkr>d}nd}|j||j|dS)Nrtzdllexport_python Z__cffi_extern_python_startzextern_python Z!__cffi_extern_python_plus_c_startzextern_python_plus_c z function )_get_type_pointerrTgetr~rrd)rXtpryrtagr r r _declare_functions    zParser._declare_functioncCs|j}t|tjjrJ|j||jd\}}t|tjs8t |j |||nt|tjj rf|j d|nHt|tjj r|j d|n,t|tjjr|j d|n|jstd||jr |j|dd\}}|jr|j |||n*|jr(t|dr(t|jd r(tj|jjr(|j|j|jjn|jrt|jtjjr|jjd krt|jjd rtj|jjjr|j|jd |jjjn||tjkr|jjd r|j|_nX|jd krtd |tj@r|j r|j d|j||dn|j d|j||ddS)N)rdstructunionenumz'construct does not declare any variableT)rxinitrrZ__cffi_extern_python_rwz|sz/Parser._parse_function_type..ryr rrZ __stdcallrzrzrz)r;getattrrr=rrrrrr rrrrpoprrrryrtuple) rXrfuncnamerr"argZellipsisrresultryabir )rXr rgs6       zParser._parse_function_typecCs6t|tjrtj|j|St|tjr.|jS|SdS)N)rrrritemrr)rXrryr r r rs   zParser._as_func_argc Cs,y |j|Stk rYnX|}|j}|dkrb|dk rDd|}n|jd7_d|j}d}n"|}d||f}|jj|d\}} |dkr|dkrtj|ddd}nP|dkrtj|ddd}n6|dkr|dkrt d |j ||j }nt d |f|dk r2|j ||n"|dkr2|j dk r2td ||jsD|j||jrjd |jkrj|j d |j|||j|<|dkr|S|jdkr|S|jdk rt d|g} g} g} g} x|jD]}t|jtjjrdj|jjdkr|j||q|jdkrd}n |j|j}d|_|j|jdd\}}|jrH|j||t|tjrj|jrj|j||| j |jpxd| j || j || j |qWt!| |_t!| |_"t!| |_#t!| |_$| dgt%| krt|tjr|jrtd|f|j&jd|_'|j(r(d|_(|j)j ||S)Nz$%srz$%dz%s %srrrrvz!Enums cannot be declared with ...z kind = %rz^enum %s: the '{}' declaration should appear on the first time the enum is mentioned, not laterrz anonymous %sz"duplicate declaration of struct %srFT)rxz#%s: using both bitfields and '...;'rsr)NNrzrz)*rSKeyErrorrdrPrNrrZ StructTypeZ UnionTyper_build_enum_typevaluesr?rr+rZforce_the_namedeclsfldnamesrrr rrrr _make_partialbitsizer_partial_lengthrpartialrrfldtypes fldbitsizefldqualsrrTrsZ completedrV)rXkindrrdrZ force_name explicit_namerr_rrrrrrZfqualr r r rs                             z"Parser._get_struct_union_enum_typecCsBt|tjstd|f|j r8| r8td|fd|_dS)Nz%s cannot be partialz%s is partial but has no C nameT)rrZ StructOrUnionrZ has_c_namer+r)rXrrr r r rs  zParser._make_partialcCst|tjjr|j}|jdrF|jds2|jdrsN               *27(__pycache__/backend_ctypes.cpython-36.opt-1.pyc000064400000115057152344566040015341 0ustar003 MZf@sddlZddlZddlZddlZddlmZejdkr sr c@s eZdZdS) CTypesTypeN)__name__ __module__ __qualname__rrrrr sr c@seZdZeZdgZdZddZeddZe ddZ ed d Z ed d Z e d dZ ed5ddZeddZddZddZd6ddZddZeddZdd Zed!d"Zd#d$Zed%d&Zd'd(Zd)d*Zed+Zed,Zed-Zed.Zed/Zed0Z d1d2Z!d3d4Z"dS)7 CTypesData __weakref__zcGstd|jfdS)Nzcannot instantiate %r) TypeError __class__)selfargsrrr__init__szCTypesData.__init__cCstd|jfdS)Nz+expected a pointer or array ctype, got '%s')r _get_c_name)clsinitrrr_newpszCTypesData._newpcCstdS)N)r)valuerrr _to_ctypesszCTypesData._to_ctypesc Gs^y |j}Wn"tk r,td|fYnX|rR|j|}t||sZ|j|}n|j}|S)Nzcannot create an instance of %r)_ctypeAttributeErrorrr isinstance)rrctyperesrrr_arg_to_ctypes s    zCTypesData._arg_to_ctypescCs|dkr|jS|j|SdS)N)r )rrrrr_create_ctype_obj.szCTypesData._create_ctype_objcCstdS)N)r)Z ctypes_valuerrr _from_ctypes5szCTypesData._from_ctypescCs|jjd|S)Nz &) _reftypenamereplace)r replace_withrrrr9szCTypesData._get_c_namecCs*d|jf|_d|jf|_d|_dS)Nz CData<%s>ffi)rr r r )rrrr _fix_class=szCTypesData._fix_classcCstdS)N)NotImplementedError)rrrr _get_own_reprCszCTypesData._get_own_reprcCs8|dkr dS|dkr,|ddtjtj>7}d|SdS)NrZNULLrz0x%x)ctypessizeofc_void_p)raddressrrr _addr_reprFs zCTypesData._addr_reprNcCs|j}d|p|j|fS)Nz )r*r)rc_nameZownrrr__repr__NszCTypesData.__repr__cCs8|dkrtd|jfntd|j|jfdS)Nzcannot convert %r to an addresszcannot convert %r to %r)rr)rBClassrrr_convert_to_addressRs zCTypesData._convert_to_addresscCs tj|jS)N)r,r-r)rrrr _get_sizeZszCTypesData._get_sizecCs tj|jS)N)r,r-r)rrrr_get_size_of_instance^sz CTypesData._get_size_of_instancecCstd|jfdS)Nzcannot cast to %r)rr)rsourcerrr _cast_fromaszCTypesData._cast_fromcCs |jdS)N)r4)rrrr_cast_to_integereszCTypesData._cast_to_integercCs tj|jS)N)r,Z alignmentr)rrrr _alignmenthszCTypesData._alignmentcCstd|jdS)Nz#cdata %r does not support iteration)rr)rrrr__iter__lszCTypesData.__iter__cs tt|fdd}||_|S)Ncszt|t }t|to t|t }|r@|r@|jd|jdS|sH|rLtSt|tr\|j}t|trl|j}||SdS)N)rCTypesGenericPrimitiverr4NotImplemented_value)rotherZv_is_ptrZw_is_ptr)cmpfuncrrcmprs       z!CTypesData._make_cmp..cmp)getattroperatorZ func_name)namerAr)r@r _make_cmpps  zCTypesData._make_cmp__eq____ne____lt____le____gt____ge__cCst|jdS)N)hashr4)rrrr__hash__szCTypesData.__hash__cCstd|fdS)Nz string(): %r)r)rmaxlenrrr _to_stringszCTypesData._to_string)r#)N)#r r r r Z __metaclass__ __slots__r classmethodr staticmethodrr r!r"rr(r*r0r2r4r5r6r8r9r:r;rErFrGrHrIrJrKrMrOrrrrrs>           rc@s eZdZgZddZddZdS)r<cCs t|jS)N)rLr>)rrrrrMszCTypesGenericPrimitive.__hash__cCst|j|jS)N)reprr"r>)rrrrr*sz$CTypesGenericPrimitive._get_own_reprN)r r r rPrMr*rrrrr<sr<c@s,eZdZgZeddZddZddZdS)CTypesGenericArraycCs||S)Nr)rrrrrrszCTypesGenericArray._newpccs$xtt|D]}||VqWdS)N)xrangelen)rirrrr;szCTypesGenericArray.__iter__cCs|jtj|jS)N)r0r, addressof_blob)rrrrr*sz CTypesGenericArray._get_own_reprN)r r r rPrQrr;r*rrrrrTs rTc@seZdZddgZdZdZeddZeddZed d Z d d Z d dZ ddZ e Z eddZeddZeddZddZdS)CTypesGenericPtr_address _as_ctype_ptrFpointercCs||S)Nr)rrrrrrszCTypesGenericPtr._newpcCsV|dkrd}n>t|tr"|j}n*t|ttfr6|}ntd|t|jf|j|S)Nrzbad type for cast to %r: %r) rrr9intlongrtyper _new_pointer_at)rr7r/rrrr8s  zCTypesGenericPtr._cast_fromcCs$|j|}||_tj||j|_|S)N)__new__r[r,castrr\)rr/rrrrras z CTypesGenericPtr._new_pointer_atc Cs&y |j|jStk r dSXdS)Nz???)r0r[r)rrrrr*s zCTypesGenericPtr._get_own_reprcCs|jS)N)r[)rrrrr9sz!CTypesGenericPtr._cast_to_integercCs t|jS)N)boolr[)rrrr __nonzero__szCTypesGenericPtr.__nonzero__cCs4t|tstdt|j|j|}tj||jS)Nzunexpected %s object) rrrr`r r4r,rcr)rrr/rrrrs  zCTypesGenericPtr._to_ctypescCstj|tjjpd}|j|S)Nr)r,rcr.rra)r ctypes_ptrr/rrrr"szCTypesGenericPtr._from_ctypescCs|r|j|j|_dS)N)rcontents)rrfrrrr _initializeszCTypesGenericPtr._initializecCs0||jdfks|js|jr |jStj||SdS)N)r_automatic_castsr[rr4)rr3rrrr4sz$CTypesGenericPtr._convert_to_addressN)r r r rPrikindrQrr8rar*r9re__bool__rr"rhr4rrrrrZs     rZc@s\eZdZdgZeddZddZeddZdd Zed d Z ed d Z dddZ dS)CTypesBaseStructOrUnionrYcCstd|fdS)Nz!cannot instantiate opaque type %s)r)rrrrrr!sz)CTypesBaseStructOrUnion._create_ctype_objcCs|jtj|jS)N)r0r,rXrY)rrrrr*sz%CTypesBaseStructOrUnion._get_own_reprcCst|j|jS)N)rBroffset)r fieldnamerrr _offsetofsz!CTypesBaseStructOrUnion._offsetofcCs.t|dd|jkrtj|jStj||SdS)N_BItem)rBrr,rXrYrr4)rr3rrrr4s z+CTypesBaseStructOrUnion._convert_to_addresscCs|j|}||_|S)N)rbrY)rZctypes_struct_or_unionrrrrr"s z$CTypesBaseStructOrUnion._from_ctypescCs|jS)N)rY)rrrrrr sz"CTypesBaseStructOrUnion._to_ctypesNcCstj||p|jdS)Nz &)rr2r)rr1rrrr2 sz CTypesBaseStructOrUnion.__repr__)N) r r r rPrQr!r*ror4r"rr2rrrrrls    rlc@seZdZejejejejejej ej ej ej ej ejejejejdZxfdMD]^ZejeeZeeedde<eejejkreeed <eejejkrLeeed <qLWxtdND]lZejeeZeeedde<eejejkreeed<eeed<eejejkreeed<qWddZddZddZdOddZddZdd Zd!d"Zd#d$Zd%d&Z d'd(Z!d)d*Z"dRd,d-Z#d.d/Z$d0d1Z%d2d3Z&d4d5Z'dTd6d7Z(dVd8d9Z)d:d;Zdd?Z+d@dAZ,dBdCZ-dDZ.dWdEdFZ/e0Z1dGdHZ2dXdIdJZ3dYdKdLZ4dDS)Z CTypesBackend)charshortr^r_z long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long longfloatdouble_Boolunsigned long long unsigned long unsigned intunsigned short unsigned charzuint%d_tr+Z uintptr_tZsize_t long longr_r^rs signed charzint%d_tZintptr_tZ ptrdiff_tZssize_tcCs d|_d|_tj|_tj|_dS)Nr) RTLD_LAZYRTLD_NOWr, RTLD_GLOBAL RTLD_LOCAL)rrrrr7szCTypesBackend.__init__cCs ||_dS)N)r')rr'rrrset_ffi=szCTypesBackend.set_fficCsttfS)N)rr )rrrr _get_types@szCTypesBackend._get_typesrcCstj||}t||S)N)r,ZCDLL CTypesLibrary)rpathflagscdllrrr load_libraryCs zCTypesBackend.load_librarycCsGdddt}|j|S)Nc@s,eZdZgZdZeddZeddZdS)z/CTypesBackend.new_void_type..CTypesVoidzvoid &cSsdS)Nr)novaluerrrr"Ksz.CTypesVoid._from_ctypescSs |dk rtdt|jfdS)NzNone expected, got %s object)rr`r )rrrrrNsz:CTypesBackend.new_void_type..CTypesVoid._to_ctypesN)r r r rPr$rRr"rrrrr CTypesVoidHs r)rr()rrrrr new_void_typeGs zCTypesBackend.new_void_typecsdkrt|jdkr(d}n<dkr6d}n.dkrDd}ndkrRd }nd }djdkfd d |GfdddtjS)NZwchar_trrrtru signed char unsigned charbytervrdr^rcsjt|tttfrt|}nLt|tr.|j}n8t|trBt|}n$|dkrPd}ntdt |j f|S)Nrzbad type for cast to %r: %r) rr^r_rtrr9rordrr`r )r7)CTypesPrimitiverr_cast_source_to_inths     z=CTypesBackend.new_primitive_type.._cast_source_to_intcseZdZdgZZdZZddZefddZ edksHedkrbe fd d Z d d Z ed kre fdd Z dd Z edkre fdd Z dd Z edkre fdd Z dd Z ddZ e Zedksedksed krefddZedkr efddZddZnddZeZedkrFefddZedd Zefd!d"Zedkrtd#d$Zedkrd%d$Zd&S)'z9CTypesBackend.new_primitive_type..CTypesPrimitiver>z%s &cSs ||_dS)N)r>)rrrrrr}szBCTypesBackend.new_primitive_type..CTypesPrimitive.__init__cs|dkrSj|S)N)r)r)rrrrr!szKCTypesBackend.new_primitive_type..CTypesPrimitive._create_ctype_objr^rcs|}|j}||S)N)r)rr7)rrrrr8s zDCTypesBackend.new_primitive_type..CTypesPrimitive._cast_fromcSs|jS)N)r>)rrrr__int__szACTypesBackend.new_primitive_type..CTypesPrimitive.__int__rdcs$t|tttfs|}|t|S)N)rr^r_rtrd)rr7)rrrr8scSs|jS)N)r>)rrrrrsrrcs|}t|d@}||S)N)bytechr)rr7)rrrr8s cSs t|jS)N)rr>)rrrrrsrtcsNt|tr n0t|tr4t|dr*t|}q)rrrrrscSs|jS)N)r>)rrrrrszCCTypesBackend.new_primitive_type..CTypesPrimitive.__float__csnt|ttfs4t|tr"t|}ntdt|j|j|krj r^|dkr^tdn td|S)Nzinteger expected, got %srz%s: negative integerz%s: integer out of bounds) rr^r_rrr`r r OverflowError)x)r is_signedrDrrrs  zDCTypesBackend.new_primitive_type..CTypesPrimitive._to_ctypescs@t|trt|dkr|St|r*|jStdt|jdS)Nrzcharacter expected, got %s)rrrVr>rr`r )r)rrrrs  cSst|jdkS)Nr)rr>)rrrrreszECTypesBackend.new_primitive_type..CTypesPrimitive.__nonzero__cSs |jdkS)Nr)r>)rrrrrescs.t|ttttfs$tdt|j|jS)Nzfloat expected, got %s) rr^r_rtrrr`r r)r)rrrrscSs t|d|S)Nr)rB)rrrrr"szFCTypesBackend.new_primitive_type..CTypesPrimitive._from_ctypescsj||_dS)N)rr)blobr)rrrrhszECTypesBackend.new_primitive_type..CTypesPrimitive._initializecSs|jS)N)r>)rrNrrrrOszDCTypesBackend.new_primitive_type..CTypesPrimitive._to_stringcSst|jd@S)Nr)chrr>)rrNrrrrOsN)r r r rPrr$rjrrRr!rQr8rrr9rrerkr"rhrOr)rrrrkind1rDrrrwsF       r)rtru)rrr)r)PRIMITIVE_TYPESrr<r()rrDrjr)rrrrrrDrnew_primitive_typeWs$  wz CTypesBackend.new_primitive_typecs|jj}|tjdkr dn>|tjd|tjdfkrFdn|tjkrZdndGfdd d t|jjtjks|jjtjdkrd _jS) Nrrcharpz signed charz unsigned charbytepvoidpgenericcseZdZdgZdkr edg7ZZedrHejjZej jZ nej Ze e rdjdZn jdZfddZd d Zd d Zfd dZfddZdksdkrefddZdks֔dkrddZfddZZS)z1CTypesBackend.new_pointer_type..CTypesPtr_ownrZ __as_strbufrz(* &)z * &csbj|}dkr8tj|jd|_tj|j|j|_n tj||_tj|jtj j|_ d|_ dS)NrT) r!r,Zcreate_string_bufferrZ_CTypesPtr__as_strbufrcrr\r]r.r[r)rrZctypeobj)BItemrjrrr s   z:CTypesBackend.new_pointer_type..CTypesPtr.__init__cSs,t|ttfr$|j|j||jStSdS)N)rr^r_rar[ _bitem_sizer=)rr?rrr__add__sz9CTypesBackend.new_pointer_type..CTypesPtr.__add__cSsNt|ttfr$|j|j||jSt|t|krF|j|j|jStSdS)N)rr^r_rar[rr`r=)rr?rrr__sub__ s z9CTypesBackend.new_pointer_type..CTypesPtr.__sub__cs(t|ddr|dkrtj|j|S)NrFr)rB IndexErrorr"r\)rindex)rrr __getitem__)sz=CTypesBackend.new_pointer_type..CTypesPtr.__getitem__csj||j|<dS)N)rr\)rrr)rrr __setitem__.sz=CTypesBackend.new_pointer_type..CTypesPtr.__setitem__rcs4|r t|dtr tj|dSt|j|SdS)Nr)rrr,Zc_char_psuperr )rr) CTypesPtrrrrr 2sz@CTypesBackend.new_pointer_type..CTypesPtr._arg_to_ctypesrcsf|dkrtj}tj|jtjtjd}x ||krH|dkrH|d7}q*Wdjfddt|DS)Nrrrcsg|] }|qSrr).0rW)prr BszPCTypesBackend.new_pointer_type..CTypesPtr._to_string..) sysmaxsizer,rcr\POINTERc_charjoinrange)rrNnr)rrrO:s z.CTypesPtr._to_stringcs.t|ddr dtj|jjfSt|jS)NrFzowning %d bytes)rBr,r-r\rgrr*)r)rrrrr*Ds z?CTypesBackend.new_pointer_type..CTypesPtr._get_own_repr)r r r rPrprr,rrr-rr. issubclassrTrr$rrrrrrQr rOr* __classcell__r)rrrj)rrrs*          rT)r'_get_cached_btyper PrimitiveTypeZ void_typerZrir()rrgetbtyper)rrrjrnew_pointer_typesMzCTypesBackend.new_pointer_typecsdkrdndj|jj}|tjdkr.CTypesArrayrYrNrcsdkr\t|ttfr |}d}n0dkr@t|tr@t|d}nt|}t|}j||_|j|_d|_|dk r|j |j|dS)NrrrT) rr^r_rrVtuplerrYrrh)rrZlen1)rrjlengthrrrjs  z:CTypesBackend.new_array_type..CTypesArray.__init__csttr&fddttDnttt|krFtdtj|tjj }tj j }tj j }x6t D]*\}}tj||||}j|j|qxWdS)Ncsg|]}||dqS)rr)rrW)rrrr}szQCTypesBackend.new_array_type..CTypesArray._initialize..ztoo many initializers)rrrrVrrr,rcr.rrrr- enumeraterhrg)rraddrPTRitemsizerWrr)r)rrrhzs   z=CTypesBackend.new_array_type..CTypesArray._initializecSs t|jS)N)rVrY)rrrr__len__sz9CTypesBackend.new_array_type..CTypesArray.__len__cs2d|kot|jkns"tj|j|S)Nr)rVrYrr")rr)rrrrsz=CTypesBackend.new_array_type..CTypesArray.__getitem__cs6d|kot|jkns"tj||j|<dS)Nr)rVrYrr)rrr)rrrrsz=CTypesBackend.new_array_type..CTypesArray.__setitem__rrrcsj|dkrt|j}tj|jtjtjd}x ||krL|dkrL|d7}q.Wdjfddt|DS)Nrrrrcsg|] }|qSrr)rrW)rrrrszPCTypesBackend.new_array_type..CTypesArray._to_string..)rVrYr,rcrrrr)rrNrr)rrrOs  z.CTypesArray._to_stringcs,t|ddrdtj|jfSt|jS)NrFzowning %d bytes)rBr,r-rYrr*)r) CTypesArrayrrrr*s z?CTypesBackend.new_array_type..CTypesArray._get_own_reprcs.|dfks|jrtj|jStj||SdS)N)rir,rXrYrr4)rr3)rrrr4s zECTypesBackend.new_array_type..CTypesArray._convert_to_addresscsj}||_|S)N)rbrY)Z ctypes_arrayr)rrrr"s z>CTypesBackend.new_array_type..CTypesArray._from_ctypescs j|S)N)r )r)rrrr sz@CTypesBackend.new_array_type..CTypesArray._arg_to_ctypescs8t|ttfr0jtj|j|tjjSt SdS)N) rr^r_rar,rXrYr-rr=)rr?)rrrrrs  z9CTypesBackend.new_array_type..CTypesArray.__add__cSstd|jfdS)Nz casting to %r)r)r)rr7rrrr8sz.CTypesArray._cast_from)r r r rPrappendrr$_declared_length_CTPtrrrRrhrrrrOr*r4r"r rrQr8rr)rrrbracketsrjr)rrr`s(       r)rpr'rrrrTr()rrrrr)rrrrrjrrnew_array_typeQsczCTypesBackend.new_array_typecsFGddd|d|f_|Gfdddt}|j|S)Nc@s eZdZdS)z;CTypesBackend._new_struct_or_union..struct_or_unionN)r r r rrrrstruct_or_unionsrz%s_%scs(eZdZdgZZdfZZZdS)z?CTypesBackend._new_struct_or_union..CTypesStructOrUnionrYz%s &N)r r r rPrr$_kindrjr)rrDrrrCTypesStructOrUnions r)r rlr()rrjrDZbase_ctypes_classrr)rrDrr_new_struct_or_unions z"CTypesBackend._new_struct_or_unioncCs|jd|tjS)Nstruct)rr,Z Structure)rrDrrrnew_struct_typeszCTypesBackend.new_struct_typecCs|jd|tjS)Nunion)rr,ZUnion)rrDrrrnew_union_typeszCTypesBackend.new_union_typercsP|dks|dkrtdjdd|Ddd|D}dd|D}i} g} xN|D]F\} } } | dkr| j| | jf| | | <qV| j| | j| ft| | <qVW|d@rd_| _| _tfd d }|_fd d j d kt t t ||x|D]\} } } | dkr$tdt | r.cSsg|]\}}}|qSrr)rrrrrrrrscSsg|]\}}}|qSrr)rrrrrrrrsr+rcs}|dk r|||S)Nr)rresult) initializerrrr!s zACTypesBackend.complete_struct_or_union.._create_ctype_objc sr"t|dkr"tdt|ft|tsxt|ttfrBtdt|}t|tkrjtdjtt |}t j |}xT|j D]H\}}|\}}j |}t j|j}t j|||} |j| j|qWdS)NrzPunion initializer: %d items given, but only one supported (use a dict if needed)zunion initializer: got a strz"too many values for %s initializer)rV ValueErrorrdictrunicoderrrzipr,rXitemsrorrrcrhrg) rrrrrrrrmrr)rfnamesis_unionname2fieldtyperrrs&       z:CTypesBackend.complete_struct_or_union..initializerr#znested anonymous structs/unionsz1the field name %r conflicts in the ctypes backendcSs(tj|j}tj|||}|j|jS)N)r,rXrYrcr"rg)rrrrmrrrrrrgetters z6CTypesBackend.complete_struct_or_union..gettercSst|j||j|dS)N)setattrrYr)rrrrrrrsetter$sz6CTypesBackend.complete_struct_or_union..settercSs&tj|j}tj|||}|j|S)N)r,rXrYrcr")rrZ BFieldPtrrmrrrrrrr*s cSs|jt|j|S)N)r"rBrY)rrrrrrr2scSsN|j|}t|j|}t|j|||t|j|krJt|j||tddS)Nzvalue too large for bitfield)rrBrYrr)rrrrZoldvaluerrrr4s   cSst|d|S)Nr)rB)rrrrrrCscSst|d||dS)Nr)r)rrrrrrrEs)r)rrEllipsisZ_pack_Z_fields_ _bfield_typesrRr!rrrrrror,rrrTrrrpropertyr'rrZ PointerType)rrZfieldstpZ totalsizeZtotalalignmentZsflagsZbtypesZ bitfieldsZ bfield_typesZcfieldsrrrr!rrrr)rrrrrrrcomplete_struct_or_unionsf            z&CTypesBackend.complete_struct_or_unioncsNddDrjddjGfdddtjS)NcSsg|] }|jqSr)r)rBArgrrrrJsz3CTypesBackend.new_function_type..z...z, cseZdZddgZejeddfddDddiZjd fZ dfd d Z e d d Z ddZ fddZfddZZS)z:CTypesBackend.new_function_type..CTypesFunctionPtr _own_callback_namerNcSsg|] }|jqSr)r)rrrrrrRszECTypesBackend.new_function_type..CTypesFunctionPtr. use_errnoTz (* &)(%s)csddltdddk r*jjndfdd}ttrptjtjfddDddi}nj }|||_ tj |j tjj |_ |_dS) Nrrc sg}x&t|D]\}}|j|j|qWy|}j|}Wnj}YnXttr|r|tj|tj j }nd}|S)N) rrr"r print_excrrZr,rcr.r)rZargs2argrZres2)BArgsBResulterrorr tracebackrrcallback_s  zUCTypesBackend.new_function_type..CTypesFunctionPtr.__init__..callbackcSsg|] }|jqSr)r)rrrrrrvszWCTypesBackend.new_function_type..CTypesFunctionPtr.__init__..rT)rrBr"r!rrZr, CFUNCTYPEr.rr\rcrr[r)rrrrZcallback_ctype)rrCTypesFunctionPtr has_varargs)rrrrrVs     zCCTypesBackend.new_function_type..CTypesFunctionPtr.__init__cSs|r tddS)NzActypes backend: not supported: initializers for function pointers)r))rfrrrrrhszFCTypesBackend.new_function_type..CTypesFunctionPtr._initializecSsPt|dd}|rD|jjd}|j|ddkr6d|}|jjd|}tj||S)Nrz(* &)rz )* )rBr$rr%rr2)rr1rWrrrr2s  zCCTypesBackend.new_function_type..CTypesFunctionPtr.__repr__cs*t|dddk rd|jfSt|jS)Nrz calling %r)rBrrr*)r)rrrrr*s zHCTypesBackend.new_function_type..CTypesFunctionPtr._get_own_reprcsr&|td}|dt}ng}x&t|D]\}}|j|j|q6Wrxjt|D]^\}}|dkr|jtjdq`t|tst ddt|t |j f|j|j|q`W|j |}j |S)NrzKargument %d passed in the variadic part needs to be a cdata object (got %s)r)rVrrr rr,r.rrrr`r r\r")rrZ extraargsZ ctypes_argsrrrWr)rrrrr__call__s$  zCCTypesBackend.new_function_type..CTypesFunctionPtr.__call__)N)r r r rPr,rrBrrr$rrRrhr2r*rrr)rrrrnameargs)rrrOs)  r)rrrZr()rrrrr)rrrrrrnew_function_typeIs  \zCTypesBackend.new_function_typecs8ttt|t|Gfddd|}|j|S)Ncs0eZdZgZdZfddZfddZdS)z/CTypesBackend.new_enum_type..CTypesEnumz%s &c s4|j}yd||fStk r.t|SXdS)Nz%d: %s)r>KeyErrorstr)rr)reverse_mappingrrr*s z=CTypesBackend.new_enum_type..CTypesEnum._get_own_reprc s,|j}y|Stk r&t|SXdS)N)r>rr)rrNr)rrrrOs z:CTypesBackend.new_enum_type..CTypesEnum._to_stringN)r r r rPr$r*rOr)rDrrr CTypesEnums r)rrreversedr()rrDZ enumeratorsZ enumvaluesZ CTypesIntrr)rDrr new_enum_types   zCTypesBackend.new_enum_typecCstjS)N)r, get_errno)rrrrrszCTypesBackend.get_errnocCstj|dS)N)r, set_errno)rrrrrrszCTypesBackend.set_errnocCs |j|S)N)rO)rbrNrrrstringszCTypesBackend.stringcCs tddS)Nzbuffer() with ctypes backend)r))rZbptrsizerrrbufferszCTypesBackend.buffercCst|tr|jS|jSdS)N)rrr6r5)rZcdata_or_BTyperrrr-s zCTypesBackend.sizeofcCs|jS)N)r:)rBTyperrralignofszCTypesBackend.alignofcCst|tst|j|S)N)rrrr)rr r7rrrnewps zCTypesBackend.newpcCs |j|S)N)r8)rr r7rrrrcszCTypesBackend.castcCs |||S)Nr)rr r7ronerrorrrrrszCTypesBackend.callbackNc s|jdkr.ddl}Gddd|j}i|f|_|j\}|dkrpy||=Wntk rjtdYnXdSfdd}|j|j||}||f|||<|S)Nrc@s$eZdZddZddZddZdS)z CTypesBackend.gcp..MyRefcSs |}||kp|dk o||kS)Nr)rr?ZmyrefrrrrFsz'CTypesBackend.gcp..MyRef.__eq__cSs ||k S)Nr)rr?rrrrGsz'CTypesBackend.gcp..MyRef.__ne__c Ss.y|jStk r(t||_|jSXdS)N)_hashrrL)rrrrrMs  z)CTypesBackend.gcp..MyRef.__hash__N)r r r rFrGrMrrrrMyRefsrzFCan remove destructor only on a object previously returned by ffi.gc()cs$j|d\}}|dk r ||dS)N)NN)pop)kcdata destructor) weak_cacherrremovesz!CTypesBackend.gcp..remove)_weakref_cache_refweakrefrefrrrctypeof)rrrrrrrZ new_cdatar)rrgcps    zCTypesBackend.gcpcCs |j|S)N)r)rr r&rrrgetcnameszCTypesBackend.getcnamecCst|tr\|dkr"t|tr"|j}t|ts4td|j|}|tkrNtd||j |fSt|t t frt|t rz|j }t|tstd|j}|j|}|tjkrt||fStt|dS)Nrz expected a struct or union ctypeznot supported for bitfieldszexpected an array or ptr ctype)rrrrZrprlrrrror^r_rTrr5rrrr`)rr rnrrrrmrrr typeoffsetofs(       zCTypesBackend.typeoffsetofcCst|tr tjt|j|}n^t|tr\|dksDtt|jt rLt dt|j|}n"t|t rvt|j|}nt d|rtj tj tj |tj j |t|}|j|S)Nzunexpected cdata typez$expected a )rrlr,r]r`rrZrrprrTrcr.rr")rZBTypePtrrrmZptrrrr rawaddressof.s     zCTypesBackend.rawaddressof)rwrxryrzr{)r|r_r^rsr})rrr)rrrr)rr)r)r)r)N)5r r r r,rZc_shortZc_intZc_longZ c_longlongZc_byteZc_ubyteZc_ushortZc_uintZc_ulongZ c_ulonglongZc_floatZc_doubleZc_boolrrr-Z_sizer.Zc_size_trrrrrrrrrrrrrrrrrrr r rcrrrr`rrrrrrrrrqsx      `u le   % rqc@s,eZdZddZddZddZddZd S) rcCs||_||_dS)N)backendr)rrrrrrrDszCTypesLibrary.__init__cCs t|j|}|j|}||_|S)N)rBrr"r)rr rDZc_funcfuncobjrrr load_functionHs  zCTypesLibrary.load_functioncCsJy|jj|j|}Wn*tk r>}zt|WYdd}~XnX|j|S)N)rin_dllrrr)r")rr rD ctypes_objerrr read_variableNs zCTypesLibrary.read_variablecCs@|j|}|jj|j|}tjtj|tj|tj|jdS)N)rrr rr,ZmemmoverXr-)rr rDrZnew_ctypes_objr!rrrwrite_variableUs   zCTypesLibrary.write_variableN)r r r rrr#r$rrrrrBsr)r)r,Z ctypes.utilrCrr#r version_inforrrrr^r_rrUr`r objectrr<rTrZrlrqrrrrrs,    C#7__pycache__/vengine_gen.cpython-36.pyc000064400000051061152344566040013702 0ustar003 MZ4h@sHddlZddlZddlZddlmZddlmZGdddeZdZ dS)N)model)VerificationErrorc@seZdZdZdZddZddZddZd d ZdZd d Z ddZ d[ddZ ddZ ddZ ddZddZddZeZeZeZddZeZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Z d5d6Z!d7d8Z"d9d:Z#d\d=d>Z$d?d@Z%eZ&d]dAdBZ'dCdDZ(dEdFZ)dGdHZ*dIdJZ+d^dLdMZ,d_dNdOZ-dPdQZ.dRdSZ/eZ0dTdUZ1dVdWZ2eZ3dXdYZ4d;S)`VGenericEnginegFcCs||_|j|_g|_i|_dS)N)verifierffiexport_symbols_struct_pending_verification)selfrr #/usr/lib64/python3.6/vengine_gen.py__init__szVGenericEngine.__init__cCs|jd|jdS)Nr ) setdefaultr )r kwdsr r r patch_extension_kwdssz#VGenericEngine.patch_extension_kwdscCsTxN|D]F}||}|dkr tj}x*|D]"}tjj||}tjj|r&|Sq&WqWdS)N)syspathosjoinisfile)r Z module_namerZ so_suffixesZ so_suffixbasenamedirnamefilenamer r r find_modules   zVGenericEngine.find_modulecCsdS)Nr )r r r r collect_types%szVGenericEngine.collect_typescCs|jj|ddS)N )Z_fwrite)r Zwhatr r r _prnt(szVGenericEngine._prntcCs`|j}|t||jj|jdtjdkr\tjdkr>d}nd}|jj}|d||fdS)NZdeclZwin32ZPyInit_Zinitzvoid %s%s(void) { } )r ) rcffimod_headerrZpreamble _generaterplatform version_infoZget_module_name)r prntprefixmodnamer r r write_source_to_f+s     z VGenericEngine.write_source_to_frcsjjj}tjjtjjj}|j||j dGfdddt j d}j d|d|S)NZloadingcs&eZdZZjZgZfddZdS)z/VGenericEngine.load_library..FFILibrarycsjS)N) _cffi_dir)r ) FFILibraryr r __dir__Tsz7VGenericEngine.load_library..FFILibrary.__dir__N)__name__ __module__ __qualname__Z_cffi_generic_modulerZ _cffi_ffir)r+r )r*moduler r r r*Psr*rZloaded)library) r_backendrrrcurdirrZmodulefilename load_library_loadtypes ModuleType)r flagsbackendrr0r )r*r/r r r3As  zVGenericEngine.load_librarycCs$dd|jjjjD}|j|S)NcSsg|]\}\}}||fqSr r ).0keytpZqualr r r ^sz4VGenericEngine._get_declarations..)rZ_parserZ _declarationsitemssort)r Zlstr r r _get_declarations]sz VGenericEngine._get_declarationscCsx|jD]\}}|jdd\}}yt|d||f}Wn tk rXtd|YnXy|||Wq tk r}ztj||WYdd}~Xq Xq WdS)N rz_generate_gen_%s_%sznot implemented in verify(): %r)r?splitgetattrAttributeErrorr Exceptionrattach_exception_info)r step_namenamer;kindrealnamemethoder r r r"cs zVGenericEngine._generatec Ksx||jD]p\}}|jdd\}}t|d||f}y||||f|Wq tk rx} ztj| |WYdd} ~ Xq Xq WdS)Nr@rz _%s_gen_%s)r?rArBrDrrE) r r/rFrrGr;rHrIrJrKr r r r4rs zVGenericEngine._loadcCsdS)Nr )r r;rGr r r _generate_nothing|sz VGenericEngine._generate_nothingcKsdS)Nr )r r;rGr/rr r r _loaded_noopszVGenericEngine._loaded_noopcst|tjst|jr(|jd||dS|j}t|j}g}xVGenericEngine._generate_gen_function_decl..rz *rz, voidz _cffi_f_%sr@z %s%s(%s)z result of %s{z*r = zreturn z %s%s(%s);}) isinstancerFunctionPtrTypeAssertionErrorellipsis_generate_gen_constrlenargs enumerate StructOrUnionappendzipresultinsertrO void_typerr abiZVoidType)r r;rGr%ZnumargsZargnamesirPZ indirectionZarglistZtpresult wrappernamerdZfuncdeclZ result_coder )rRr _generate_gen_function_declsJ      z*VGenericEngine._generate_gen_function_declcCsrt|tjst|jr*|jd|||}n(g}|}tdd|jDsVt|jtj rg}xDt |jD]6\} } t| tj rtj | } |j | | f|j | qfW|j} t| tj r| j dkrtd| jftj | } |jd| |jdd| ftj} tjt|| |j}|jj|} d|} |j| | }x"|D]\} } |j|| | |}q4Wt|||t|jj |dS)NFcss|]}t|tjVqdS)N)rVrr^)r9typr r r sz6VGenericEngine._loaded_gen_function..z*'%s' is used as result type, but is opaquerraz _cffi_f_%s)rVrrWrXrY_load_constantanyr\rar^r] PointerTyper_Zfldtypes TypeError _get_c_namerbrctupler_get_cached_btype load_function_make_struct_wrappersetattrrPr))r r;rGr/r0Z newfunctionZ indirectionsbase_tpZ indirect_argsrerhZindirect_resultBFuncrfr r r _loaded_gen_functions@             z#VGenericEngine._loaded_gen_functioncsP|jj|jj|dkr4|jfdd}nfdd}||_|S)Nracs j}|f||dS)Nr)new)r\res)BTyperoldfuncr r newfuncs z4VGenericEngine._make_struct_wrapper..newfunccs6|dj|f|dd}|S)Nr)Znewp)r\)ryr8rerzr r r{s.)rr1rpZ_cffi_base_type)r rzrer;rtr{r )ryr8rrerzr rrs z#VGenericEngine._make_struct_wrappercCs ||jkst|j|d|dS)Nstruct)rGrX_generate_struct_or_union_decl)r r;rGr r r _generate_gen_struct_declsz(VGenericEngine._generate_gen_struct_declcCs|j|d||dS)Nr|)_loading_struct_or_union)r r;rGr/r r r _loading_gen_structsz"VGenericEngine._loading_gen_structcKs|j|dS)N)_loaded_struct_or_union)r r;rGr/rr r r _loaded_gen_structsz!VGenericEngine._loaded_gen_structcCs ||jkst|j|d|dS)Nunion)rGrXr})r r;rGr r r _generate_gen_union_declsz'VGenericEngine._generate_gen_union_declcCs|j|d||dS)Nr)r)r r;rGr/r r r _loading_gen_unionsz!VGenericEngine._loading_gen_unioncKs|j|dS)N)r)r r;rGr/rr r r _loaded_gen_unionsz VGenericEngine._loaded_gen_unionc Cs|jdkrdSd||f}d||f}d||fj}|j}|d||f|d|d|dx|jD]\}} } } t| tjr| js| dkr|d |qny$|d | jd d || d |fWqnt k r} z|dt | WYdd} ~ XqnXqnW|d|j j ||d|f|d|d||d|d||dxt|jD]h\}} } } | dkrvq\|d||ft| tj r| jdkr|d| jn|d||fq\W|d|d|d|d|d|f|d|dS)Nz_cffi_check_%s_%sz_cffi_layout_%s_%sz%s %szstatic void %s(%s *p)rTz8 /* only to generate compile-time warnings or errors */z (void)p;rz (void)((p->%s) << 1);z { %s = &p->%s; (void)tmp; }z*tmpzfield %r)Zqualsz /* %s */rUzintptr_t %s(intptr_t i)z, struct _cffi_aligncheck { char x; %s y; };z static intptr_t nums[] = {z sizeof(%s),z) offsetof(struct _cffi_aligncheck, y),z offsetof(%s, %s),z 0, /* %s */z sizeof(((%s *)0)->%s),z -1z };z return nums[i];z3 /* the next line is not executed, but compiled */z %s(0);)fldnamesstripr enumfieldsrVr PrimitiveTypeis_integer_typerOrstrr r_ ArrayTypelengthrn) r r;r&rGZ checkfuncnamelayoutfuncnamecnamer%fnameftypefbitsizefqualrKr r r r}sT    &    z-VGenericEngine._generate_struct_or_union_declcCs |jdkrdSd||f}|jjdd}|j||}g}d} x(|| } | dkrRP|j| | d7} q@Wt|tjr|jr|d} |d} |ddd} |ddd}|j t | t |kot |jknst | || | f|_ nd||fj }||f|j|<dS)Nz_cffi_layout_%s_%szintptr_t(*)(intptr_t)rrr z%s %s)rr_typeof_lockedrqr_rVrr^partialZ force_flattenr[rXZ fixedlayoutrr )r r;r&rGr/rrufunctionlayoutZnumxZ totalsizeZtotalalignmentZfieldofsZ fieldsizerr r r r7s.     *z'VGenericEngine._loading_struct_or_unionc Cs|jdkrdS|jj|||jkrdd}|j}|j|}|jj|\}}||d|j|d||d|j|dd}x|jD]v\}} } } | dkrq||||j||d|f||ddkr|j| } |||d|j| d |f|d7}qW|t |kst dS) NcSs||krtd|||fdS)Nz'%s (we have %d, but C compiler says %d))r)Z realvalueZ expectedvaluemsgr r r checkZsz5VGenericEngine._loaded_struct_or_union..checkrzwrong total sizerzwrong total alignmentrzwrong offset for field %rzwrong size for field %r) rrrpr popsizeofZalignofrZoffsetofr[rX) r r;rrZBStructrrrerrrrZBFieldr r r rSs,        z&VGenericEngine._loaded_struct_or_unioncCs.t|tjr|j||dn|j|d|dS)Nr)rVrEnumType_generate_gen_enum_declr})r r;rGr r r _generate_gen_anonymous_declus z+VGenericEngine._generate_gen_anonymous_declcCs2t|tjr|j|||dn|j|d||dS)Nr)rVrr_loading_gen_enumr)r r;rGr/r r r _loading_gen_anonymous{s z%VGenericEngine._loading_gen_anonymouscKs.t|tjr |j|||f|n |j|dS)N)rVrr_loaded_gen_enumr)r r;rGr/rr r r _loaded_gen_anonymouss z$VGenericEngine._loaded_gen_anonymousNconstc CsD|j}d||f}|jj||dk rl|s.t|dks:t|d||d|j|||d|dn|r|dks|t|d||d|d|f|d |f|dn|dk st|dkst|d krd }nd }d } |dkot|tjrd } d }||jd| |f||d|d||f|d|dS)Nz _cffi_%s_%srzint %s(char *out_error)rTz return 0;rUzint %s(long long *out_value)z *out_value = (long long)(%s);z return (%s) <= 0;var&rzconst *z %s%s(void)z return (%s%s);) rr r_rX_check_int_constant_valuerVrr^rO) r is_intrGr;category check_valuer%funcnameZ ampersandZextrar r r rZs@           z"VGenericEngine._generate_gen_constcCs&t|tjo|j}|j|||dS)N)rVrrrrZ)r r;rGrr r r _generate_gen_constant_declsz*VGenericEngine._generate_gen_constant_declcCsd|}|dk r*|st|j|||}n|r|jjdd}|jjdd} |j| |} |jj|} | | } t| d}|dkr| r|jjdd} |dd|jj| >7}nd|dkstd}t|t j rd |}|jj|j ||d} |j| |} | }t|t j r|d}|S) Nz_cffi_const_%sz long long*rzint(*)(long long*)z long longrz (*)(void)rN) rX_load_known_int_constantrrrqrwintrrVrr^rO)r rr;rGr/rrvalueryrurpnegativeZ BLongLongZ fntypeextrar r r rjs2       zVGenericEngine._load_constantcCsDt|tjo|j}|j||||}t|||t|jj|dS)N) rVrrrrjrsrPr)r_)r r;rGr/r0rrr r r _loaded_gen_constants z#VGenericEngine._loaded_gen_constantcCs|j}|dkr"|d|||fn|d|||f|d|d||d||d|d||d |d |dd |f|d |d dS)Nrz& if ((%s) > 0 || (long)(%s) != %dL) {z1 if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {z char buf[64];z if ((%s) <= 0)z) sprintf(buf, "%%ld", (long)(%s));z elsez2 sprintf(buf, "%%lu", (unsigned long)(%s));z: sprintf(out_error, "%s has the real value %s, not %s",z "%s", buf, "%d");dz return -1;z })r)r rGrr%r r r rs   z(VGenericEngine._check_int_constant_valuecCsr|jjdd}|jjdd}|j||}|jj|d}||dkrn|jj|}tjdkrft|d}t|dS)Nzchar[]rz int(*)(char*)r zutf-8)r ) rrrqrwstringrr$rr)r r/rryrurrerrorr r r rs     z'VGenericEngine._load_known_int_constantcCs|jdd}d||fS)N$Z___D_z _cffi_e_%s_%s)replace)r r&rGr r r _enum_funcnames zVGenericEngine._enum_funcnameenumcCs|jr&x|jD]}|jd|qWdS|j||}|jj||j}|d||dx&t|j|jD]\}}|j ||qhW|d|d|dS)NTzint %s(char *out_error)rTz return 0;rU) r enumeratorsrZrr r_rr` enumvaluesr)r r;rGr& enumeratorrr% enumvaluer r r rs    z&VGenericEngine._generate_gen_enum_declcsLjr0fddjD}t|_d_nj||}j|dS)Ncsg|]}jd|qS)T)rj)r9r)r/r r;r r r<sz4VGenericEngine._loading_gen_enum..T)rrrorZpartial_resolvedrr)r r;rGr/r&rrr )r/r r;r rs   z VGenericEngine._loading_gen_enumcCs<x6t|j|jD]$\}}t|||t|jj|qWdS)N)r`rrrsrPr)r_)r r;rGr/r0rrr r r rs zVGenericEngine._loaded_gen_enumcCs&|dkrd}n|}|jd||ddS)Nz...T)r)rZ)r r;rGrr r r _generate_gen_macro_decl sz'VGenericEngine._generate_gen_macro_declcCsF|dkrd}n|}|jd||||d}t|||t|jj|dS)Nz...T)r)rjrsrPr)r_)r r;rGr/r0rrr r r _loaded_gen_macro)s  z VGenericEngine._loaded_gen_macrocCst|tjrx|jdkr\|j}d|f}|jj||d||d|d|f|dtj|j}|j d||ntj|}|j d||dd dS) Nz...z_cffi_sizeof_%szsize_t %s(void)rTz return sizeof(%s);rUFr)r) rVrrrrr r_rlitemrZ)r r;rGr%rtp_ptrr r r _generate_gen_variable_decl6s       z*VGenericEngine._generate_gen_variable_declcs^t|tjr|jdkrd|f}|jjdd}|j||}|}|jj|j} t ||jj | \} } | dkrt d||jf|j | }tj |j} |jd| ||} |jdk r|jj|}|jj|| } t||| t|jj|dSd|}|jj|jd|d}|j||}|fd d }fd d }tt||t||t|jj|dS) Nz...z_cffi_sizeof_%szsize_t(*)(void)rz/bad size: %r does not seem to be an array of %sFz _cffi_var_%sz *(*)(void)csdS)Nrr )r0)ptrr r gettergsz3VGenericEngine._loaded_gen_variable..gettercs |d<dS)Nrr )r0r)rr r setterisz3VGenericEngine._loaded_gen_variable..setter)rVrrrrrrqrprdivmodrrZresolve_lengthrlrjcastrsrPr)r_rOproperty)r r;rGr/r0rrursizeZ BItemTyperrestrrZBArrayrrr )rr _loaded_gen_variableHs:            z#VGenericEngine._loaded_gen_variable)r)r)NrN)N)r)r)5r,r-r.Z _class_keyZ_gen_python_modulerrrrrr(r3r?r"r4rLrMZ_generate_gen_typedef_declZ_loading_gen_typedefZ_loaded_gen_typedefrgZ_loading_gen_functionrvrrr~rrrrrr}rrrrrrZrZ_loading_gen_constantrjrrrrrrrrZ_loading_gen_macrorrZ_loading_gen_variablerr r r r r sf    -$1"  $      rad #include #include #include #include #include /* XXX for ssize_t on some platforms */ /* this block of #ifs should be kept exactly identical between c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py and cffi/_cffi_include.h */ #if defined(_MSC_VER) # include /* for alloca() */ # if _MSC_VER < 1600 /* MSVC < 2010 */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; typedef __int8 int_least8_t; typedef __int16 int_least16_t; typedef __int32 int_least32_t; typedef __int64 int_least64_t; typedef unsigned __int8 uint_least8_t; typedef unsigned __int16 uint_least16_t; typedef unsigned __int32 uint_least32_t; typedef unsigned __int64 uint_least64_t; typedef __int8 int_fast8_t; typedef __int16 int_fast16_t; typedef __int32 int_fast32_t; typedef __int64 int_fast64_t; typedef unsigned __int8 uint_fast8_t; typedef unsigned __int16 uint_fast16_t; typedef unsigned __int32 uint_fast32_t; typedef unsigned __int64 uint_fast64_t; typedef __int64 intmax_t; typedef unsigned __int64 uintmax_t; # else # include # endif # if _MSC_VER < 1800 /* MSVC < 2013 */ # ifndef __cplusplus typedef unsigned char _Bool; # endif # endif #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif #endif ) rrr5rrrrobjectrr!r r r r s  __pycache__/ffiplatform.cpython-36.opt-1.pyc000064400000006556152344566040014677 0ustar003 MZ@sddlZddlZddlmZdddddgZffd d Zdd d Zdd dZyddlm Z Wne k rvddZ YnXddZ ye e fZddlZWn ek re ZddlZYnXddZddZddZdS)N)VerificationErrorsourcesZ include_dirsZ library_dirsZ extra_objectsZdependscKsLtddlm}|g}x|D]}|jtjj|qW|f||d|S)Nr) Extension)namer)_hack_at_distutilsdistutils.corerappendospathnormpath)Z srcfilenamemodnamerkwdsrZ allsourcessrcr#/usr/lib64/python3.6/ffiplatform.py get_extensions   rc Csfttjj}zt||||}tjj|}Wdx.|jD]"\}}tjj||kr:|tj|<q:WX|S)z-Compile a C extension module using distutils.N) rr environcopy_buildr abspathitemsget)tmpdirextcompiler_verbosedebugZ saved_environZoutputfilenamekeyvaluerrrcompiles rc Csddlm}ddl}ddl}|d|gi}|j|jd}|dkrLtjj}d|f|d<d |d<d|f|d <d|f|d <yR|j j dpd}z.|j j ||j d|j d} | j\} Wd|j j |XWnD|jj|jjfk r} ztd | jj| fWYdd} ~ XnX| S) Nr) DistributionZ ext_modulesZ build_ext ffiplatformrTforceZ build_libZ build_tempz%s: %s)r!T)rr Zdistutils.errorsZ distutils.logZparse_config_filesZget_option_dictsysflagsrlogZ set_thresholdZ set_verbosityZ run_commandZget_command_objZ get_outputserrorsZ CompileErrorZ LinkErrorr __class____name__) rrrrr Z distutilsZdistZoptionsZ old_levelZcmd_objZsonameerrrr s.        &r)samefilecCstjj|tjj|kS)N)r r r)f1f2rrrr*Asr*c Cstjj|s|S|}g}xn|}tjj|\}}||ks<| r@|S|j|y$t|tjrl|jtjj|SWqt k rYqXqWdS)N) r r isabssplitr r*curdirreversejoinOSError)r dirnamesZprevdirrrrrmaybe_relative_pathDs    r5cCst|tr"|jdt||fnt|trrt|j}|jdt|x|D]}t||t|||qPWndt|tt fr|jdt|xB|D]}t||qWn*t|t r|jd|fnt d|fdS)Nz%ds%sz%ddz%dlz%diz7the keywords to verify() contains unsupported object %r) isinstancestrwritelendictsortedkeys_flattenlisttuple int_or_long TypeError)xfr<rrrrrr=_s        r=cCstj}t|||jS)N) cStringIOStringIOr=getvalue)rBrCrrrflattenrs rGc Cs0tjdkr,y ddl}Wntk r*YnXdS)NZwin32r)r#platform setuptools ImportError)rIrrrrws   r)rN)rN)r#r errorrZLIST_OF_FILE_NAMESrrrZos.pathr*rJr5intZlongr@rD NameErrorior=rGrrrrrs(     __pycache__/model.cpython-36.pyc000064400000046016152344566040012522 0ustar003 MZ0T@sddlZddlZddlmZddlmZmZmZdZdZ dZ ddZ Gd d d e Z Gd d d e ZGd ddeZeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZeeZdd ZeeZGd!d"d"eZGd#d$d$eZeed%dZGd&d'd'e ZGd(d)d)eZ Gd*d+d+e Z!Gd,d-d-e Z"Gd.d/d/eZ#dN) allocate_lock) CDefErrorVerificationErrorVerificationMissingcCs@|t@rd|j}|t@r(d|j}|t@r)r)r rrr__repr__AszBaseTypeByIdentity.__repr__csfddjDS)Ncsg|]}|t|fqSr)getattr).0name)r rr Esz1BaseTypeByIdentity._get_items..)_attrs_)r r)r r _get_itemsDszBaseTypeByIdentity._get_itemsN)rrr)F) __name__ __module__ __qualname__ is_array_typeis_raw_functionr#rr$r%r.r/r5rrrrrs  rc@s$eZdZddZddZddZdS)BaseTypecCs|j|jko|j|jkS)N) __class__r5)r otherrrr__eq__Js zBaseType.__eq__cCs ||k S)Nr)r r=rrr__ne__NszBaseType.__ne__cCst|jt|jfS)N)hashr<tupler5)r rrr__hash__QszBaseType.__hash__N)r6r7r8r>r?rBrrrrr;Hsr;c@s eZdZfZddZddZdS)VoidTypecCs d|_dS)Nzvoid&)r)r rrr__init__XszVoidType.__init__cCs t||dS)NZ new_void_type) global_cache)r r*r+rrrr([szVoidType.build_backend_typeN)r6r7r8r4rDr(rrrrrCUsrCc@seZdZddZdS)BasePrimitiveTypecCsdS)NFr)r rrris_complex_typebsz!BasePrimitiveType.is_complex_typeN)r6r7r8rGrrrrrFasrFc4@seZdZdZdddddddddddddddddddddddddddddddddddddddddddddddddddd3ZddZd d Zd d Zd dZddZ ddZ dS) PrimitiveTyper2cifj)3charZshortintlongz long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long longfloatZdoublez long doublezfloat _Complexzdouble _ComplexZ_BoolZwchar_tZchar16_tZchar32_tZint8_tZuint8_tZint16_tZuint16_tZint32_tZuint32_tZint64_tZuint64_tZ int_least8_tZ uint_least8_tZ int_least16_tZuint_least16_tZ int_least32_tZuint_least32_tZ int_least64_tZuint_least64_tZ int_fast8_tZ uint_fast8_tZ int_fast16_tZ uint_fast16_tZ int_fast32_tZ uint_fast32_tZ int_fast64_tZ uint_fast64_tZintptr_tZ uintptr_tZintmax_tZ uintmax_tZ ptrdiff_tZsize_tZssize_tcCs"||jkst||_|d|_dS)Nr)ALL_PRIMITIVE_TYPESrr2r)r r2rrrrDszPrimitiveType.__init__cCs|j|jdkS)NrI)rQr2)r rrr is_char_typeszPrimitiveType.is_char_typecCs|j|jdkS)NrJ)rQr2)r rrrr%szPrimitiveType.is_integer_typecCs|j|jdkS)NrK)rQr2)r rrr is_float_typeszPrimitiveType.is_float_typecCs|j|jdkS)NrL)rQr2)r rrrrGszPrimitiveType.is_complex_typecCst||d|jS)NZnew_primitive_type)rEr2)r r*r+rrrr(sz PrimitiveType.build_backend_typeN)r2) r6r7r8r4rQrDrRr%rSrGr(rrrrrHfstrHc@s(eZdZd ZddZddZddZdS) UnknownIntegerTyper2cCs||_|d|_dS)Nr)r2r)r r2rrrrDszUnknownIntegerType.__init__cCsdS)NTr)r rrrr%sz"UnknownIntegerType.is_integer_typecCstd|jdS)Nz4integer type '%s' can only be used after compilation)NotImplementedErrorr2)r r*r+rrrr(sz%UnknownIntegerType.build_backend_typeN)r2)r6r7r8r4rDr%r(rrrrrTsrTc@s eZdZdZddZddZdS)UnknownFloatTyper2cCs||_|d|_dS)Nr)r2r)r r2rrrrDszUnknownFloatType.__init__cCstd|jdS)Nz2float type '%s' can only be used after compilation)rUr2)r r*r+rrrr(sz#UnknownFloatType.build_backend_typeN)r2)r6r7r8r4rDr(rrrrrVsrVc@seZdZdZd ddZdS) BaseFunctionTypeargsr"ellipsisabiNcCs||_||_||_||_dd|jD}|jr8|jd|p@dg}|jdj|f}|dk r||dd|d|dd}|jjjd||_dS) NcSsg|] }|jqSr)r)r1argrrrr3sz-BaseFunctionType.__init__..z...voidz, rrr) rXr"rYrZappend _base_patternjoinrr)r rXr"rYrZZreprargsrrrrrDs   zBaseFunctionType.__init__)rXr"rYrZ)N)r6r7r8r4rDrrrrrWsrWc@s$eZdZdZdZddZddZdS)RawFunctionTypez(&)(%s)TcCstd|fdS)NzPcannot render the type %r: it is a function type, not a pointer-to-function type)r)r r*r+rrrr(sz"RawFunctionType.build_backend_typecCst|j|j|j|jS)N)FunctionPtrTyperXr"rYrZ)r rrras_function_pointersz#RawFunctionType.as_function_pointerN)r6r7r8r^r:r(rbrrrrr`sr`c@s eZdZdZddZddZdS)raz(*&)(%s)cCs|jj||}g}x |jD]}|j|j||qWf}|jdkrl|jsly|jjf}Wntk rjYnXt ||dt |||jf|S)NZ __stdcallZnew_function_type) r"r.rXr]rZrY_backendZ FFI_STDCALLAttributeErrorrErA)r r*r+r"rXtpZabi_argsrrrr(s  z"FunctionPtrType.build_backend_typecCst|j|j|j|jS)N)r`rXr"rYrZ)r rrras_raw_functionszFunctionPtrType.as_raw_functionN)r6r7r8r^r(rfrrrrrasrac@s"eZdZd Zd ddZddZdS) PointerTypetotyper rcCs>||_||_t|d}|jr*d|jf}|jjd||_dS)Nz *&z(%s)r)rhr rr9r rr)r rhr ZextrarrrrDs  zPointerType.__init__cCs |jj||dd}t||d|S)NT)r,new_pointer_type)rhr.rE)r r*r+ZBItemrrrr(szPointerType.build_backend_typeN)rhr )r)r6r7r8r4rDr(rrrrrgs rgcCs t|tS)N)rgr )rhrrrConstPointerTypesrjc@seZdZdZdddZdS) NamedPointerTyperhr2rcCs"tj|||||_|d|_dS)Nr)rgrDr2r)r rhr2r rrrrDszNamedPointerType.__init__N)rhr2)r)r6r7r8r4rDrrrrrksrkc@s,eZdZd ZdZddZddZdd Zd S) ArrayTypeitemlengthTcCsF||_||_|dkrd}n|dkr(d}nd|}|jjjd||_dS)Nz&[]z...z &[/*...*/]z&[%s]r)rmrnrr)r rmrnZbracketsrrrrD)szArrayType.__init__cCs t|j|S)N)rlrm)r Z newlengthrrrresolve_length6szArrayType.resolve_lengthcCsJ|jdkrtd|f|jj||t|jj||}t||d||jS)Nz...z)cannot render the type %r: unknown lengthZnew_array_type)rnrrmr.rgrE)r r*r+ZBPtrItemrrrr(9s   zArrayType.build_backend_typeN)rmrn)r6r7r8r4r9rDror(rrrrrl%s  rlrMc@s,eZdZd ZdZddZddZddZdS) StructOrUnionOrEnumr2NcCs$|jpd|j|jf}|d|_dS)Nz%s %sr) forcenamekindr2r)r r2rrrbuild_c_name_with_markerHsz,StructOrUnionOrEnum.build_c_name_with_markercCs||_|jdS)N)rqrs)r rqrrrforce_the_nameLsz"StructOrUnionOrEnum.force_the_namecCs|jjdst|jddS)Nrr)rendswithr)r rrrget_official_namePsz%StructOrUnionOrEnum.get_official_name)r2)r6r7r8r4rqrsrtrwrrrrrpDs rpc@sjeZdZdZdZdZdZdddZddZdd d Z d d Z dd dZ ddZ ddZ ddZddZdS) StructOrUnionNrFcCs*||_||_||_||_||_|jdS)N)r2fldnamesfldtypes fldbitsizefldqualsrs)r r2ryrzr{r|rrrrD[s zStructOrUnion.__init__ccsB|jdk r>x2t|j|jD] \}}|dkrt|tr|VqWdS)Nr)rzzipry isinstancerx)r r2typerrranonymous_struct_fieldscs z%StructOrUnion.anonymous_struct_fieldsTccs|j}|dkrdt|j}xbt|j|j|j|D]J\}}}}|dkrnt|trn|rnx&|jD] }|Vq^Wq2||||fVq2WdS)Nrr)r) r|lenryr}rzr{r~rx enumfields)r Zexpand_anonymous_struct_unionr|r2rbitsizer r"rrrris  zStructOrUnion.enumfieldsc Cs~g}g}g}g}x@|jD]4\}}}}|j||j||j||j|qWt||_t||_t||_t||_dS)N)rr]rAryrzr{r|) r namestypesZbitsizesr|r2rrr rrr force_flattenws      zStructOrUnion.force_flattencCs$tj||||}|s |j|||S)N)rpr.finish_backend_type)r r*r+r,r-rrrr.s   zStructOrUnion.get_cached_btypecs|jr$|jdkr td|jfdSj|}d|_|jdkrBn|jdkrfdd|jD}tt|j||j }d}|j rd}j j |||d d |ntg}|j\}}} } x4t t|jD] } || } |j| } t| to| jdkrn| jj}t| j|\}}|dkr<|jd |j|j| p4d f| j|} |jd| | f|j| dd|_| j}t| tr| jdkr| dkstn8j|}|| kr|jd |j|j| pd || f|j|qWtt|j||j |}j j |||| | d|_dS)Nrz(recursive structure declaration for '%s'rcsg|]}|jqSr)r.)r1re)r*r+rrr3sz5StructOrUnion.finish_backend_type..rz...zfield '%s.%s' has a bogus size?z{}z=field '%s.%s' is declared as %d bytes, but is really %d bytesruru) completedrUr2r&rz fixedlayoutlistr}ryr{packedrcZcomplete_struct_or_unionrangerr~rlrnrmr.divmodsizeof_verification_errorrorr])r r*r+r-rzZlstZsflagsZfieldofsZ fieldsizeZ totalsizeZtotalalignmentrJZfsizeZftypeZ BItemTypeZnlenZnrestZ BFieldTypeZ bitemsizer)r*r+rrs`                z!StructOrUnion.finish_backend_typecCs t|dS)N)r)r msgrrrrsz!StructOrUnion._verification_errorcCs |jr|jdkrt|jdS)N)partialrrr)r rrrcheck_not_partialszStructOrUnion.check_not_partialcCs.|j|j|t||d|j|j|dS)Nz new_%s_type)key)rr]rErrrw)r r*r+rrrr(s z StructOrUnion.build_backend_type)N)T)F)r6r7r8rrrrrDrrrr.rrrr(rrrrrxUs   <rxc@seZdZdZdS) StructTypestructN)r6r7r8rrrrrrrsrc@seZdZdZdS) UnionTypeunionN)r6r7r8rrrrrrrsrc@sBeZdZdZdZdZdddZddZdd Zd d Z d d Z dS)EnumTypeenumFNcCs$||_||_||_||_|jdS)N)r2 enumerators enumvalues baseinttypers)r r2rrrrrrrDs zEnumType.__init__cCs4tj|||jdkr0|j}d|jdd|_dS)Nrr_)rprtrqrwr)r rqr2rrrrts  zEnumType.force_the_namecCs|jr|j rt|jdS)N)rpartial_resolvedrr)r rrrrszEnumType.check_not_partialc Cs4|j|j||}t||d|j|j|j||dS)NZ new_enum_type)r)rbuild_baseinttyperErwrr)r r*r+Z base_btyperrrr(s  zEnumType.build_backend_typec CsH|jdk r|jj||S|jr4t|j}t|j}nDddl}y tjWntk r\YnX|j d|j d}}|dkrd}t d}t d}nd}t d}t d}|j||} |j||} |j | } |j | } |d d| d>ko|dd| |>kr| S|d d| d>kr4|dd| |>kr4| St d |j dS) NrzU%r has no values explicitly defined; guessing that it is equivalent to 'unsigned int'rrNrOz unsigned intz unsigned longrz=%s values don't all fit into either 'long' or 'unsigned long'ruru)rr.rminmaxwarningsZ__warningregistry__clear NameErrorwarnrrHrr) r r*r+Zsmallest_valueZ largest_valuerZsignZ candidate1Z candidate2Zbtype1Zbtype2Zsize1Zsize2rrrrs@          zEnumType.build_baseinttype)N) r6r7r8rrrrrDrtrr(rrrrrrs rcCs2|dkrd|}t|ddd}|j|d|_|S)Nz$%s unknown_type)rrtorigin)r2 structnamererrrr*s  rcCs(|dkrd|}t|ddd}t||S)Nz$$%s)rrk)r2rrerrrunknown_ptr_type2src CsFt|tjrtSt(tt|ds2tjt|_ t|j SQRXdS)N __typecache) r~r ModuleType_typecache_cffi_backend global_lockhasattrrweakrefWeakValueDictionaryr)Zbackendrrr get_typecache<s  rc Os|jd||f}| sty |j|Stk r8YnXyt|j||}Wn4tk r}ztd|||fWYdd}~XnX|j}t(|j|} | dkr|||<|S| SWdQRXdS)Nrz %s: %r: %s) poprZ _typecacher'r0rcrUrget) Zsrctyper*funcnamerXkwdsrresecacheZres1rrrrEGs"  $ rEcCstd|d|S)N?ri)rE)r*r-rrr pointer_cache^srcCs@|jrsN  /  L  G   __pycache__/api.cpython-36.opt-1.pyc000064400000077716152344566040013145 0ustar003 '+e@sddlZddlZddlmZddlmZddlmZyeWn(e k rdddl m Z ddZYnXye Wne k re Z YnXGd d d eZd d Zd dZddZdS)N) allocate_lock) CDefError)model)CallablecCs t|tS)N) isinstancer)xr /usr/lib64/python3.6/api.py sr c@seZdZdZdfddZdgddZdhdd Zdid d Zdjd dZddZ ddZ dkddZ ddZ ddZ ddZddZdlddZdmd d!Zd"d#Zdod%d&Zd'd(Zdpd)d*Zd+d,Zdqd-d.Zdrd0d1Zdsd2d3Zd4d5Zdtd6d7Zd8d9Zd:d;Zeeedd<Zdvd=d>Z d?d@Z!dAdBZ"dCdDZ#dEdFZ$dGdHZ%dIdJZ&dKdLZ'dMdNZ(dOdPZ)dwdRdSZ*dxdUdVZ+dWdXZ,dYdZZ-dyd\d]Z.d^d_Z/d`daZ0dbdcZ1dddeZ2dS)zFFIa The main top-level class that you instantiate once, or once per module. Example usage: ffi = FFI() ffi.cdef(""" int printf(const char *, ...); """) C = ffi.dlopen(None) # standard library -or- C = ffi.verify() # use a C compiler: verify the decl above is right C.printf("hello, %s!\n", ffi.new("char[]", "world")) Nc Cs|dkr^ddl}ddlm}|j|kr^t|drJtd|t|j|jfntd|t|jfddlm}||_t|_ |j |_ i|_ t jd j|_t jd j|_g|_g|_g|_g|_d|_i|_d|_d|_tj||_t|d r|j|x2t|jD]$}|jd rt ||t!||qW|j "|j"tj#|_$|j"tj%|_&WdQRXt'|t jrtt(d s|j)|j$dt(_*|j+\t(_,t(_-n |j)|j$d|_*|j+\|_,|_-|j.|_.dS)z{Create an FFI instance. The 'backend' argument is used to select a non-default backend, mostly for tests. Nrr) __version____file__zVersion mismatch: this is the 'cffi' package version %s, located in %r. When we import the top-level '_cffi_backend' extension module, we get version %s, located in %r. The two versions should be equal; check your installation.zVersion mismatch: this is the 'cffi' package version %s, located in %r. This interpreter comes with a built-in '_cffi_backend' module, which is version %s. The two versions should be equal; check your installation.)cparserZ parsed_typesZ new_typesset_ffiZRTLD_NULL)/Z _cffi_backendr hasattr Exceptionrr_backendr_lockZParser_parser_cached_btypestypes ModuleType__dict__ _parsed_typesZ _new_types_function_caches _libraries _cdefsources_included_ffis_windows_unicode_init_once_cache _cdef_version _embeddingrZ get_typecacheZ _typecacherlist startswithsetattrgetattr_get_cached_btypeZ voidp_typeBVoidPZchar_array_typeBCharArr castrZ _get_typesCDataZCTypebuffer)selfbackendr rnamer r r __init__'sR          z FFI.__init__FcCs|j|||ddS)aParse the given C source. This registers all declared functions, types, and global variables. The functions and global variables can then be accessed via either 'ffi.dlopen()' or 'ffi.verify()'. The types can be used in 'ffi.new()' and other functions. If 'packed' is specified as True, all structs declared inside this cdef are packed, i.e. laid out without any field alignment at all. )overridepackedN)_cdef)r/csourcer3r4r r r cdefcszFFI.cdefcCs$|j||dd|jdkr d|_dS)NT)r4Z dllexportr)r5r$)r/r6r4r r r embedding_apims zFFI.embedding_apic Kst|ts&t|tstd|jd}|j|t|_|jj |fd|i||j j ||rvx|j D] }|j qfW|jj}|rg|j_x|D]}|j||qWWdQRXdS)Nz cdef() argument must be a stringasciir3)rstr basestring TypeErrorencoderobjectr#rparserappendrclearZ _recompletefinish_backend_type)r/r6r3Zoptionscache finishlisttpr r r r5rs        z FFI._cdefrc Cs>|j.t|||\}}|jj||jj|WdQRX|S)aHLoad and return a dynamic library identified by 'name'. The standard C library can be loaded by passing None. Note that functions and types declared by 'ffi.cdef()' are not linked to a particular library, just like C headers; in the library we only look for the actual (untyped) symbols. N)r_make_ffi_libraryrr@r)r/r1flagslibZfunction_cacher r r dlopens  z FFI.dlopencCst|j|dS)zClose a library obtained with ffi.dlopen(). After this call, access to functions or variables from the library will fail (possibly with a segmentation fault). N)type__cffi_close__)r/rHr r r dlclosesz FFI.dlclosecCsj|}||jkr|j|St|ts,|jd}|jj|}|j}|rJ|j}|j|}||f}||j|<|S)Nr9) rrr:r=rZ parse_typeZis_raw_functionZas_function_pointerr))r/cdeclkeyrJreally_a_function_typebtyperesultr r r _typeof_lockeds       zFFI._typeof_lockedcCsdy|j|}Wn0tk r>|j|j|}WdQRXYnX|\}}|r`| r`td|f|S)Nz>the type %r is a function type, not a pointer-to-function type)rKeyErrorrrRr)r/rMconsider_function_as_funcptrrQrPrOr r r _typeofs  z FFI._typeofc Cst|tr|j|St||jr,|jj|St|tjrLt|}|dk rL|St|tj r|t |dr||j |j |j SQRXtt|dS)zParse the C type given as a string and return the corresponding object. It can also be used on 'cdata' instance to get its C type. N_cffi_base_type)rr;rUr-rtypeofrBuiltinFunctionType_builtin_function_type FunctionTyperrr)rVr<rJ)r/rMresr r r rWs       z FFI.typeofcCs0t|tr |j|}|jj|S|jj|SdS)zvReturn the size in bytes of the argument. It can be a string naming a C type, or a 'cdata' instance. N)rr;rUrsizeof)r/rMBTyper r r r\s   z FFI.sizeofcCs t|tr|j|}|jj|S)z\Return the natural alignment size in bytes of the C type given as a string. )rr;rUralignof)r/rMr r r r^s  z FFI.alignofcGs(t|tr|j|}|j|f|dS)a3Return the offset of the named field inside the given structure or array, which must be given as a C type name. You can give several field names in case of nested structures. You can also give numeric values which correspond to array items, in case of an array type. r)rr;rU _typeoffsetof)r/rMfields_or_indexesr r r offsetofs  z FFI.offsetofcCs"t|tr|j|}|jj||S)aAllocate an instance according to the specified C type and return a pointer to it. The specified C type must be either a pointer or an array: ``new('X *')`` allocates an X and returns a pointer to it, whereas ``new('X[n]')`` allocates an array of n X'es and returns an array referencing it (which works mostly like a pointer, like in C). You can also use ``new('X[]', n)`` to allocate an array of a non-constant length n. The memory is initialized following the rules of declaring a global variable in C: by default it is zero-initialized, but an explicit initializer can be given which can be used to fill all or part of the memory. When the returned object goes out of scope, the memory is freed. In other words the returned object has ownership of the value of type 'cdecl' that it points to. This means that the raw data can be used as long as this object is kept alive, but must not be used for a longer time. Be careful about that when copying the pointer to the memory somewhere else, e.g. into another structure. )rr;rUrZnewp)r/rMinitr r r news  zFFI.newTcs,jj}|j|||dfdd }|S)aReturn a new allocator, i.e. a function that behaves like ffi.new() but uses the provided low-level 'alloc' and 'free' functions. 'alloc' is called with the size as argument. If it returns NULL, a MemoryError is raised. 'free' is called with the result of 'alloc' as argument. Both can be either Python function or directly C functions. If 'free' is None, then no free function is called. If both 'alloc' and 'free' are None, the default is used. If 'should_clear_after_alloc' is set to False, then the memory returned by 'alloc' is assumed to be already cleared (or you are fine with garbage); otherwise CFFI will clear it. Ncst|trj|}||S)N)rr;rU)rMrb) allocatorr/r r allocates  z#FFI.new_allocator..allocate)N)rr new_allocator)r/ZallocZfreeZshould_clear_after_allocZ compiled_ffirer )rdr/r rfs  zFFI.new_allocatorcCs"t|tr|j|}|jj||S)zSimilar to a C cast: returns an instance of the named C type initialized with the given 'source'. The source is casted between integers or pointers of any type. )rr;rUrr,)r/rMsourcer r r r,s  zFFI.castrcCs|jj||S)aReturn a Python string (or unicode string) from the 'cdata'. If 'cdata' is a pointer or array of characters or bytes, returns the null-terminated string. The returned string extends until the first null character, or at most 'maxlen' characters. If 'cdata' is an array then 'maxlen' defaults to its length. If 'cdata' is a pointer or array of wchar_t, returns a unicode string following the same rules. If 'cdata' is a single character or byte or a wchar_t, returns it as a string or unicode string. If 'cdata' is an enum, returns the value of the enumerator as a string, or 'NUMBER' if the value is out of range. )rstring)r/cdatamaxlenr r r rh%sz FFI.stringcCs|jj||S)aLUnpack an array of C data of the given length, returning a Python string/unicode/list. If 'cdata' is a pointer to 'char', returns a byte string. It does not stop at the first null. This is equivalent to: ffi.buffer(cdata, length)[:] If 'cdata' is a pointer to 'wchar_t', returns a unicode string. 'length' is measured in wchar_t's; it is not the size in bytes. If 'cdata' is a pointer to anything else, returns a list of 'length' items. This is a faster equivalent to: [cdata[i] for i in range(length)] )runpack)r/riZlengthr r r rk7sz FFI.unpackcCs|jj|j||S)aReturn a that points to the data of the given Python object, which must support the buffer interface. Note that this is not meant to be used on the built-in types str or unicode (you can build 'char[]' arrays explicitly) but only on objects containing large quantities of raw data in some other format, like 'array.array' or numpy arrays. )r from_bufferr+)r/Z python_bufferZrequire_writabler r r rlUs zFFI.from_buffercCs|jj|||S)a_ffi.memmove(dest, src, n) copies n bytes of memory from src to dest. Like the C function memmove(), the memory areas may overlap; apart from that it behaves like the C function memcpy(). 'src' can be any cdata ptr or array, or any Python buffer object. 'dest' can be any cdata ptr or array, or a writable Python buffer object. The size to copy, 'n', is always measured in bytes. Unlike other methods, this one supports all Python buffer including byte strings and bytearrays---but it still does not support non-contiguous buffers. )rmemmove)r/destsrcnr r r rm`sz FFI.memmovecsBfdd}ttr*jdd|dkr6|S||SdS)aReturn a callback object or a decorator making such a callback object. 'cdecl' must name a C function pointer type. The callback invokes the specified 'python_callable' (which may be provided either directly or via a decorator). Important: the callback object must be manually kept alive for as long as the callback may be invoked from the C level. cs"t|stdjj|S)Nz.the 'python_callable' argument is not callable)callabler<rcallback)python_callable)rMerroronerrorr/r r callback_decorator_wrapxs z-FFI.callback..callback_decorator_wrapT)rTN)rr;rU)r/rMrsrtrurvr )rMrtrur/r rrps  z FFI.callbackrcCsht|tr|j|}|j}|jdrBd|jj|dkrBd|}n|rZ|ddkrZd|}|jj||S)a.Return a string giving the C type 'cdecl', which may be itself a string or a object. If 'replace_with' is given, it gives extra text to append (or insert for more complicated C types), like a variable name, or '*' to get actually the C type 'pointer-to-cdecl'. *z&[&z(%s)rz[( )rr;rUstripr&rZgetcname)r/rMZ replace_withr r r getctypes    z FFI.getctypecCs|jj|||S)aReturn a new cdata object that points to the same data. Later, when this new cdata object is garbage-collected, 'destructor(old_cdata_object)' will be called. The optional 'size' gives an estimate of the size, used to trigger the garbage collection more eagerly. So far only used on PyPy. It tells the GC that the returned object keeps alive roughly 'size' bytes of external memory. )rZgcp)r/riZ destructorsizer r r gcs zFFI.gcc CsRy|j|}Wn>tk rLg}|j||}x|D]}|j||q4WYnX|S)N)rrSZget_cached_btyperB)r/rJr]rDr r r r)s  zFFI._get_cached_btypecKsVddlm}m}|jr |j||p(|}||||f||_|jj}|jj||S)aVerify that the current ffi signatures compile on this machine, and return a dynamic library object. The dynamic library can be used to call functions and access global variables declared in this 'ffi'. The library is compiled by the C compiler: it gives you C-level API compatibility (including calling macros). This is unlike 'ffi.dlopen()', which requires binary compatibility in the signatures. r)Verifier_caller_dir_pycache)verifierr~rr!_apply_windows_unicode load_libraryrr@)r/rgtmpdirkwargsr~rrHr r r verifys     z FFI.verifycCs |jjS)N)rZ get_errno)r/r r r _get_errnoszFFI._get_errnocCs|jj|dS)N)rZ set_errno)r/errnor r r _set_errnoszFFI._set_errnoz(the value of 'errno' from/to the C callscCs |jj|S)N)r getwinerror)r/coder r r rszFFI.getwinerrorc Cs|jtj||SQRXdS)N)rr pointer_cache)r/ctyper r r _pointer_toszFFI._pointer_toc Gsy|jj|}Wn8tk rHdt|jkrBt|j|f|SYnX|rd|j|f|\}}n|jdkrvtdd}|j|}|jj |||S)zReturn the address of a . If 'fields_or_indexes' are given, returns the address of that field or array item in the structure or array, recursively in case of nested structures. __addressof__Zpointerzaddressof(pointer)r) rrWr<rJrrr_kindrZ rawaddressof)r/rir`roffsetZctypeptrr r r addressofs  z FFI.addressofcGsD|jj||\}}x(|D] }|jj||d\}}||7}qW||fS)Nr)rZ typeoffsetof)r/rZfield_or_indexr`rZfield1Zoffset1r r r r_s   zFFI._typeoffsetofcCst|tstdt|jf||kr.td|jX|jF|jj|j|j j d|j j |j |j j d|j j |WdQRXWdQRXdS)aIncludes the typedefs, structs, unions and enums defined in another FFI instance. Usage is similar to a #include in C, where a part of the program might include types defined in another part for its own usage. Note that the include() method has no effect on functions, constants and global variables, which must anyway be accessed directly from the lib object returned by the original FFI instance. zGffi.include() expects an argument that is also of type cffi.FFI, not %rzself.include(self)[]N) rr r<rJ__name__ ValueErrorrrincluderr@extendr )r/Zffi_to_includer r r rs   z FFI.includecCs|jj|j|S)N)rZ newp_handler*)r/rr r r new_handle szFFI.new_handlecCs |jj|S)N)r from_handle)r/rr r r rszFFI.from_handlecCs>|jdk rtdt|}|r*|jdn |jd||_dS)acWindows: if 'enabled_flag' is True, enable the UNICODE and _UNICODE defines in C, and declare the types like TCHAR and LPTCSTR to be (pointers to) wchar_t. If 'enabled_flag' is False, declare these types to be (pointers to) plain 8-bit characters. This is mostly for backward compatibility; you usually want True. Nz%set_unicode() can only be called onceztypedef wchar_t TBYTE;typedef wchar_t TCHAR;typedef const wchar_t *LPCTSTR;typedef const wchar_t *PCTSTR;typedef wchar_t *LPTSTR;typedef wchar_t *PTSTR;typedef TBYTE *PTBYTE;typedef TCHAR *PTCHAR;ztypedef char TBYTE;typedef char TCHAR;typedef const char *LPCTSTR;typedef const char *PCTSTR;typedef char *LPTSTR;typedef char *PTSTR;typedef TBYTE *PTBYTE;typedef TCHAR *PTCHAR;)r!rboolr7)r/Z enabled_flagr r r set_unicodes  zFFI.set_unicodecCs>|jdf}t|ttfs"tdt|ddg}||d<dS)NZ define_macrosz''define_macros' must be a list or tupleUNICODE1_UNICODE)rr)rr)getrr%tupler<)r/kwdsZ defmacrosr r r r0s  zFFI._apply_windows_unicodec sjfdd}dtjkrddl}tjdkrNd}ttdr|d|jjtjd n4tjdkr^d }nd }ttdr|d|jjtjd ttdr|d|jjtjddntjdkrd}ttdr|d7}nLy ddl }Wn t k rddl m }YnXd}|j dr||j d7}|tj d?tj d?d@f}ttdrF|tj7}|d|tjdkrf|dddS)Ncs"j|g}||kr|j|dS)N) setdefaultr@)rNvalueZlst)rr r ensure:s z(FFI._apply_embedding_fix..ensureZ__pypy__rwin32Zpython27prefixZ library_dirsZlibszpypy-czpypy3-cbinZpypyZgoalz python%d%dgettotalrefcountZ_d) sysconfigz python%d.%dZ DEBUG_EXTabiflagsZ librariesZextra_link_argsz /MANIFEST)r)sysbuiltin_module_namesosplatformrpathjoinr version_infor ImportErrorZ distutilsget_config_var hexversionr)r/rrrZ pythonlibtemplaterr )rr _apply_embedding_fix8s@                zFFI._apply_embedding_fix.ccKsdddl}t|drtdt|ts,td|j|ksF|jrN|j|krNtdt||||f|_ dS)Nr_assigned_sourcez:set_source() cannot be called several times per ffi objectz'module_name' must be a stringzY'module_name' must not contain '/': use a dotted name to make a 'package.module' location) rrrrr;r<sepaltsepr:r)r/ module_namergsource_extensionrrr r r set_sourcejs  zFFI.set_sourcebuildc Csddlm}ddlm}t|ds>t|dr6|jjStd|j\}}}}|dkr\t d||||||f|||d d |\} } |r| rt j j d | j dfnt j j d | j df| S) Nr)mkpathr) recompilerrz8set_source() must be called before distutils_extension()zadistutils_extension() is only for C extension modules, not for dlopen()-style pure Python modulesF)rZextradirrcall_c_compilerzregenerated: %r znot modified: %r )Zdistutils.dir_utilr recompilerrrrZ get_extensionrrr<rstderrwriteZsources) r/rverboserrrrgrrZextupdatedr r r distutils_extensionws&     zFFI.distutils_extensioncCsZddlm}t|dstd|j\}}}}|dkrsz+FFI.embedding_init_code..Z cffi_initexec) r$rrematchend splitlinesgrouprangelenrstripr&rr)r/Zpysourcerrlinesrrr )rr embedding_init_codes$    zFFI.embedding_init_codecOs tddS)Nz:ffi.def_extern() is only available on API-mode FFI objects)r)r/argsrr r r def_externszFFI.def_externcCsg}g}g}xh|jjD]\}|jdr8|j|ddq|jdrV|j|ddq|jdr|j|ddqW|j|j|j|||fS)zReturns the user type names known to this FFI instance. This returns a tuple containing three lists of names: (typedef_names, names_of_structs, names_of_unions) ztypedef Nzstruct zunion )r _declarationsr&r@sort)r/ZtypedefsZstructsZunionsrNr r r list_typess   zFFI.list_types)N)FF)F)F)r)F)N)NNTr)r)F)NNN)r)r)rNr)r)r)rT)rrNN)3r __module__ __qualname____doc__r2r7r8r5rIrLrRrUrWr\r^rarcrfr,rhrkrlrmrrr{r}r)rrrpropertyrrrrr_rrrrrrrrrrrrrrrr r r r r sb <              2   r c Csddl}|dkr*tjdkr&|jd|Sd}d}d|ksHd|ksH|j|kr|y |j||Stk rz}z |}WYdd}~XnXddl}|jj|}|dkr|dkrtjdkrtj d krtdd|f}|dk rd ||f}t||j||S) Nrrcr/rzXdlopen(None) cannot work on Windows for Python 3 (see http://bugs.python.org/issue23606)zGctypes.util.find_library() did not manage to locate a library called %rz%s. Additionally, %s)r) rrrrrOSErrorZ ctypes.utilutilZ find_libraryr) r0r1rGrZ first_erroreZctypesrmsgr r r _load_backend_libs,     rc s j}t||| fdd fdd fdddd fd d id gi fd d   fdd G fdddt|dk ry"t|ts|jd}d|_Wntk rYnX jfS)Ncs<d|}jj|\}}j|}j||}|j|<dS)Nz function )rrr) load_functionr)r1rNrE_r]r) backendlibffilibraryr r accessor_function%s   z,_make_ffi_library..accessor_functioncs\d}jj|\}}j|jjttfddfdddS)Nz variable cs S)Nr )r/)r]r1 read_variabler r r 3sz>_make_ffi_library..accessor_variable..cs |S)Nr )r/r)r]r1write_variabler r r 4s)rrr)rrr'r)r1rNrEr) FFILibraryrr)r]r1rrr accessor_variable,s z,_make_ffi_library..accessor_variablecsy|Stk rjZ|krtd|}jj|\}}j|}|jdkr`tj|}j||}||<WdQRX|SXdS)Nz variable Zarray) rSrrrr)rrrr)r1rNrErr]p)addr_variablesrrr r addressof_var6s    z(_make_ffi_library..addressof_varcSstd|fdS)NzDnon-integer constant '%s' cannot be accessed from a dlopen() library)NotImplementedError)r1r r r accessor_constantEsz,_make_ffi_library..accessor_constantcsjj|j|<dS)N)r_int_constantsr)r1)rrr r accessor_int_constantIsz0_make_ffi_library..accessor_int_constantFcsdjkrdSxjjjD]\}\}}t|tjs~|jdd\}}|dkrZ|<q|dkrl|<q|dkr|<q x0t|j D]"\}}||ffdd }||<qWq Wxjj D]}j |qWjd<dS) NrryrZfunctionZvariableZconstantcs|j|j|j|<dS)N)Zcheck_not_partialZ enumvaluesr)r1rEr)rr r accessor_enum_szB_make_ffi_library..update_accessors..accessor_enum) r#rritemsrrZEnumTypesplit enumerateZ enumeratorsr r)rNrErrr1rZenumnamer )r rr r accessorsaccessors_versionrrr r update_accessorsPs"    z+_make_ffi_library..update_accessorsc sXjH|jks|jkr dS|kr>|kr>t|||WdQRXdS)N)rrAttributeError)r1)rrrrrr r make_accessorgsz(_make_ffi_library..make_accessorcsReZdZfddZfddZfddZfddZfd d Zd S) z%_make_ffi_library..FFILibrarycs|t||S)N)r()r/r1)rr r __getattr__rsz1_make_ffi_library..FFILibrary.__getattr__c sJyt|j|}Wn(tk r8|t|||YnX|j||dS)N)r( __class__rr'__set__)r/r1rr)rr r __setattr__us z1_make_ffi_library..FFILibrary.__setattr__c s jjSQRXdS)N)rkeys)r/)rrrr r __dir__}sz-_make_ffi_library..FFILibrary.__dir__csf|jkrj|S|jkr&|S||jkrBj|S|jkrT|Std|fdS)Nz:cffi library has no function or global variable named '%s')rr)r/r1)rrrrr r rs      z3_make_ffi_library..FFILibrary.__addressof__csj|jjdS)N)Z close_librrA)r/)rr r rKsz4_make_ffi_library..FFILibrary.__cffi_close__N)rrrrrrrrKr )rrrrrrrrr r rqs    rzutf-8z FFILibrary_%s) rrr>rr:r=r UnicodeErrorr)rZlibnamerGr0r )rr rr rrrrrrrrrrr rF!s,   "   rFcCsdddl}y$|j|j}|j}|j}||}Wntttfk rFdSX|j |j |SQRXdS)Nr) rmodulesrZ_cffi_original_ffiZ_cffi_types_of_builtin_funcsrSrr<rr))rrmodulerZtypes_of_builtin_funcsrEr r r rYs  rY)rrlockrrtrrrrq NameError collectionsrr;r:r>r rrFrYr r r r s*     wz__pycache__/api.cpython-36.pyc000064400000100052152344566040012162 0ustar003 '+e@sddlZddlZddlmZddlmZddlmZyeWn(e k rdddl m Z ddZYnXye Wne k re Z YnXGd d d eZd d Zd dZddZdS)N) allocate_lock) CDefError)model)CallablecCs t|tS)N) isinstancer)xr /usr/lib64/python3.6/api.py sr c@seZdZdZdfddZdgddZdhdd Zdid d Zdjd dZddZ ddZ dkddZ ddZ ddZ ddZddZdlddZdmd d!Zd"d#Zdod%d&Zd'd(Zdpd)d*Zd+d,Zdqd-d.Zdrd0d1Zdsd2d3Zd4d5Zdtd6d7Zd8d9Zd:d;Zeeedd<Zdvd=d>Z d?d@Z!dAdBZ"dCdDZ#dEdFZ$dGdHZ%dIdJZ&dKdLZ'dMdNZ(dOdPZ)dwdRdSZ*dxdUdVZ+dWdXZ,dYdZZ-dyd\d]Z.d^d_Z/d`daZ0dbdcZ1dddeZ2dS)zFFIa The main top-level class that you instantiate once, or once per module. Example usage: ffi = FFI() ffi.cdef(""" int printf(const char *, ...); """) C = ffi.dlopen(None) # standard library -or- C = ffi.verify() # use a C compiler: verify the decl above is right C.printf("hello, %s!\n", ffi.new("char[]", "world")) Nc Cs|dkr^ddl}ddlm}|j|kr^t|drJtd|t|j|jfntd|t|jfddlm}||_t|_ |j |_ i|_ t jd j|_t jd j|_g|_g|_g|_g|_d|_i|_d|_d|_tj||_t|d r|j|x2t|jD]$}|jd rt ||t!||qW|j "|j"tj#|_$|j"tj%|_&WdQRXt'|t jrtt(d s|j)|j$dt(_*|j+\t(_,t(_-n |j)|j$d|_*|j+\|_,|_-|j.|_.dS)z{Create an FFI instance. The 'backend' argument is used to select a non-default backend, mostly for tests. Nrr) __version____file__zVersion mismatch: this is the 'cffi' package version %s, located in %r. When we import the top-level '_cffi_backend' extension module, we get version %s, located in %r. The two versions should be equal; check your installation.zVersion mismatch: this is the 'cffi' package version %s, located in %r. This interpreter comes with a built-in '_cffi_backend' module, which is version %s. The two versions should be equal; check your installation.)cparserZ parsed_typesZ new_typesset_ffiZRTLD_NULL)/Z _cffi_backendr hasattr Exceptionrr_backendr_lockZParser_parser_cached_btypestypes ModuleType__dict__ _parsed_typesZ _new_types_function_caches _libraries _cdefsources_included_ffis_windows_unicode_init_once_cache _cdef_version _embeddingrZ get_typecacheZ _typecacherlist startswithsetattrgetattr_get_cached_btypeZ voidp_typeBVoidPZchar_array_typeBCharArr castrZ _get_typesCDataZCTypebuffer)selfbackendr rnamer r r __init__'sR          z FFI.__init__FcCs|j|||ddS)aParse the given C source. This registers all declared functions, types, and global variables. The functions and global variables can then be accessed via either 'ffi.dlopen()' or 'ffi.verify()'. The types can be used in 'ffi.new()' and other functions. If 'packed' is specified as True, all structs declared inside this cdef are packed, i.e. laid out without any field alignment at all. )overridepackedN)_cdef)r/csourcer3r4r r r cdefcszFFI.cdefcCs$|j||dd|jdkr d|_dS)NT)r4Z dllexportr)r5r$)r/r6r4r r r embedding_apims zFFI.embedding_apic Kst|ts&t|tstd|jd}|j|t|_|jj |fd|i||j j ||rvx|j D] }|j qfW|jj}|rg|j_x|D]}|j||qWWdQRXdS)Nz cdef() argument must be a stringasciir3)rstr basestring TypeErrorencoderobjectr#rparserappendrclearZ _recompletefinish_backend_type)r/r6r3Zoptionscache finishlisttpr r r r5rs        z FFI._cdefrc CsTt|ts|dkst|j.t|||\}}|jj||jj|WdQRX|S)aHLoad and return a dynamic library identified by 'name'. The standard C library can be loaded by passing None. Note that functions and types declared by 'ffi.cdef()' are not linked to a particular library, just like C headers; in the library we only look for the actual (untyped) symbols. N)rr;AssertionErrorr_make_ffi_libraryrr@r)r/r1flagslibZfunction_cacher r r dlopens  z FFI.dlopencCst|j|dS)zClose a library obtained with ffi.dlopen(). After this call, access to functions or variables from the library will fail (possibly with a segmentation fault). N)type__cffi_close__)r/rIr r r dlclosesz FFI.dlclosecCsj|}||jkr|j|St|ts,|jd}|jj|}|j}|rJ|j}|j|}||f}||j|<|S)Nr9) rrr:r=rZ parse_typeZis_raw_functionZas_function_pointerr))r/cdeclkeyrKreally_a_function_typebtyperesultr r r _typeof_lockeds       zFFI._typeof_lockedcCsdy|j|}Wn0tk r>|j|j|}WdQRXYnX|\}}|r`| r`td|f|S)Nz>the type %r is a function type, not a pointer-to-function type)rKeyErrorrrSr)r/rNconsider_function_as_funcptrrRrQrPr r r _typeofs  z FFI._typeofc Cst|tr|j|St||jr,|jj|St|tjrLt|}|dk rL|St|tj r|t |dr||j |j |j SQRXtt|dS)zParse the C type given as a string and return the corresponding object. It can also be used on 'cdata' instance to get its C type. N_cffi_base_type)rr;rVr-rtypeofrBuiltinFunctionType_builtin_function_type FunctionTyperrr)rWr<rK)r/rNresr r r rXs       z FFI.typeofcCs0t|tr |j|}|jj|S|jj|SdS)zvReturn the size in bytes of the argument. It can be a string naming a C type, or a 'cdata' instance. N)rr;rVrsizeof)r/rNBTyper r r r]s   z FFI.sizeofcCs t|tr|j|}|jj|S)z\Return the natural alignment size in bytes of the C type given as a string. )rr;rVralignof)r/rNr r r r_s  z FFI.alignofcGs(t|tr|j|}|j|f|dS)a3Return the offset of the named field inside the given structure or array, which must be given as a C type name. You can give several field names in case of nested structures. You can also give numeric values which correspond to array items, in case of an array type. r)rr;rV _typeoffsetof)r/rNfields_or_indexesr r r offsetofs  z FFI.offsetofcCs"t|tr|j|}|jj||S)aAllocate an instance according to the specified C type and return a pointer to it. The specified C type must be either a pointer or an array: ``new('X *')`` allocates an X and returns a pointer to it, whereas ``new('X[n]')`` allocates an array of n X'es and returns an array referencing it (which works mostly like a pointer, like in C). You can also use ``new('X[]', n)`` to allocate an array of a non-constant length n. The memory is initialized following the rules of declaring a global variable in C: by default it is zero-initialized, but an explicit initializer can be given which can be used to fill all or part of the memory. When the returned object goes out of scope, the memory is freed. In other words the returned object has ownership of the value of type 'cdecl' that it points to. This means that the raw data can be used as long as this object is kept alive, but must not be used for a longer time. Be careful about that when copying the pointer to the memory somewhere else, e.g. into another structure. )rr;rVrZnewp)r/rNinitr r r news  zFFI.newTcs,jj}|j|||dfdd }|S)aReturn a new allocator, i.e. a function that behaves like ffi.new() but uses the provided low-level 'alloc' and 'free' functions. 'alloc' is called with the size as argument. If it returns NULL, a MemoryError is raised. 'free' is called with the result of 'alloc' as argument. Both can be either Python function or directly C functions. If 'free' is None, then no free function is called. If both 'alloc' and 'free' are None, the default is used. If 'should_clear_after_alloc' is set to False, then the memory returned by 'alloc' is assumed to be already cleared (or you are fine with garbage); otherwise CFFI will clear it. Ncst|trj|}||S)N)rr;rV)rNrc) allocatorr/r r allocates  z#FFI.new_allocator..allocate)N)rr new_allocator)r/ZallocZfreeZshould_clear_after_allocZ compiled_ffirfr )rer/r rgs  zFFI.new_allocatorcCs"t|tr|j|}|jj||S)zSimilar to a C cast: returns an instance of the named C type initialized with the given 'source'. The source is casted between integers or pointers of any type. )rr;rVrr,)r/rNsourcer r r r,s  zFFI.castrcCs|jj||S)aReturn a Python string (or unicode string) from the 'cdata'. If 'cdata' is a pointer or array of characters or bytes, returns the null-terminated string. The returned string extends until the first null character, or at most 'maxlen' characters. If 'cdata' is an array then 'maxlen' defaults to its length. If 'cdata' is a pointer or array of wchar_t, returns a unicode string following the same rules. If 'cdata' is a single character or byte or a wchar_t, returns it as a string or unicode string. If 'cdata' is an enum, returns the value of the enumerator as a string, or 'NUMBER' if the value is out of range. )rstring)r/cdatamaxlenr r r ri%sz FFI.stringcCs|jj||S)aLUnpack an array of C data of the given length, returning a Python string/unicode/list. If 'cdata' is a pointer to 'char', returns a byte string. It does not stop at the first null. This is equivalent to: ffi.buffer(cdata, length)[:] If 'cdata' is a pointer to 'wchar_t', returns a unicode string. 'length' is measured in wchar_t's; it is not the size in bytes. If 'cdata' is a pointer to anything else, returns a list of 'length' items. This is a faster equivalent to: [cdata[i] for i in range(length)] )runpack)r/rjZlengthr r r rl7sz FFI.unpackcCs|jj|j||S)aReturn a that points to the data of the given Python object, which must support the buffer interface. Note that this is not meant to be used on the built-in types str or unicode (you can build 'char[]' arrays explicitly) but only on objects containing large quantities of raw data in some other format, like 'array.array' or numpy arrays. )r from_bufferr+)r/Z python_bufferZrequire_writabler r r rmUs zFFI.from_buffercCs|jj|||S)a_ffi.memmove(dest, src, n) copies n bytes of memory from src to dest. Like the C function memmove(), the memory areas may overlap; apart from that it behaves like the C function memcpy(). 'src' can be any cdata ptr or array, or any Python buffer object. 'dest' can be any cdata ptr or array, or a writable Python buffer object. The size to copy, 'n', is always measured in bytes. Unlike other methods, this one supports all Python buffer including byte strings and bytearrays---but it still does not support non-contiguous buffers. )rmemmove)r/destsrcnr r r rn`sz FFI.memmovecsBfdd}ttr*jdd|dkr6|S||SdS)aReturn a callback object or a decorator making such a callback object. 'cdecl' must name a C function pointer type. The callback invokes the specified 'python_callable' (which may be provided either directly or via a decorator). Important: the callback object must be manually kept alive for as long as the callback may be invoked from the C level. cs"t|stdjj|S)Nz.the 'python_callable' argument is not callable)callabler<rcallback)python_callable)rNerroronerrorr/r r callback_decorator_wrapxs z-FFI.callback..callback_decorator_wrapT)rUN)rr;rV)r/rNrtrurvrwr )rNrurvr/r rsps  z FFI.callbackrcCsht|tr|j|}|j}|jdrBd|jj|dkrBd|}n|rZ|ddkrZd|}|jj||S)a.Return a string giving the C type 'cdecl', which may be itself a string or a object. If 'replace_with' is given, it gives extra text to append (or insert for more complicated C types), like a variable name, or '*' to get actually the C type 'pointer-to-cdecl'. *z&[&z(%s)rz[( )rr;rVstripr&rZgetcname)r/rNZ replace_withr r r getctypes    z FFI.getctypecCs|jj|||S)aReturn a new cdata object that points to the same data. Later, when this new cdata object is garbage-collected, 'destructor(old_cdata_object)' will be called. The optional 'size' gives an estimate of the size, used to trigger the garbage collection more eagerly. So far only used on PyPy. It tells the GC that the returned object keeps alive roughly 'size' bytes of external memory. )rZgcp)r/rjZ destructorsizer r r gcs zFFI.gcc Csf|jjddksty|j|}Wn>tk r`g}|j||}x|D]}|j||qHWYnX|S)NF)racquirerFrrTZget_cached_btyperB)r/rKr^rDr r r r)s  zFFI._get_cached_btypecKsVddlm}m}|jr |j||p(|}||||f||_|jj}|jj||S)aVerify that the current ffi signatures compile on this machine, and return a dynamic library object. The dynamic library can be used to call functions and access global variables declared in this 'ffi'. The library is compiled by the C compiler: it gives you C-level API compatibility (including calling macros). This is unlike 'ffi.dlopen()', which requires binary compatibility in the signatures. r)Verifier_caller_dir_pycache)verifierrrr!_apply_windows_unicode load_libraryrr@)r/rhtmpdirkwargsrrrIr r r verifys     z FFI.verifycCs |jjS)N)rZ get_errno)r/r r r _get_errnoszFFI._get_errnocCs|jj|dS)N)rZ set_errno)r/errnor r r _set_errnoszFFI._set_errnoz(the value of 'errno' from/to the C callscCs |jj|S)N)r getwinerror)r/coder r r rszFFI.getwinerrorc Cs|jtj||SQRXdS)N)rr pointer_cache)r/ctyper r r _pointer_toszFFI._pointer_toc Gsy|jj|}Wn8tk rHdt|jkrBt|j|f|SYnX|rd|j|f|\}}n|jdkrvtdd}|j|}|jj |||S)zReturn the address of a . If 'fields_or_indexes' are given, returns the address of that field or array item in the structure or array, recursively in case of nested structures. __addressof__Zpointerzaddressof(pointer)r) rrXr<rKrrr`kindrZ rawaddressof)r/rjraroffsetZctypeptrr r r addressofs  z FFI.addressofcGsD|jj||\}}x(|D] }|jj||d\}}||7}qW||fS)Nr)rZ typeoffsetof)r/rZfield_or_indexrarZfield1Zoffset1r r r r`s   zFFI._typeoffsetofcCst|tstdt|jf||kr.td|jX|jF|jj|j|j j d|j j |j |j j d|j j |WdQRXWdQRXdS)aIncludes the typedefs, structs, unions and enums defined in another FFI instance. Usage is similar to a #include in C, where a part of the program might include types defined in another part for its own usage. Note that the include() method has no effect on functions, constants and global variables, which must anyway be accessed directly from the lib object returned by the original FFI instance. zGffi.include() expects an argument that is also of type cffi.FFI, not %rzself.include(self)[]N) rr r<rK__name__ ValueErrorrrincluderr@extendr )r/Zffi_to_includer r r rs   z FFI.includecCs|jj|j|S)N)rZ newp_handler*)r/rr r r new_handle szFFI.new_handlecCs |jj|S)N)r from_handle)r/rr r r rszFFI.from_handlecCs>|jdk rtdt|}|r*|jdn |jd||_dS)acWindows: if 'enabled_flag' is True, enable the UNICODE and _UNICODE defines in C, and declare the types like TCHAR and LPTCSTR to be (pointers to) wchar_t. If 'enabled_flag' is False, declare these types to be (pointers to) plain 8-bit characters. This is mostly for backward compatibility; you usually want True. Nz%set_unicode() can only be called onceztypedef wchar_t TBYTE;typedef wchar_t TCHAR;typedef const wchar_t *LPCTSTR;typedef const wchar_t *PCTSTR;typedef wchar_t *LPTSTR;typedef wchar_t *PTSTR;typedef TBYTE *PTBYTE;typedef TCHAR *PTCHAR;ztypedef char TBYTE;typedef char TCHAR;typedef const char *LPCTSTR;typedef const char *PCTSTR;typedef char *LPTSTR;typedef char *PTSTR;typedef TBYTE *PTBYTE;typedef TCHAR *PTCHAR;)r!rboolr7)r/Z enabled_flagr r r set_unicodes  zFFI.set_unicodecCs>|jdf}t|ttfs"tdt|ddg}||d<dS)NZ define_macrosz''define_macros' must be a list or tupleUNICODE1_UNICODE)rr)rr)getrr%tupler<)r/kwdsZ defmacrosr r r r0s  zFFI._apply_windows_unicodec sjfdd}dtjkrddl}tjdkrNd}ttdr|d|jjtjd n4tjdkr^d }nd }ttdr|d|jjtjd ttdr|d|jjtjddntjdkrd}ttdr|d7}nLy ddl }Wn t k rddl m }YnXd}|j dr||j d7}|tj d?tj d?d@f}ttdrF|tj7}|d|tjdkrf|dddS)Ncs"j|g}||kr|j|dS)N) setdefaultr@)rOvalueZlst)rr r ensure:s z(FFI._apply_embedding_fix..ensureZ__pypy__rwin32Zpython27prefixZ library_dirsZlibszpypy-czpypy3-cbinZpypyZgoalz python%d%dgettotalrefcountZ_d) sysconfigz python%d.%dZ DEBUG_EXTabiflagsZ librariesZextra_link_argsz /MANIFEST)r)sysbuiltin_module_namesosplatformrpathjoinr version_infor ImportErrorZ distutilsget_config_var hexversionr)r/rrrZ pythonlibtemplaterr )rr _apply_embedding_fix8s@                zFFI._apply_embedding_fix.ccKsdddl}t|drtdt|ts,td|j|ksF|jrN|j|krNtdt||||f|_ dS)Nr_assigned_sourcez:set_source() cannot be called several times per ffi objectz'module_name' must be a stringzY'module_name' must not contain '/': use a dotted name to make a 'package.module' location) rrrrr;r<sepaltsepr:r)r/ module_namerhsource_extensionrrr r r set_sourcejs  zFFI.set_sourcebuildc Csddlm}ddlm}t|ds>t|dr6|jjStd|j\}}}}|dkr\t d||||||f|||d d |\} } |r| rt j j d | j dfnt j j d | j df| S) Nr)mkpathr) recompilerrz8set_source() must be called before distutils_extension()zadistutils_extension() is only for C extension modules, not for dlopen()-style pure Python modulesF)rZextradirrcall_c_compilerzregenerated: %r znot modified: %r )Zdistutils.dir_utilr recompilerrrrZ get_extensionrrr<rstderrwriteZsources) r/rverboserrrrhrrZextupdatedr r r distutils_extensionws&     zFFI.distutils_extensioncCsZddlm}t|dstd|j\}}}}|dkrsz+FFI.embedding_init_code..Z cffi_initexec) r$rrematchend splitlinesgrouprangelenrstripr&rr)r/Zpysourcerrlinesrrr )rr embedding_init_codes$    zFFI.embedding_init_codecOs tddS)Nz:ffi.def_extern() is only available on API-mode FFI objects)r)r/argsrr r r def_externszFFI.def_externcCsg}g}g}xh|jjD]\}|jdr8|j|ddq|jdrV|j|ddq|jdr|j|ddqW|j|j|j|||fS)zReturns the user type names known to this FFI instance. This returns a tuple containing three lists of names: (typedef_names, names_of_structs, names_of_unions) ztypedef Nzstruct zunion )r _declarationsr&r@sort)r/ZtypedefsZstructsZunionsrOr r r list_typess   zFFI.list_types)N)FF)F)F)r)F)N)NNTr)r)F)NNN)r)r)rNr)r)r)rT)rrNN)3r __module__ __qualname____doc__r2r7r8r5rJrMrSrVrXr]r_rbrdrgr,rirlrmrnrsr|r~r)rrrpropertyrrrrr`rrrrrrrrrrrrrrrr r r r r sb <              2   r c Csddl}|dkr*tjdkr&|jd|Sd}d}d|ksHd|ksH|j|kr|y |j||Stk rz}z |}WYdd}~XnXddl}|jj|}|dkr|dkrtjdkrtj d krtdd|f}|dk rd ||f}t||j||S) Nrrcr/rzXdlopen(None) cannot work on Windows for Python 3 (see http://bugs.python.org/issue23606)zGctypes.util.find_library() did not manage to locate a library called %rz%s. Additionally, %s)r) rrrrrOSErrorZ ctypes.utilutilZ find_libraryr) r0r1rHrZ first_erroreZctypesrmsgr r r _load_backend_libs,     rc s j}t||| fdd fdd fdddd fd d id gi fd d   fdd G fdddt|dk ry"t|ts|jd}d|_Wntk rYnX jfS)Ncs<d|}jj|\}}j|}j||}|j|<dS)Nz function )rrr) load_functionr)r1rOrE_r^r) backendlibffilibraryr r accessor_function%s   z,_make_ffi_library..accessor_functioncs\d}jj|\}}j|jjttfddfdddS)Nz variable cs S)Nr )r/)r^r1 read_variabler r r 3sz>_make_ffi_library..accessor_variable..cs |S)Nr )r/r)r^r1write_variabler r r 4s)rrr)rrr'r)r1rOrEr) FFILibraryrr)r^r1rrr accessor_variable,s z,_make_ffi_library..accessor_variablecsy|Stk rjZ|krtd|}jj|\}}j|}|jdkr`tj|}j||}||<WdQRX|SXdS)Nz variable Zarray) rTrrrr)rrrr)r1rOrErr^p)addr_variablesrrr r addressof_var6s    z(_make_ffi_library..addressof_varcSstd|fdS)NzDnon-integer constant '%s' cannot be accessed from a dlopen() library)NotImplementedError)r1r r r accessor_constantEsz,_make_ffi_library..accessor_constantcsjj|j|<dS)N)r_int_constantsr)r1)rrr r accessor_int_constantIsz0_make_ffi_library..accessor_int_constantFcsdjkrdSxjjjD]\}\}}t|tjs~|jdd\}}|dkrZ|<q|dkrl|<q|dkr|<q x0t|j D]"\}}||ffdd }||<qWq Wxjj D]}j |qWjd<dS) NrrzrZfunctionZvariableZconstantcs|j|j|j|<dS)N)Zcheck_not_partialZ enumvaluesr)r1rEr)rr r accessor_enum_szB_make_ffi_library..update_accessors..accessor_enum) r#rritemsrrZEnumTypesplit enumerateZ enumeratorsr r)rOrErrr1rZenumnamer)r rr r accessorsaccessors_versionrrr r update_accessorsPs"    z+_make_ffi_library..update_accessorsc sXjH|jks|jkr dS|kr>|kr>t|||WdQRXdS)N)rrAttributeError)r1)rrrrrr r make_accessorgsz(_make_ffi_library..make_accessorcsReZdZfddZfddZfddZfddZfd d Zd S) z%_make_ffi_library..FFILibrarycs|t||S)N)r()r/r1)rr r __getattr__rsz1_make_ffi_library..FFILibrary.__getattr__c sJyt|j|}Wn(tk r8|t|||YnX|j||dS)N)r( __class__rr'__set__)r/r1rr)rr r __setattr__us z1_make_ffi_library..FFILibrary.__setattr__c s jjSQRXdS)N)rkeys)r/)rrrr r __dir__}sz-_make_ffi_library..FFILibrary.__dir__csf|jkrj|S|jkr&|S||jkrBj|S|jkrT|Std|fdS)Nz:cffi library has no function or global variable named '%s')rr)r/r1)rr rrr r rs      z3_make_ffi_library..FFILibrary.__addressof__csj|jjdS)N)Z close_librrA)r/)rr r rLsz4_make_ffi_library..FFILibrary.__cffi_close__N)rrrrrrrrLr )rrr rrrrrr r rqs    rzutf-8z FFILibrary_%s) rrr>rr:r=r UnicodeErrorr)rZlibnamerHr0r )rr rr rrrrr rrrrrr rG!s,   "   rGcCsdddl}y$|j|j}|j}|j}||}Wntttfk rFdSX|j |j |SQRXdS)Nr) rmodulesrZ_cffi_original_ffiZ_cffi_types_of_builtin_funcsrTrr<rr))rrmodulerZtypes_of_builtin_funcsrEr r r rZs  rZ)rrlockrrurrrrr NameError collectionsrr;r:r>r rrGrZr r r r s*     wz__pycache__/vengine_cpy.cpython-36.pyc000064400000102100152344566040013713 0ustar003 MZ@s@ddlZddlZddlmZddlmZGdddeZdZdS)N)model)VerificationErrorc@seZdZdZdZddZddZddZd d Zdnd d Z ddZ ddZ ddZ doddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(ZeZeZeZeZeZd)d*Zd+d,Zd-d.ZeZd/d0Z eZ!d1d2Z"d3d4Z#d5d6Z$d7d8Z%eZ&d9d:Z'd;d<Z(d=d>Z)d?d@Z*dAdBZ+dCdDZ,dEdFZ-dGdHZ.eZ/dIdJZ0dKdLZ1dMdNZ2dOdPZ3dpdSdTZ4dUdVZ5dWdXZ6eZ7eZ8eZ9dqdYdZZ:d[d\Z;drd^d_Zd`daZ?dbdcZ@dddeZAeZBeZCeZDeZEdfdgZFdhdiZGeZHeZIdjdkZJdldmZKdS)sVCPythonEnginexTcCs||_|j|_i|_i|_dS)N)verifierffi_struct_pending_verification_types_of_builtin_functions)selfrr #/usr/lib64/python3.6/vengine_cpy.py__init__ szVCPythonEngine.__init__cCsdS)Nr )r kwdsr r r patch_extension_kwdssz#VCPythonEngine.patch_extension_kwdsc CsPytj||\}}}Wntk r*dSX|dk r<|j|d|krLdS|S)Nr)imp find_module ImportErrorclose)r Z module_namepathZ so_suffixesffilenameZdescrr r r rs zVCPythonEngine.find_modulecCsi|_|jddS)NZ collecttype) _typesdict _generate)r r r r collect_types$szVCPythonEngine.collect_typescCs|jj|ddS)N )Z_fwrite)r Zwhatr r r _prnt(szVCPythonEngine._prntcCs |j|S)N)r)r typer r r _gettypenum+szVCPythonEngine._gettypenumcCs:t|tj s|jdkr6||jkr6t|j}||j|<dS)Nz long double) isinstancer PrimitiveTypenamerlen)r tpZnumr r r _do_collect_type/s    zVCPythonEngine._do_collect_typecCs|jddg|_|j}|t|||jj||jd|j||d|jd|d|d|d||jj}|jd}|d ||d |d |d ||d |d|d|d|d||d|d||d|d|d|d|d|d|f|d|d|d|d|d||d||d|d||d|d|d||d|d |d!|f|d |d"|d||d#dS)$Nz ((void)lib,0)declz&static PyMethodDef _cffi_methods[] = {methodz3 {"_cffi_setup", _cffi_setup, METH_VARARGS, NULL},z) {NULL, NULL, 0, NULL} /* Sentinel */z};Fz#if PY_MAJOR_VERSION >= 3z.static struct PyModuleDef _cffi_module_def = {z PyModuleDef_HEAD_INIT,z "%s",z NULL,z -1,z _cffi_methods,z NULL, NULL, NULL, NULLZPyMODINIT_FUNCzPyInit_%s(void){z PyObject *lib;z+ lib = PyModule_Create(&_cffi_module_def);z if (lib == NULL)z return NULL;z# if (%s < 0 || _cffi_init() < 0) {z Py_DECREF(lib);z }z return lib;}z#elsez init%s(void)z+ lib = Py_InitModule("%s", _cffi_methods);z return;z! if (%s < 0 || _cffi_init() < 0)z return;z#endif) r_chained_list_constantsrcffimod_headerrZpreambler_generate_setup_customget_module_name)r prntmodnameZ constantsr r r write_source_to_f6st          z VCPythonEngine.write_source_to_fNcsvtjzttdrtj}zvy6ttdr<|dk rsz/VCPythonEngine.load_library..csg|] }|qSr r )r4i) revmappingr r r7scs&eZdZZjZgZfddZdS)z/VCPythonEngine.load_library..FFILibrarycsjt|jS)N) _cffi_dirlist__dict__)r ) FFILibraryr r __dir__sz7VCPythonEngine.load_library..FFILibrary.__dir__N)__name__ __module__ __qualname__Z_cffi_python_modulerZ _cffi_ffir:r>r )r=moduler r r r=sr=rz0reimporting %r might overwrite older definitionsZloaded)library)r acquire_lockhasattrsysr2r3Z load_dynamicrr.Zmodulefilenamerr release_lock_loaddictritemsranger$r;mapr_get_cached_btypeobjectZ _cffi_setupwarningswarnZ_cffi_original_ffir Z_cffi_types_of_builtin_funcs)r flagsZprevious_flagseerrorlstrCrOr )r=rBr9r r load_librarys<      zVCPythonEngine.load_librarycCs$dd|jjjjD}|j|S)NcSsg|]\}\}}||fqSr r )r4r5r%Zqualr r r r7sz4VCPythonEngine._get_declarations..)rZ_parserZ _declarationsrJsort)r rTr r r _get_declarationssz VCPythonEngine._get_declarationscCsx|jD]\}}|jdd\}}yt|d||f}Wn tk rXtd|YnXy|||Wq tk r}ztj||WYdd}~Xq Xq WdS)N rz_generate_cpy_%s_%sznot implemented in verify(): %r)rWsplitgetattrAttributeErrorr Exceptionrattach_exception_info)r step_namer#r%kindrealnamer(rRr r r rs zVCPythonEngine._generatec Ksx||jD]p\}}|jdd\}}t|d||f}y||||f|Wq tk rx} ztj| |WYdd} ~ Xq Xq WdS)NrXrz _%s_cpy_%s)rWrYrZr\rr]) r rBr^rr#r%r_r`r(rRr r r rHs zVCPythonEngine._loadcCsdS)Nr )r r%r#r r r _generate_nothingsz VCPythonEngine._generate_nothingcKsdS)Nr )r r%r#rBrr r r _loaded_noopszVCPythonEngine._loaded_noopcCs*d}t|tjrT|jr2|jdkr2d}d|j}nd|jd|jjddf}d}nt|tjrt|j||||dSt|tj tj fr|j d ||j ||f|j d |dSt|tj rd |jd}d |j |}d }nt||j d||||f|j d||jd|f|j d |dS)Nr_BoolZ_cffi_to_c_intz, %sz(%s)_cffi_to_c_%srX_z-1z6 if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)z %s;z(%s)_cffi_to_c_pointerz, _cffi_type(%d)ZNULLz %s = %s(%s%s);z' if (%s == (%s)%s && PyErr_Occurred()))r!rr"is_integer_typer# get_c_namereplace PointerType"_convert_funcarg_to_c_ptr_or_array StructOrUnionEnumTyperr FunctionPtrTypeNotImplementedError)r r%fromvartovarerrcodeZextraargZ converterZerrvaluer r r _convert_funcarg_to_cs4     z$VCPythonEngine._convert_funcarg_to_ccCst|tjr|jddS)NzPy_ssize_t datasize)r!rrhadd)r r% localvarsr r r _extra_local_variabless z%VCPythonEngine._extra_local_variablescCs|jd|jd|j|||f|jd|jd|jd||jd|f|jd|f|jd||j||f|jd||jd dS) Nz1 datasize = _cffi_prepare_pointer_call_argument(z( _cffi_type(%d), %s, (char **)&%s);z if (datasize != 0) {z if (datasize < 0)z %s;z" %s = alloca((size_t)datasize);z, memset((void *)%s, 0, (size_t)datasize);zL if (_cffi_convert_array_from_object((char *)%s, _cffi_type(%d), %s) < 0)z })rr )r r%rnrorpr r r ris   z1VCPythonEngine._convert_funcarg_to_c_ptr_or_arraycCst|tjr`|jr,|jdkr,d||jfS|jdkrLd|jjdd|fSd||j|fSnt|tjtjfrd||j|fSt|tj rd||jtj|j fSt|tj r|j dkrt d |j|fd ||j|fSt|tjrd||j|fSt|dS) Nrcz_cffi_from_c_int(%s, %s)z long doublez_cffi_from_c_%s(%s)rXrdz/_cffi_from_c_deref((char *)&%s, _cffi_type(%d))z0_cffi_from_c_pointer((char *)%s, _cffi_type(%d))z!'%s' is used as %s, but is opaquez0_cffi_from_c_struct((char *)&%s, _cffi_type(%d)))r!rr"rer#rgr rhrl ArrayTypeitemrjfldnames TypeError _get_c_namerkrm)r r%varcontextr r r _convert_expr_from_c)s.     z#VCPythonEngine._convert_expr_from_ccCsLt|tjst|jr"|j|n&x|jD]}|j|q*W|j|jdS)N)r!rrlAssertionErrorellipsisr&argsresult)r r%r#rr r r "_generate_cpy_function_collecttypePs   z1VCPythonEngine._generate_cpy_function_collecttypec Cst|tjst|jr(|jd||dS|j}t|j}|dkrFd}n|dkrTd}nd}|d|d||f|d d |}x.t |jD] \}}|d |j d ||qWt } x|jD]}|j || qWx| D]} |d | fqWt|j tjs"d } d|}|d |j j d|nd} t|jdkrtt|j} x| D]}|d|qJW||dd||djdd| Df|d|x6t |jD](\}}|j|d|d|d|qW|d|d|d| |djddtt|jDf|d|d ||d!|dkr>|d"| r^|d#|j|j d$d%n|d&|d'|d(|dS))NFrZnoargrZarg0rzstatic PyObject *z(_cffi_f_%s(PyObject *self, PyObject *%s)r)zargument of %sz %s;z x%dz result = z result of %sz resultrz PyObject *arg%d;z+ if (!PyArg_ParseTuple(args, "%s:%s", %s))Oz, cSsg|] }d|qS)z&arg%dr )r4r8r r r r7sz>VCPythonEngine._generate_cpy_function_decl..z return NULL;zarg%dzx%dz return NULLz Py_BEGIN_ALLOW_THREADSz _cffi_restore_errno();z { %s%s(%s); }cSsg|] }d|qS)zx%dr )r4r8r r r r7sz _cffi_save_errno();z Py_END_ALLOW_THREADSz (void)self; /* unused */z (void)noarg; /* unused */z return %s;rz result typez Py_INCREF(Py_None);z return Py_None;r*)r!rrlr}r~_generate_cpy_constrr$r enumeraterfsetrtrZVoidTyperKjoinrqr|) r r%r#r/numargsZargnamer{r8rrsr'Z result_coderngr r r _generate_cpy_function_decl[st    " $ z*VCPythonEngine._generate_cpy_function_declcCsL|jr dSt|j}|dkr"d}n|dkr0d}nd}|jd|||fdS)NrZ METH_NOARGSrZMETH_OZ METH_VARARGSz {"%s", _cffi_f_%s, %s, NULL},)r~r$rr)r r%r#rmethr r r _generate_cpy_function_methods z,VCPythonEngine._generate_cpy_function_methodcCs.|jr dSt||}t|||||j|<dS)N)r~rZsetattrr )r r%r#rBrCfuncr r r _loaded_cpy_functions   z#VCPythonEngine._loaded_cpy_functioncCs ||jkst|j|d|dS)Nstruct)r#r}_generate_struct_or_union_decl)r r%r#r r r _generate_cpy_struct_declsz(VCPythonEngine._generate_cpy_struct_declcCs|j|d|dS)Nr) _generate_struct_or_union_method)r r%r#r r r _generate_cpy_struct_methodsz*VCPythonEngine._generate_cpy_struct_methodcCs|j|d||dS)Nr)_loading_struct_or_union)r r%r#rBr r r _loading_cpy_structsz"VCPythonEngine._loading_cpy_structcKs|j|dS)N)_loaded_struct_or_union)r r%r#rBrr r r _loaded_cpy_structsz!VCPythonEngine._loaded_cpy_structcCs ||jkst|j|d|dS)Nunion)r#r}r)r r%r#r r r _generate_cpy_union_declsz'VCPythonEngine._generate_cpy_union_declcCs|j|d|dS)Nr)r)r r%r#r r r _generate_cpy_union_methodsz)VCPythonEngine._generate_cpy_union_methodcCs|j|d||dS)Nr)r)r r%r#rBr r r _loading_cpy_unionsz!VCPythonEngine._loading_cpy_unioncKs|j|dS)N)r)r r%r#rBrr r r _loaded_cpy_unionsz VCPythonEngine._loaded_cpy_unionc Cs|jdkrdSd||f}d||f}d||fj}|j}|d||f|d|d|dx|jD]\}} } } t| tjr| js| dkr|d |qny$|d | jd d || d |fWqnt k r} z|dt | WYdd} ~ XqnXqnW|d|d|d|f|d|d||d|d||dxt|jD]h\}} } } | dkrrqX|d||ft| tj r| j dkr|d| j n|d||fqXW|d|d|d|d|d|d|d|f|d|dS) Nz_cffi_check_%s_%sz_cffi_layout_%s_%sz%s %szstatic void %s(%s *p)r)z8 /* only to generate compile-time warnings or errors */z (void)p;rz (void)((p->%s) << 1);z { %s = &p->%s; (void)tmp; }z*tmpzfield %r)Zqualsz /* %s */r*zstatic PyObject *z#%s(PyObject *self, PyObject *noarg)z, struct _cffi_aligncheck { char x; %s y; };z static Py_ssize_t nums[] = {z sizeof(%s),z) offsetof(struct _cffi_aligncheck, y),z offsetof(%s, %s),z 0, /* %s */z sizeof(((%s *)0)->%s),z -1z };z (void)self; /* unused */z (void)noarg; /* unused */z' return _cffi_get_struct_layout(nums);z3 /* the next line is not executed, but compiled */z %s(0);)rwstripr enumfieldsr!rr"rerfrstrrulengthry) r r%prefixr#Z checkfuncnamelayoutfuncnamecnamer/fnameftypefbitsizefqualrRr r r rsX    &   z-VCPythonEngine._generate_struct_or_union_declcCs0|jdkrdSd||f}|jd||fdS)Nz_cffi_layout_%s_%sz {"%s", %s, METH_NOARGS, NULL},)rwr)r r%rr#rr r r rs   z/VCPythonEngine._generate_struct_or_union_methodc Cs|jdkrdSd||f}t||}|}t|tjr|jr|d}|d} |ddd} |ddd} |jt| t| kot|jknst| | || f|_ nd||fj } || f|j |<dS)Nz_cffi_layout_%s_%srrz%s %s) rwrZr!rrjpartialZ force_flattenr$r}Z fixedlayoutrr ) r r%rr#rBrZfunctionlayoutZ totalsizeZtotalalignmentZfieldofsZ fieldsizerr r r r s   *z'VCPythonEngine._loading_struct_or_unionc Cs|jdkrdS|jj|||jkrdd}|j}|j|}|jj|\}}||d|j|d||d|j|dd}x|jD]v\}} } } | dkrq||||j||d|f||ddkr|j| } |||d|j| d |f|d7}qW|t |kst dS) NcSs||krtd|||fdS)Nz'%s (we have %d, but C compiler says %d))r)Z realvalueZ expectedvaluemsgr r r check(sz5VCPythonEngine._loaded_struct_or_union..checkrzwrong total sizerzwrong total alignmentrzwrong offset for field %rzwrong size for field %r) rwrrMr popsizeofZalignofrZoffsetofr$r}) r r%rrZBStructrrr8rrrrZBFieldr r r r!s,        z&VCPythonEngine._loaded_struct_or_unioncCs.t|tjr|j||dn|j|d|dS)Nr)r!rrk_generate_cpy_enum_declr)r r%r#r r r _generate_cpy_anonymous_declEs z+VCPythonEngine._generate_cpy_anonymous_declcCst|tjs|j|d|dS)Nr)r!rrkr)r r%r#r r r _generate_cpy_anonymous_methodKs z-VCPythonEngine._generate_cpy_anonymous_methodcCs0t|tjr|j|||n|j|d||dS)Nr)r!rrk_loading_cpy_enumr)r r%r#rBr r r _loading_cpy_anonymousOs z%VCPythonEngine._loading_cpy_anonymouscKs.t|tjr |j|||f|n |j|dS)N)r!rrk_loaded_cpy_enumr)r r%r#rBrr r r _loaded_cpy_anonymousUs z$VCPythonEngine._loaded_cpy_anonymousconstFc Cst|j} d||f} | d| | d| d| d|sT| d|pD|jd|n |dks`t|dk rt|j|||s|d krd |} n|} | d | f| d |j|d df|stn | d|| d| d|r| d| d| d|f| d| d| d| d| d|| d| d| d| d|j|| d|j|<| d| dS)Nz _cffi_%s_%szstatic int %s(PyObject *lib)r)z PyObject *o;z int res;z %s;z irrz&z i = (%s);z o = %s;r8z variable typez! o = _cffi_from_c_int_const(%s);z if (o == NULL)z return -1;z {z PyObject *o1 = o;z8 o = Py_BuildValue("On", o1, (Py_ssize_t)sizeof(%s));z Py_DECREF(o1);z if (o == NULL)z return -1;z }z- res = PyObject_SetAttrString(lib, "%s", o);z Py_DECREF(o);z if (res < 0)z return %s;z(lib)r*)rrfr}_check_int_constant_valuer|r+) r is_intr#r%categoryvartpdelayedsize_too check_valuer/funcnameZrealexprr r r r^sN           z"VCPythonEngine._generate_cpy_constcCs&t|tjo|j}|s"|j|dS)N)r!rr"rer&)r r%r#rr r r "_generate_cpy_constant_collecttypesz1VCPythonEngine._generate_cpy_constant_collecttypecCs&t|tjo|j}|j|||dS)N)r!rr"rer)r r%r#rr r r _generate_cpy_constant_declsz*VCPythonEngine._generate_cpy_constant_declcCs|j}|dkr"|d|||fn|d|||f|d|d||d||d|d||d |d |d |||f|d |d dS)Nrz& if ((%s) > 0 || (long)(%s) != %dL) {z1 if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {z char buf[64];z if ((%s) <= 0)z. snprintf(buf, 63, "%%ld", (long)(%s));z elsez7 snprintf(buf, 63, "%%lu", (unsigned long)(%s));z) PyErr_Format(_cffi_VerificationError,z6 "%s%s has the real value %s, not %s",z( "%s", "%s", buf, "%d");z return -1;z })r)r r#r6Z err_prefixr/r r r rs$  z(VCPythonEngine._check_int_constant_valuecCs|jdd}d||fS)N$Z___D_z _cffi_e_%s_%s)rg)r rr#r r r _enum_funcnames zVCPythonEngine._enum_funcnameenumcCs|jr*x|jD]}|jd|ddqWdS|j||}|j}|d||dx,t|j|jD]\}}|j||d|q`W|d|jd|d|jd<|d |dS) NTF)rzstatic int %s(PyObject *lib)r)z enum %s: z return %s;z(lib)r*) r enumeratorsrrrzip enumvaluesrr+)r r%r#r enumeratorrr/ enumvaluer r r rs   z&VCPythonEngine._generate_cpy_enum_declcs.|jr*fdd|jD}t||_d|_dS)Ncsg|]}t|qSr )rZ)r4r)rBr r r7sz4VCPythonEngine._loading_cpy_enum..T)rrtuplerZpartial_resolved)r r%r#rBrr )rBr rs    z VCPythonEngine._loading_cpy_enumcCs,x&t|j|jD]\}}t|||qWdS)N)rrrr)r r%r#rBrCrrr r r rszVCPythonEngine._loaded_cpy_enumcCs&|dkrd}n|}|jd||ddS)Nz...T)r)r)r r%r#rr r r _generate_cpy_macro_declsz'VCPythonEngine._generate_cpy_macro_declcCs2t|tjrtj|j}n tj|}|j|dS)N)r!rrurhrvr&)r r%r#tp_ptrr r r "_generate_cpy_variable_collecttypes  z1VCPythonEngine._generate_cpy_variable_collecttypecCsTt|tjr4tj|j}|jd||||jdkdntj|}|jd||dddS)NFz...)rrrz)r)r!rrurhrvrr)r r%r#rr r r _generate_cpy_variable_decls     z*VCPythonEngine._generate_cpy_variable_declc st||}t|tjr|jdkr~t|ts.t|\}}|jj|j }t ||jj |\}} | dkrtt d||j f|j |}|jdk r|jj|} |jj| |}t|||dS|t||fdd} fdd} tt||t| | t|jj|dS)Nz...rz/bad size: %r does not seem to be an array of %scsdS)Nrr )rC)ptrr r gettersz3VCPythonEngine._loaded_cpy_variable..gettercs |d<dS)Nrr )rCr6)rr r settersz3VCPythonEngine._loaded_cpy_variable..setter)rZr!rrurrr}rrMrvdivmodrrZresolve_lengthcastrdelattrrpropertyr:append) r r%r#rBrCr6sizeZ BItemTyperrestZBArrayrrr )rr _loaded_cpy_variables.          z#VCPythonEngine._loaded_cpy_variablecCs4|j}|d|d|d|jd|ddS)Nz,static int _cffi_setup_custom(PyObject *lib)r)z return %s;Tr*)rr+)r r/r r r r-s z%VCPythonEngine._generate_setup_custom)r)N)NrNTFN)r)r)Lr?r@rAZ _class_keyZ_gen_python_modulerrrrrr r&r1rUrWrrHrarbrqrtrir|Z!_generate_cpy_typedef_collecttypeZ_generate_cpy_typedef_declZ_generate_cpy_typedef_methodZ_loading_cpy_typedefZ_loaded_cpy_typedefrrrZ_loading_cpy_functionrZ _generate_cpy_struct_collecttyperrrrZ_generate_cpy_union_collecttyperrrrrrrrZ#_generate_cpy_anonymous_collecttyperrrrrrrZ_generate_cpy_constant_methodZ_loading_cpy_constantZ_loaded_cpy_constantrrrZ_generate_cpy_enum_collecttypeZ_generate_cpy_enum_methodrrrZ_generate_cpy_macro_collecttypeZ_generate_cpy_macro_methodZ_loading_cpy_macroZ_loaded_cpy_macrorrZ_generate_cpy_variable_methodZ_loading_cpy_variablerr-r r r r r s [ : $ I  3"  .   "rat# #include #include /* this block of #ifs should be kept exactly identical between c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py and cffi/_cffi_include.h */ #if defined(_MSC_VER) # include /* for alloca() */ # if _MSC_VER < 1600 /* MSVC < 2010 */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; typedef __int8 int_least8_t; typedef __int16 int_least16_t; typedef __int32 int_least32_t; typedef __int64 int_least64_t; typedef unsigned __int8 uint_least8_t; typedef unsigned __int16 uint_least16_t; typedef unsigned __int32 uint_least32_t; typedef unsigned __int64 uint_least64_t; typedef __int8 int_fast8_t; typedef __int16 int_fast16_t; typedef __int32 int_fast32_t; typedef __int64 int_fast64_t; typedef unsigned __int8 uint_fast8_t; typedef unsigned __int16 uint_fast16_t; typedef unsigned __int32 uint_fast32_t; typedef unsigned __int64 uint_fast64_t; typedef __int64 intmax_t; typedef unsigned __int64 uintmax_t; # else # include # endif # if _MSC_VER < 1800 /* MSVC < 2013 */ # ifndef __cplusplus typedef unsigned char _Bool; # endif # endif #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif #endif #if PY_MAJOR_VERSION < 3 # undef PyCapsule_CheckExact # undef PyCapsule_GetPointer # define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule)) # define PyCapsule_GetPointer(capsule, name) \ (PyCObject_AsVoidPtr(capsule)) #endif #if PY_MAJOR_VERSION >= 3 # define PyInt_FromLong PyLong_FromLong #endif #define _cffi_from_c_double PyFloat_FromDouble #define _cffi_from_c_float PyFloat_FromDouble #define _cffi_from_c_long PyInt_FromLong #define _cffi_from_c_ulong PyLong_FromUnsignedLong #define _cffi_from_c_longlong PyLong_FromLongLong #define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong #define _cffi_from_c__Bool PyBool_FromLong #define _cffi_to_c_double PyFloat_AsDouble #define _cffi_to_c_float PyFloat_AsDouble #define _cffi_from_c_int_const(x) \ (((x) > 0) ? \ ((unsigned long long)(x) <= (unsigned long long)LONG_MAX) ? \ PyInt_FromLong((long)(x)) : \ PyLong_FromUnsignedLongLong((unsigned long long)(x)) : \ ((long long)(x) >= (long long)LONG_MIN) ? \ PyInt_FromLong((long)(x)) : \ PyLong_FromLongLong((long long)(x))) #define _cffi_from_c_int(x, type) \ (((type)-1) > 0 ? /* unsigned */ \ (sizeof(type) < sizeof(long) ? \ PyInt_FromLong((long)x) : \ sizeof(type) == sizeof(long) ? \ PyLong_FromUnsignedLong((unsigned long)x) : \ PyLong_FromUnsignedLongLong((unsigned long long)x)) : \ (sizeof(type) <= sizeof(long) ? \ PyInt_FromLong((long)x) : \ PyLong_FromLongLong((long long)x))) #define _cffi_to_c_int(o, type) \ ((type)( \ sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \ : (type)_cffi_to_c_i8(o)) : \ sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \ : (type)_cffi_to_c_i16(o)) : \ sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \ : (type)_cffi_to_c_i32(o)) : \ sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \ : (type)_cffi_to_c_i64(o)) : \ (Py_FatalError("unsupported size for type " #type), (type)0))) #define _cffi_to_c_i8 \ ((int(*)(PyObject *))_cffi_exports[1]) #define _cffi_to_c_u8 \ ((int(*)(PyObject *))_cffi_exports[2]) #define _cffi_to_c_i16 \ ((int(*)(PyObject *))_cffi_exports[3]) #define _cffi_to_c_u16 \ ((int(*)(PyObject *))_cffi_exports[4]) #define _cffi_to_c_i32 \ ((int(*)(PyObject *))_cffi_exports[5]) #define _cffi_to_c_u32 \ ((unsigned int(*)(PyObject *))_cffi_exports[6]) #define _cffi_to_c_i64 \ ((long long(*)(PyObject *))_cffi_exports[7]) #define _cffi_to_c_u64 \ ((unsigned long long(*)(PyObject *))_cffi_exports[8]) #define _cffi_to_c_char \ ((int(*)(PyObject *))_cffi_exports[9]) #define _cffi_from_c_pointer \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10]) #define _cffi_to_c_pointer \ ((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11]) #define _cffi_get_struct_layout \ ((PyObject *(*)(Py_ssize_t[]))_cffi_exports[12]) #define _cffi_restore_errno \ ((void(*)(void))_cffi_exports[13]) #define _cffi_save_errno \ ((void(*)(void))_cffi_exports[14]) #define _cffi_from_c_char \ ((PyObject *(*)(char))_cffi_exports[15]) #define _cffi_from_c_deref \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16]) #define _cffi_to_c \ ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17]) #define _cffi_from_c_struct \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18]) #define _cffi_to_c_wchar_t \ ((wchar_t(*)(PyObject *))_cffi_exports[19]) #define _cffi_from_c_wchar_t \ ((PyObject *(*)(wchar_t))_cffi_exports[20]) #define _cffi_to_c_long_double \ ((long double(*)(PyObject *))_cffi_exports[21]) #define _cffi_to_c__Bool \ ((_Bool(*)(PyObject *))_cffi_exports[22]) #define _cffi_prepare_pointer_call_argument \ ((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23]) #define _cffi_convert_array_from_object \ ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24]) #define _CFFI_NUM_EXPORTS 25 typedef struct _ctypedescr CTypeDescrObject; static void *_cffi_exports[_CFFI_NUM_EXPORTS]; static PyObject *_cffi_types, *_cffi_VerificationError; static int _cffi_setup_custom(PyObject *lib); /* forward */ static PyObject *_cffi_setup(PyObject *self, PyObject *args) { PyObject *library; int was_alive = (_cffi_types != NULL); (void)self; /* unused */ if (!PyArg_ParseTuple(args, "OOO", &_cffi_types, &_cffi_VerificationError, &library)) return NULL; Py_INCREF(_cffi_types); Py_INCREF(_cffi_VerificationError); if (_cffi_setup_custom(library) < 0) return NULL; return PyBool_FromLong(was_alive); } static int _cffi_init(void) { PyObject *module, *c_api_object = NULL; module = PyImport_ImportModule("_cffi_backend"); if (module == NULL) goto failure; c_api_object = PyObject_GetAttrString(module, "_C_API"); if (c_api_object == NULL) goto failure; if (!PyCapsule_CheckExact(c_api_object)) { PyErr_SetNone(PyExc_ImportError); goto failure; } memcpy(_cffi_exports, PyCapsule_GetPointer(c_api_object, "cffi"), _CFFI_NUM_EXPORTS * sizeof(void *)); Py_DECREF(module); Py_DECREF(c_api_object); return 0; failure: Py_XDECREF(module); Py_XDECREF(c_api_object); return -1; } #define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num)) /**********/ ) rFrrrrSrrNrr,r r r r s  u__pycache__/backend_ctypes.cpython-36.pyc000064400000115554152344566040014404 0ustar003 MZf@sddlZddlZddlZddlZddlmZejdkr sr c@s eZdZdS) CTypesTypeN)__name__ __module__ __qualname__rrrrr sr c@seZdZeZdgZdZddZeddZe ddZ ed d Z ed d Z e d dZ ed5ddZeddZddZddZd6ddZddZeddZdd Zed!d"Zd#d$Zed%d&Zd'd(Zd)d*Zed+Zed,Zed-Zed.Zed/Zed0Z d1d2Z!d3d4Z"dS)7 CTypesData __weakref__zcGstd|jfdS)Nzcannot instantiate %r) TypeError __class__)selfargsrrr__init__szCTypesData.__init__cCstd|jfdS)Nz+expected a pointer or array ctype, got '%s')r _get_c_name)clsinitrrr_newpszCTypesData._newpcCstdS)N)r)valuerrr _to_ctypesszCTypesData._to_ctypesc Gs^y |j}Wn"tk r,td|fYnX|rR|j|}t||sZ|j|}n|j}|S)Nzcannot create an instance of %r)_ctypeAttributeErrorrr isinstance)rrctyperesrrr_arg_to_ctypes s    zCTypesData._arg_to_ctypescCs|dkr|jS|j|SdS)N)r )rrrrr_create_ctype_obj.szCTypesData._create_ctype_objcCstdS)N)r)Z ctypes_valuerrr _from_ctypes5szCTypesData._from_ctypescCs|jjd|S)Nz &) _reftypenamereplace)r replace_withrrrr9szCTypesData._get_c_namecCs*d|jf|_d|jf|_d|_dS)Nz CData<%s>ffi)rr r r )rrrr _fix_class=szCTypesData._fix_classcCstdS)N)NotImplementedError)rrrr _get_own_reprCszCTypesData._get_own_reprcCs8|dkr dS|dkr,|ddtjtj>7}d|SdS)NrZNULLrz0x%x)ctypessizeofc_void_p)raddressrrr _addr_reprFs zCTypesData._addr_reprNcCs|j}d|p|j|fS)Nz )r*r)rc_nameZownrrr__repr__NszCTypesData.__repr__cCs8|dkrtd|jfntd|j|jfdS)Nzcannot convert %r to an addresszcannot convert %r to %r)rr)rBClassrrr_convert_to_addressRs zCTypesData._convert_to_addresscCs tj|jS)N)r,r-r)rrrr _get_sizeZszCTypesData._get_sizecCs tj|jS)N)r,r-r)rrrr_get_size_of_instance^sz CTypesData._get_size_of_instancecCstd|jfdS)Nzcannot cast to %r)rr)rsourcerrr _cast_fromaszCTypesData._cast_fromcCs |jdS)N)r4)rrrr_cast_to_integereszCTypesData._cast_to_integercCs tj|jS)N)r,Z alignmentr)rrrr _alignmenthszCTypesData._alignmentcCstd|jdS)Nz#cdata %r does not support iteration)rr)rrrr__iter__lszCTypesData.__iter__cs tt|fdd}||_|S)Ncszt|t }t|to t|t }|r@|r@|jd|jdS|sH|rLtSt|tr\|j}t|trl|j}||SdS)N)rCTypesGenericPrimitiverr4NotImplemented_value)rotherZv_is_ptrZw_is_ptr)cmpfuncrrcmprs       z!CTypesData._make_cmp..cmp)getattroperatorZ func_name)namerAr)r@r _make_cmpps  zCTypesData._make_cmp__eq____ne____lt____le____gt____ge__cCst|jdS)N)hashr4)rrrr__hash__szCTypesData.__hash__cCstd|fdS)Nz string(): %r)r)rmaxlenrrr _to_stringszCTypesData._to_string)r#)N)#r r r r Z __metaclass__ __slots__r classmethodr staticmethodrr r!r"rr(r*r0r2r4r5r6r8r9r:r;rErFrGrHrIrJrKrMrOrrrrrs>           rc@s eZdZgZddZddZdS)r<cCs t|jS)N)rLr>)rrrrrMszCTypesGenericPrimitive.__hash__cCst|j|jS)N)reprr"r>)rrrrr*sz$CTypesGenericPrimitive._get_own_reprN)r r r rPrMr*rrrrr<sr<c@s,eZdZgZeddZddZddZdS)CTypesGenericArraycCs||S)Nr)rrrrrrszCTypesGenericArray._newpccs$xtt|D]}||VqWdS)N)xrangelen)rirrrr;szCTypesGenericArray.__iter__cCs|jtj|jS)N)r0r, addressof_blob)rrrrr*sz CTypesGenericArray._get_own_reprN)r r r rPrQrr;r*rrrrrTs rTc@seZdZddgZdZdZeddZeddZed d Z d d Z d dZ ddZ e Z eddZeddZeddZddZdS)CTypesGenericPtr_address _as_ctype_ptrFpointercCs||S)Nr)rrrrrrszCTypesGenericPtr._newpcCsV|dkrd}n>t|tr"|j}n*t|ttfr6|}ntd|t|jf|j|S)Nrzbad type for cast to %r: %r) rrr9intlongrtyper _new_pointer_at)rr7r/rrrr8s  zCTypesGenericPtr._cast_fromcCs$|j|}||_tj||j|_|S)N)__new__r[r,castrr\)rr/rrrrras z CTypesGenericPtr._new_pointer_atc Cs&y |j|jStk r dSXdS)Nz???)r0r[r)rrrrr*s zCTypesGenericPtr._get_own_reprcCs|jS)N)r[)rrrrr9sz!CTypesGenericPtr._cast_to_integercCs t|jS)N)boolr[)rrrr __nonzero__szCTypesGenericPtr.__nonzero__cCs4t|tstdt|j|j|}tj||jS)Nzunexpected %s object) rrrr`r r4r,rcr)rrr/rrrrs  zCTypesGenericPtr._to_ctypescCstj|tjjpd}|j|S)Nr)r,rcr.rra)r ctypes_ptrr/rrrr"szCTypesGenericPtr._from_ctypescCs|r|j|j|_dS)N)rcontents)rrfrrrr _initializeszCTypesGenericPtr._initializecCs0||jdfks|js|jr |jStj||SdS)N)r_automatic_castsr[rr4)rr3rrrr4sz$CTypesGenericPtr._convert_to_addressN)r r r rPrikindrQrr8rar*r9re__bool__rr"rhr4rrrrrZs     rZc@s\eZdZdgZeddZddZeddZdd Zed d Z ed d Z dddZ dS)CTypesBaseStructOrUnionrYcCstd|fdS)Nz!cannot instantiate opaque type %s)r)rrrrrr!sz)CTypesBaseStructOrUnion._create_ctype_objcCs|jtj|jS)N)r0r,rXrY)rrrrr*sz%CTypesBaseStructOrUnion._get_own_reprcCst|j|jS)N)rBroffset)r fieldnamerrr _offsetofsz!CTypesBaseStructOrUnion._offsetofcCs.t|dd|jkrtj|jStj||SdS)N_BItem)rBrr,rXrYrr4)rr3rrrr4s z+CTypesBaseStructOrUnion._convert_to_addresscCs|j|}||_|S)N)rbrY)rZctypes_struct_or_unionrrrrr"s z$CTypesBaseStructOrUnion._from_ctypescCs|jS)N)rY)rrrrrr sz"CTypesBaseStructOrUnion._to_ctypesNcCstj||p|jdS)Nz &)rr2r)rr1rrrr2 sz CTypesBaseStructOrUnion.__repr__)N) r r r rPrQr!r*ror4r"rr2rrrrrls    rlc@seZdZejejejejejej ej ej ej ej ejejejejdZxfdMD]^ZejeeZeeedde<eejejkreeed <eejejkrLeeed <qLWxtdND]lZejeeZeeedde<eejejkreeed<eeed<eejejkreeed<qWddZddZddZdOddZddZdd Zd!d"Zd#d$Zd%d&Z d'd(Z!d)d*Z"dRd,d-Z#d.d/Z$d0d1Z%d2d3Z&d4d5Z'dTd6d7Z(dVd8d9Z)d:d;Zdd?Z+d@dAZ,dBdCZ-dDZ.dWdEdFZ/e0Z1dGdHZ2dXdIdJZ3dYdKdLZ4dDS)Z CTypesBackend)charshortr^r_z long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long longfloatdouble_Boolunsigned long long unsigned long unsigned intunsigned short unsigned charzuint%d_tr+Z uintptr_tZsize_t long longr_r^rs signed charzint%d_tZintptr_tZ ptrdiff_tZssize_tcCs d|_d|_tj|_tj|_dS)Nr) RTLD_LAZYRTLD_NOWr, RTLD_GLOBAL RTLD_LOCAL)rrrrr7szCTypesBackend.__init__cCs ||_dS)N)r')rr'rrrset_ffi=szCTypesBackend.set_fficCsttfS)N)rr )rrrr _get_types@szCTypesBackend._get_typesrcCstj||}t||S)N)r,ZCDLL CTypesLibrary)rpathflagscdllrrr load_libraryCs zCTypesBackend.load_librarycCsGdddt}|j|S)Nc@s,eZdZgZdZeddZeddZdS)z/CTypesBackend.new_void_type..CTypesVoidzvoid &cSsdS)Nr)novaluerrrr"Ksz.CTypesVoid._from_ctypescSs |dk rtdt|jfdS)NzNone expected, got %s object)rr`r )rrrrrNsz:CTypesBackend.new_void_type..CTypesVoid._to_ctypesN)r r r rPr$rRr"rrrrr CTypesVoidHs r)rr()rrrrr new_void_typeGs zCTypesBackend.new_void_typecsdkrt|jdkr(d}n<dkr6d}n.dkrDd}ndkrRd }nd }djdkfd d |GfdddtjS)NZwchar_trrrtru signed char unsigned charbytervrdr^rcsjt|tttfrt|}nLt|tr.|j}n8t|trBt|}n$|dkrPd}ntdt |j f|S)Nrzbad type for cast to %r: %r) rr^r_rtrr9rordrr`r )r7)CTypesPrimitiverr_cast_source_to_inths     z=CTypesBackend.new_primitive_type.._cast_source_to_intcseZdZdgZZdZZddZefddZ edksHedkrbe fd d Z d d Z ed kre fdd Z dd Z edkre fdd Z dd Z edkre fdd Z dd Z ddZ e Zedksedksed krefddZedkr efddZddZnddZeZedkrFefddZedd Zefd!d"Zedkrtd#d$Zedkrd%d$Zd&S)'z9CTypesBackend.new_primitive_type..CTypesPrimitiver>z%s &cSs ||_dS)N)r>)rrrrrr}szBCTypesBackend.new_primitive_type..CTypesPrimitive.__init__cs|dkrSj|S)N)r)r)rrrrr!szKCTypesBackend.new_primitive_type..CTypesPrimitive._create_ctype_objr^rcs|}|j}||S)N)r)rr7)rrrrr8s zDCTypesBackend.new_primitive_type..CTypesPrimitive._cast_fromcSs|jS)N)r>)rrrr__int__szACTypesBackend.new_primitive_type..CTypesPrimitive.__int__rdcs$t|tttfs|}|t|S)N)rr^r_rtrd)rr7)rrrr8scSs|jS)N)r>)rrrrrsrrcs|}t|d@}||S)N)bytechr)rr7)rrrr8s cSs t|jS)N)rr>)rrrrrsrtcsNt|tr n0t|tr4t|dr*t|}q)rrrrrscSs|jS)N)r>)rrrrrszCCTypesBackend.new_primitive_type..CTypesPrimitive.__float__csnt|ttfs4t|tr"t|}ntdt|j|j|krj r^|dkr^tdn td|S)Nzinteger expected, got %srz%s: negative integerz%s: integer out of bounds) rr^r_rrr`r r OverflowError)x)r is_signedrDrrrs  zDCTypesBackend.new_primitive_type..CTypesPrimitive._to_ctypescs@t|trt|dkr|St|r*|jStdt|jdS)Nrzcharacter expected, got %s)rrrVr>rr`r )r)rrrrs  cSst|jdkS)Nr)rr>)rrrrreszECTypesBackend.new_primitive_type..CTypesPrimitive.__nonzero__cSs |jdkS)Nr)r>)rrrrrescs.t|ttttfs$tdt|j|jS)Nzfloat expected, got %s) rr^r_rtrrr`r r)r)rrrrscSs t|d|S)Nr)rB)rrrrr"szFCTypesBackend.new_primitive_type..CTypesPrimitive._from_ctypescsj||_dS)N)rr)blobr)rrrrhszECTypesBackend.new_primitive_type..CTypesPrimitive._initializecSs|jS)N)r>)rrNrrrrOszDCTypesBackend.new_primitive_type..CTypesPrimitive._to_stringcSst|jd@S)Nr)chrr>)rrNrrrrOsN)r r r rPrr$rjrrRr!rQr8rrr9rrerkr"rhrOr)rrrrkind1rDrrrwsF       r)rtru)rrr)r)PRIMITIVE_TYPESrr<r()rrDrjr)rrrrrrDrnew_primitive_typeWs$  wz CTypesBackend.new_primitive_typecs|jj}|tjdkr dn>|tjd|tjdfkrFdn|tjkrZdndGfdd d t|jjtjks|jjtjdkrd _jS) Nrrcharpz signed charz unsigned charbytepvoidpgenericcseZdZdgZdkr edg7ZZedrHejjZej jZ nej Ze e rdjdZn jdZfddZd d Zd d Zfd dZfddZdksdkrefddZdks֔dkrddZfddZZS)z1CTypesBackend.new_pointer_type..CTypesPtr_ownrZ __as_strbufrz(* &)z * &csbj|}dkr8tj|jd|_tj|j|j|_n tj||_tj|jtj j|_ d|_ dS)NrT) r!r,Zcreate_string_bufferrZ_CTypesPtr__as_strbufrcrr\r]r.r[r)rrZctypeobj)BItemrjrrr s   z:CTypesBackend.new_pointer_type..CTypesPtr.__init__cSs,t|ttfr$|j|j||jStSdS)N)rr^r_rar[ _bitem_sizer=)rr?rrr__add__sz9CTypesBackend.new_pointer_type..CTypesPtr.__add__cSsNt|ttfr$|j|j||jSt|t|krF|j|j|jStSdS)N)rr^r_rar[rr`r=)rr?rrr__sub__ s z9CTypesBackend.new_pointer_type..CTypesPtr.__sub__cs(t|ddr|dkrtj|j|S)NrFr)rB IndexErrorr"r\)rindex)rrr __getitem__)sz=CTypesBackend.new_pointer_type..CTypesPtr.__getitem__csj||j|<dS)N)rr\)rrr)rrr __setitem__.sz=CTypesBackend.new_pointer_type..CTypesPtr.__setitem__rcs4|r t|dtr tj|dSt|j|SdS)Nr)rrr,Zc_char_psuperr )rr) CTypesPtrrrrr 2sz@CTypesBackend.new_pointer_type..CTypesPtr._arg_to_ctypesrcsf|dkrtj}tj|jtjtjd}x ||krH|dkrH|d7}q*Wdjfddt|DS)Nrrrcsg|] }|qSrr).0rW)prr BszPCTypesBackend.new_pointer_type..CTypesPtr._to_string..) sysmaxsizer,rcr\POINTERc_charjoinrange)rrNnr)rrrO:s z.CTypesPtr._to_stringcs.t|ddr dtj|jjfSt|jS)NrFzowning %d bytes)rBr,r-r\rgrr*)r)rrrrr*Ds z?CTypesBackend.new_pointer_type..CTypesPtr._get_own_repr)r r r rPrprr,rrr-rr. issubclassrTrr$rrrrrrQr rOr* __classcell__r)rrrj)rrrs*          rT)r'_get_cached_btyper PrimitiveTypeZ void_typerZrir()rrgetbtyper)rrrjrnew_pointer_typesMzCTypesBackend.new_pointer_typecsdkrdndj|jj}|tjdkr.CTypesArrayrYrNrcsdkr\t|ttfr |}d}n0dkr@t|tr@t|d}nt|}t|}j||_|j|_d|_|dk r|j |j|dS)NrrrT) rr^r_rrVtuplerrYrrh)rrZlen1)rrjlengthrrrjs  z:CTypesBackend.new_array_type..CTypesArray.__init__csttr&fddttDnttt|krFtdtj|tjj }tj j }tj j }x6t D]*\}}tj||||}j|j|qxWdS)Ncsg|]}||dqS)rr)rrW)rrrr}szQCTypesBackend.new_array_type..CTypesArray._initialize..ztoo many initializers)rrrrVrrr,rcr.rrrr- enumeraterhrg)rraddrPTRitemsizerWrr)r)rrrhzs   z=CTypesBackend.new_array_type..CTypesArray._initializecSs t|jS)N)rVrY)rrrr__len__sz9CTypesBackend.new_array_type..CTypesArray.__len__cs2d|kot|jkns"tj|j|S)Nr)rVrYrr")rr)rrrrsz=CTypesBackend.new_array_type..CTypesArray.__getitem__cs6d|kot|jkns"tj||j|<dS)Nr)rVrYrr)rrr)rrrrsz=CTypesBackend.new_array_type..CTypesArray.__setitem__rrrcsj|dkrt|j}tj|jtjtjd}x ||krL|dkrL|d7}q.Wdjfddt|DS)Nrrrrcsg|] }|qSrr)rrW)rrrrszPCTypesBackend.new_array_type..CTypesArray._to_string..)rVrYr,rcrrrr)rrNrr)rrrOs  z.CTypesArray._to_stringcs,t|ddrdtj|jfSt|jS)NrFzowning %d bytes)rBr,r-rYrr*)r) CTypesArrayrrrr*s z?CTypesBackend.new_array_type..CTypesArray._get_own_reprcs.|dfks|jrtj|jStj||SdS)N)rir,rXrYrr4)rr3)rrrr4s zECTypesBackend.new_array_type..CTypesArray._convert_to_addresscsj}||_|S)N)rbrY)Z ctypes_arrayr)rrrr"s z>CTypesBackend.new_array_type..CTypesArray._from_ctypescs j|S)N)r )r)rrrr sz@CTypesBackend.new_array_type..CTypesArray._arg_to_ctypescs8t|ttfr0jtj|j|tjjSt SdS)N) rr^r_rar,rXrYr-rr=)rr?)rrrrrs  z9CTypesBackend.new_array_type..CTypesArray.__add__cSstd|jfdS)Nz casting to %r)r)r)rr7rrrr8sz.CTypesArray._cast_from)r r r rPrappendrr$_declared_length_CTPtrrrRrhrrrrOr*r4r"r rrQr8rr)rrrbracketsrjr)rrr`s(       r)rpr'rrrrTr()rrrrr)rrrrrjrrnew_array_typeQsczCTypesBackend.new_array_typecsFGddd|d|f_|Gfdddt}|j|S)Nc@s eZdZdS)z;CTypesBackend._new_struct_or_union..struct_or_unionN)r r r rrrrstruct_or_unionsrz%s_%scs(eZdZdgZZdfZZZdS)z?CTypesBackend._new_struct_or_union..CTypesStructOrUnionrYz%s &N)r r r rPrr$_kindrjr)rrDrrrCTypesStructOrUnions r)r rlr()rrjrDZbase_ctypes_classrr)rrDrr_new_struct_or_unions z"CTypesBackend._new_struct_or_unioncCs|jd|tjS)Nstruct)rr,Z Structure)rrDrrrnew_struct_typeszCTypesBackend.new_struct_typecCs|jd|tjS)Nunion)rr,ZUnion)rrDrrrnew_union_typeszCTypesBackend.new_union_typercsP|dks|dkrtdjdd|Ddd|D}dd|D}i} g} xN|D]F\} } } | dkr| j| | jf| | | <qV| j| | j| ft| | <qVW|d@rd_| _| _tfd d }|_fd d j d kt t t ||x|D]\} } } | dkr$tdt | r.cSsg|]\}}}|qSrr)rrrrrrrrscSsg|]\}}}|qSrr)rrrrrrrrsr+rcs}|dk r|||S)Nr)rresult) initializerrrr!s zACTypesBackend.complete_struct_or_union.._create_ctype_objc sr"t|dkr"tdt|ft|tsxt|ttfrBtdt|}t|tkrjtdjtt |}t j |}xd|j D]X\}}|\}}|dkst dj|}t j|j}t j|||} |j| j|qWdS)NrzPunion initializer: %d items given, but only one supported (use a dict if needed)zunion initializer: got a strz"too many values for %s initializerrz,not implemented: initializer with bit fields)rV ValueErrorrdictrunicoderrrzipr,rXitemsAssertionErrorrorrrcrhrg) rrrrrrrrmrr)rfnamesis_unionname2fieldtyperrrs*        z:CTypesBackend.complete_struct_or_union..initializerr#znested anonymous structs/unionsz1the field name %r conflicts in the ctypes backendcSs(tj|j}tj|||}|j|jS)N)r,rXrYrcr"rg)rrrrmrrrrrrgetters z6CTypesBackend.complete_struct_or_union..gettercSst|j||j|dS)N)setattrrYr)rrrrrrrsetter$sz6CTypesBackend.complete_struct_or_union..settercSs&tj|j}tj|||}|j|S)N)r,rXrYrcr")rrZ BFieldPtrrmrrrrrrr*s cSs|jt|j|S)N)r"rBrY)rrrrrrr2scSsN|j|}t|j|}t|j|||t|j|krJt|j||tddS)Nzvalue too large for bitfield)rrBrYrr)rrrrZoldvaluerrrr4s   cSst|d|S)Nr)rB)rrrrrrCscSst|d||dS)Nr)r)rrrrrrrEs)r)rrEllipsisZ_pack_Z_fields_ _bfield_typesrRr!rrrrrror,rrrTrrrpropertyr'rrZ PointerType)rrZfieldstpZ totalsizeZtotalalignmentZsflagsZbtypesZ bitfieldsZ bfield_typesZcfieldsrrrr!rrrr)rrrrrrrcomplete_struct_or_unionsf            z&CTypesBackend.complete_struct_or_unioncsNddDrjddjGfdddtjS)NcSsg|] }|jqSr)r)rBArgrrrrJsz3CTypesBackend.new_function_type..z...z, cseZdZddgZejeddfddDddiZjd fZ dfd d Z e d d Z ddZ fddZfddZZS)z:CTypesBackend.new_function_type..CTypesFunctionPtr _own_callback_namerNcSsg|] }|jqSr)r)rrrrrrRszECTypesBackend.new_function_type..CTypesFunctionPtr. use_errnoTz (* &)(%s)csddl stdtdddk r8jjndfdd}ttr~tjtj fddDdd i}nj }|||_ tj |j tj j |_|_dS) Nrz#varargs not supported for callbacksrc sg}x&t|D]\}}|j|j|qWy|}j|}Wnj}YnXttr|r|tj|tj j }nd}|S)N) rrr"r print_excrrZr,rcr.r)rZargs2argrZres2)BArgsBResulterrorr tracebackrrcallback_s  zUCTypesBackend.new_function_type..CTypesFunctionPtr.__init__..callbackcSsg|] }|jqSr)r)rrrrrrvszWCTypesBackend.new_function_type..CTypesFunctionPtr.__init__..rT)rrrBr"r!rrZr, CFUNCTYPEr.rr\rcrr[r)rrrrZcallback_ctype)rrCTypesFunctionPtr has_varargs)rrrrrVs"    zCCTypesBackend.new_function_type..CTypesFunctionPtr.__init__cSs|r tddS)NzActypes backend: not supported: initializers for function pointers)r))rfrrrrrhszFCTypesBackend.new_function_type..CTypesFunctionPtr._initializecSsPt|dd}|rD|jjd}|j|ddkr6d|}|jjd|}tj||S)Nrz(* &)rz )* )rBr$rr%rr2)rr1rWrrrr2s  zCCTypesBackend.new_function_type..CTypesFunctionPtr.__repr__cs*t|dddk rd|jfSt|jS)Nrz calling %r)rBrrr*)r)rrrrr*s zHCTypesBackend.new_function_type..CTypesFunctionPtr._get_own_reprcsr:t|tkst|td}|dt}nt|tksNtg}x&t|D]\}}|j|j|q^Wrxjt|D]^\}}|dkr|jtjdqt|t st ddt|t |j f|j|j|qW|j |}j|S)NrzKargument %d passed in the variadic part needs to be a cdata object (got %s)r)rVrrrr rr,r.rrrr`r r\r")rrZ extraargsZ ctypes_argsrrrWr)rrrrr__call__s(  zCCTypesBackend.new_function_type..CTypesFunctionPtr.__call__)N)r r r rPr,rrBrrr$rrRrhr2r*rrr)rrrrnameargs)rrrOs)  r)rrrZr()rrrrr)rrrrrrnew_function_typeIs  \zCTypesBackend.new_function_typecsFttstttt|t|Gfddd|}|j|S)Ncs0eZdZgZdZfddZfddZdS)z/CTypesBackend.new_enum_type..CTypesEnumz%s &c s4|j}yd||fStk r.t|SXdS)Nz%d: %s)r>KeyErrorstr)rr)reverse_mappingrrr*s z=CTypesBackend.new_enum_type..CTypesEnum._get_own_reprc s,|j}y|Stk r&t|SXdS)N)r>rr)rrNr)rrrrOs z:CTypesBackend.new_enum_type..CTypesEnum._to_stringN)r r r rPr$r*rOr)rDrrr CTypesEnums r)rrrrrreversedr()rrDZ enumeratorsZ enumvaluesZ CTypesIntrr)rDrr new_enum_types   zCTypesBackend.new_enum_typecCstjS)N)r, get_errno)rrrrrszCTypesBackend.get_errnocCstj|dS)N)r, set_errno)rrrrrrszCTypesBackend.set_errnocCs |j|S)N)rO)rbrNrrrstringszCTypesBackend.stringcCs tddS)Nzbuffer() with ctypes backend)r))rZbptrsizerrrbufferszCTypesBackend.buffercCs,t|tr|jSt|ts t|jSdS)N)rrr6rrr5)rZcdata_or_BTyperrrr-s zCTypesBackend.sizeofcCst|tst|jS)N)rrrr:)rBTyperrralignofszCTypesBackend.alignofcCst|tst|j|S)N)rrrr)rr r7rrrnewps zCTypesBackend.newpcCs |j|S)N)r8)rr r7rrrrcszCTypesBackend.castcCs|dks t|||S)N)r)rr r7ronerrorrrrrs zCTypesBackend.callbackNc s|jdkr.ddl}Gddd|j}i|f|_|j\}|dkrpy||=Wntk rjtdYnXdSfdd}|j|j||}||k st||f|||<|S)Nrc@s$eZdZddZddZddZdS)z CTypesBackend.gcp..MyRefcSs |}||kp|dk o||kS)Nr)rr?ZmyrefrrrrFsz'CTypesBackend.gcp..MyRef.__eq__cSs ||k S)Nr)rr?rrrrGsz'CTypesBackend.gcp..MyRef.__ne__c Ss.y|jStk r(t||_|jSXdS)N)_hashrrL)rrrrrMs  z)CTypesBackend.gcp..MyRef.__hash__N)r r r rFrGrMrrrrMyRefsrzFCan remove destructor only on a object previously returned by ffi.gc()cs$j|d\}}|dk r ||dS)N)NN)pop)kcdata destructor) weak_cacherrremovesz!CTypesBackend.gcp..remove)_weakref_cache_refweakrefrefrrrctypeofr)rrrrrrrZ new_cdatar)rrgcps      zCTypesBackend.gcpcCs |j|S)N)r)rr r&rrrgetcnameszCTypesBackend.getcnamecCst|tr\|dkr"t|tr"|j}t|ts4td|j|}|tkrNtd||j |fSt|t t frt|t rz|j }t|tstd|j}|j|}|tjkrt||fStt|dS)Nrz expected a struct or union ctypeznot supported for bitfieldszexpected an array or ptr ctype)rrrrZrprlrrrror^r_rTrr5rrrr`)rr rnrrrrmrrr typeoffsetofs(       zCTypesBackend.typeoffsetofcCst|tr tjt|j|}n^t|tr\|dksDtt|jt rLt dt|j|}n"t|t rvt|j|}nt d|rtj tj tj |tj j |t|}|j|S)Nzunexpected cdata typez$expected a )rrlr,r]r`rrZrrprrTrcr.rr")rZBTypePtrrrmZptrrrr rawaddressof.s     zCTypesBackend.rawaddressof)rwrxryrzr{)r|r_r^rsr})rrr)rrrr)rr)r)r)r)N)5r r r r,rZc_shortZc_intZc_longZ c_longlongZc_byteZc_ubyteZc_ushortZc_uintZc_ulongZ c_ulonglongZc_floatZc_doubleZc_boolrrr-Z_sizer.Zc_size_trrrrrrrrrrrrrrrrrr r r rcrrrr`rrrrrrrrrqsx      `u le   % rqc@s,eZdZddZddZddZddZd S) rcCs||_||_dS)N)backendr)rrrrrrrDszCTypesLibrary.__init__cCs t|j|}|j|}||_|S)N)rBrr"r)rr rDZc_funcfuncobjrrr load_functionHs  zCTypesLibrary.load_functioncCsJy|jj|j|}Wn*tk r>}zt|WYdd}~XnX|j|S)N)rin_dllrrr)r")rr rD ctypes_objerrr read_variableNs zCTypesLibrary.read_variablecCs@|j|}|jj|j|}tjtj|tj|tj|jdS)N)rrr!rr,ZmemmoverXr-)rr rDrZnew_ctypes_objr"rrrwrite_variableUs   zCTypesLibrary.write_variableN)r r r rr r$r%rrrrrBsr)r)r,Z ctypes.utilrCrr#r version_inforrrrr^r_rrUr`r objectrr<rTrZrlrqrrrrrs,    C#7__pycache__/__init__.cpython-36.pyc000064400000000663152344566040013157 0ustar003 MZ@sBdddddgZddlmZddlmZmZmZmZdZd Z d Z d S)FFIVerificationErrorVerificationMissing CDefErrorFFIError)r)rrrrz1.11.5 z0.8.6N)rrr) __all__Zapirerrorrrrr __version__Z__version_info__Z__version_verifier_modules__r r /usr/lib64/python3.6/__init__.pys  __pycache__/error.cpython-36.pyc000064400000002273152344566040012550 0ustar003 MZ@sDGdddeZGdddeZGdddeZGdddeZdS) c@s eZdZdS)FFIErrorN)__name__ __module__ __qualname__rr/usr/lib64/python3.6/error.pyrsrc@seZdZddZdS) CDefErrorc Cs\y*|jd}|jj}|jj}d||f}Wntttfk rHd}YnXd||jdfS)Nz%s:%d: z%s%s)argsZcoordfilelineAttributeError TypeError IndexError)selfZ current_declfilenameZlinenumprefixrrr__str__s  zCDefError.__str__N)rrrrrrrrrsrc@seZdZdZdS)VerificationErrorz- An error raised when verification fails N)rrr__doc__rrrrrsrc@seZdZdZdS)VerificationMissingzl An error raised when incomplete structures are passed into cdef, but no verification has been done N)rrrrrrrrrsrN) Exceptionrrrrrrrrs __pycache__/vengine_gen.cpython-36.opt-1.pyc000064400000050405152344566040014642 0ustar003 MZ4h@sHddlZddlZddlZddlmZddlmZGdddeZdZ dS)N)model)VerificationErrorc@seZdZdZdZddZddZddZd d ZdZd d Z ddZ d[ddZ ddZ ddZ ddZddZddZeZeZeZddZeZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Z d5d6Z!d7d8Z"d9d:Z#d\d=d>Z$d?d@Z%eZ&d]dAdBZ'dCdDZ(dEdFZ)dGdHZ*dIdJZ+d^dLdMZ,d_dNdOZ-dPdQZ.dRdSZ/eZ0dTdUZ1dVdWZ2eZ3dXdYZ4d;S)`VGenericEnginegFcCs||_|j|_g|_i|_dS)N)verifierffiexport_symbols_struct_pending_verification)selfrr #/usr/lib64/python3.6/vengine_gen.py__init__szVGenericEngine.__init__cCs|jd|jdS)Nr ) setdefaultr )r kwdsr r r patch_extension_kwdssz#VGenericEngine.patch_extension_kwdscCsTxN|D]F}||}|dkr tj}x*|D]"}tjj||}tjj|r&|Sq&WqWdS)N)syspathosjoinisfile)r Z module_namerZ so_suffixesZ so_suffixbasenamedirnamefilenamer r r find_modules   zVGenericEngine.find_modulecCsdS)Nr )r r r r collect_types%szVGenericEngine.collect_typescCs|jj|ddS)N )Z_fwrite)r Zwhatr r r _prnt(szVGenericEngine._prntcCs`|j}|t||jj|jdtjdkr\tjdkr>d}nd}|jj}|d||fdS)NZdeclZwin32ZPyInit_Zinitzvoid %s%s(void) { } )r ) rcffimod_headerrZpreamble _generaterplatform version_infoZget_module_name)r prntprefixmodnamer r r write_source_to_f+s     z VGenericEngine.write_source_to_frcsjjj}tjjtjjj}|j||j dGfdddt j d}j d|d|S)NZloadingcs&eZdZZjZgZfddZdS)z/VGenericEngine.load_library..FFILibrarycsjS)N) _cffi_dir)r ) FFILibraryr r __dir__Tsz7VGenericEngine.load_library..FFILibrary.__dir__N)__name__ __module__ __qualname__Z_cffi_generic_modulerZ _cffi_ffir)r+r )r*moduler r r r*Psr*rZloaded)library) r_backendrrrcurdirrZmodulefilename load_library_loadtypes ModuleType)r flagsbackendrr0r )r*r/r r r3As  zVGenericEngine.load_librarycCs$dd|jjjjD}|j|S)NcSsg|]\}\}}||fqSr r ).0keytpZqualr r r ^sz4VGenericEngine._get_declarations..)rZ_parserZ _declarationsitemssort)r Zlstr r r _get_declarations]sz VGenericEngine._get_declarationscCsx|jD]\}}|jdd\}}yt|d||f}Wn tk rXtd|YnXy|||Wq tk r}ztj||WYdd}~Xq Xq WdS)N rz_generate_gen_%s_%sznot implemented in verify(): %r)r?splitgetattrAttributeErrorr Exceptionrattach_exception_info)r step_namenamer;kindrealnamemethoder r r r"cs zVGenericEngine._generatec Ksx||jD]p\}}|jdd\}}t|d||f}y||||f|Wq tk rx} ztj| |WYdd} ~ Xq Xq WdS)Nr@rz _%s_gen_%s)r?rArBrDrrE) r r/rFrrGr;rHrIrJrKr r r r4rs zVGenericEngine._loadcCsdS)Nr )r r;rGr r r _generate_nothing|sz VGenericEngine._generate_nothingcKsdS)Nr )r r;rGr/rr r r _loaded_noopszVGenericEngine._loaded_noopcsx|jr|jd||dS|j}t|j}g}xVGenericEngine._generate_gen_function_decl..rz *rz, voidz _cffi_f_%sr@z %s%s(%s)z result of %s{z*r = zreturn z %s%s(%s);})ellipsis_generate_gen_constrlenargs enumerate isinstancer StructOrUnionappendzipresultinsertrO void_typerr abiZVoidType)r r;rGr%ZnumargsZargnamesirPZ indirectionZarglistZtpresult wrappernamerbZfuncdeclZ result_coder )rRr _generate_gen_function_declsH      z*VGenericEngine._generate_gen_function_declcCs`|jr|jd|||}n&g}|}tdd|jDsDt|jtjrg}xDt|jD]6\} } t| tjrtj | } |j | | f|j | qTW|j} t| tjr| j dkrt d| j ftj | } |jd| |jdd| ftj} tjt|| |j}|jj|} d|} |j| | }x"|D]\} } |j|| | |}q"Wt|||t|jj |dS)NFcss|]}t|tjVqdS)N)r[rr\)r9typr r r sz6VGenericEngine._loaded_gen_function..z*'%s' is used as result type, but is opaquerr_z _cffi_f_%s)rV_load_constantanyrYr[r_rr\rZ PointerTyper]Zfldtypes TypeError _get_c_namer`raZFunctionPtrTypetupler_get_cached_btype load_function_make_struct_wrappersetattrrPr))r r;rGr/r0Z newfunctionZ indirectionsbase_tpZ indirect_argsrcrfZindirect_resultBFuncrdr r r _loaded_gen_functions>             z#VGenericEngine._loaded_gen_functioncsP|jj|jj|dkr4|jfdd}nfdd}||_|S)Nr_cs j}|f||dS)Nr)new)rYres)BTyperoldfuncr r newfuncs z4VGenericEngine._make_struct_wrapper..newfunccs6|dj|f|dd}|S)Nr)Znewp)rY)rwr8rcrxr r rys.)rr1rnZ_cffi_base_type)r rxrcr;rrryr )rwr8rrcrxr rps z#VGenericEngine._make_struct_wrappercCs|j|d|dS)Nstruct)_generate_struct_or_union_decl)r r;rGr r r _generate_gen_struct_declsz(VGenericEngine._generate_gen_struct_declcCs|j|d||dS)Nrz)_loading_struct_or_union)r r;rGr/r r r _loading_gen_structsz"VGenericEngine._loading_gen_structcKs|j|dS)N)_loaded_struct_or_union)r r;rGr/rr r r _loaded_gen_structsz!VGenericEngine._loaded_gen_structcCs|j|d|dS)Nunion)r{)r r;rGr r r _generate_gen_union_declsz'VGenericEngine._generate_gen_union_declcCs|j|d||dS)Nr)r})r r;rGr/r r r _loading_gen_unionsz!VGenericEngine._loading_gen_unioncKs|j|dS)N)r)r r;rGr/rr r r _loaded_gen_unionsz VGenericEngine._loaded_gen_unionc Cs|jdkrdSd||f}d||f}d||fj}|j}|d||f|d|d|dx|jD]\}} } } t| tjr| js| dkr|d |qny$|d | jd d || d |fWqnt k r} z|dt | WYdd} ~ XqnXqnW|d|j j ||d|f|d|d||d|d||dxt|jD]h\}} } } | dkrvq\|d||ft| tj r| jdkr|d| jn|d||fq\W|d|d|d|d|d|f|d|dS)Nz_cffi_check_%s_%sz_cffi_layout_%s_%sz%s %szstatic void %s(%s *p)rTz8 /* only to generate compile-time warnings or errors */z (void)p;rz (void)((p->%s) << 1);z { %s = &p->%s; (void)tmp; }z*tmpzfield %r)Zqualsz /* %s */rUzintptr_t %s(intptr_t i)z, struct _cffi_aligncheck { char x; %s y; };z static intptr_t nums[] = {z sizeof(%s),z) offsetof(struct _cffi_aligncheck, y),z offsetof(%s, %s),z 0, /* %s */z sizeof(((%s *)0)->%s),z -1z };z return nums[i];z3 /* the next line is not executed, but compiled */z %s(0);)fldnamesstripr enumfieldsr[r PrimitiveTypeis_integer_typerOrstrr r] ArrayTypelengthrl) r r;r&rGZ checkfuncnamelayoutfuncnamecnamer%fnameftypefbitsizefqualrKr r r r{sT    &    z-VGenericEngine._generate_struct_or_union_declcCs|jdkrdSd||f}|jjdd}|j||}g}d} x(|| } | dkrRP|j| | d7} q@Wt|tjr|jr|d} |d} |ddd} |ddd}|j | || | f|_ nd||fj }||f|j |<dS)Nz_cffi_layout_%s_%szintptr_t(*)(intptr_t)rrr z%s %s) rr_typeof_lockedror]r[rr\partialZ force_flattenZ fixedlayoutrr )r r;r&rGr/rrsfunctionlayoutZnumxZ totalsizeZtotalalignmentZfieldofsZ fieldsizerr r r r}7s,     z'VGenericEngine._loading_struct_or_unionc Cs|jdkrdS|jj|||jkrdd}|j}|j|}|jj|\}}||d|j|d||d|j|dd}x|jD]v\}} } } | dkrq||||j||d|f||ddkr|j| } |||d|j| d |f|d7}qWdS) NcSs||krtd|||fdS)Nz'%s (we have %d, but C compiler says %d))r)Z realvalueZ expectedvaluemsgr r r checkZsz5VGenericEngine._loaded_struct_or_union..checkrzwrong total sizerzwrong total alignmentrzwrong offset for field %rzwrong size for field %r) rrrnr popsizeofZalignofrZoffsetof) r r;rrZBStructrrrcrrrrZBFieldr r r rSs,        z&VGenericEngine._loaded_struct_or_unioncCs.t|tjr|j||dn|j|d|dS)Nr)r[rEnumType_generate_gen_enum_declr{)r r;rGr r r _generate_gen_anonymous_declus z+VGenericEngine._generate_gen_anonymous_declcCs2t|tjr|j|||dn|j|d||dS)Nr)r[rr_loading_gen_enumr})r r;rGr/r r r _loading_gen_anonymous{s z%VGenericEngine._loading_gen_anonymouscKs.t|tjr |j|||f|n |j|dS)N)r[rr_loaded_gen_enumr)r r;rGr/rr r r _loaded_gen_anonymouss z$VGenericEngine._loaded_gen_anonymousNconstc Cs |j}d||f}|jj||dk rX|d||d|j|||d|dn|r|d||d|d|f|d|f|dnj|d krd }nd }d } |d krt|tjrd } d }||jd| |f||d|d||f|d|dS)Nz _cffi_%s_%szint %s(char *out_error)rTz return 0;rUzint %s(long long *out_value)z *out_value = (long long)(%s);z return (%s) <= 0;var&rrzconst *z %s%s(void)z return (%s%s);)rr r]_check_int_constant_valuer[rr\rO) r is_intrGr;category check_valuer%funcnameZ ampersandZextrar r r rWs6       z"VGenericEngine._generate_gen_constcCs&t|tjo|j}|j|||dS)N)r[rrrrW)r r;rGrr r r _generate_gen_constant_declsz*VGenericEngine._generate_gen_constant_declcCsd|}|dk r"|j|||}n|r|jjdd}|jjdd} |j| |} |jj|} | | } t| d}|dkr| r|jjdd} |dd|jj| >7}nXd}t|tj rd |}|jj|j ||d} |j| |} | }t|tj r|d}|S) Nz_cffi_const_%sz long long*rzint(*)(long long*)z long longrz (*)(void)rN) _load_known_int_constantrrroruintrr[rr\rO)r rr;rGr/rrvaluerwrsrpnegativeZ BLongLongZ fntypeextrar r r rhs.      zVGenericEngine._load_constantcCsDt|tjo|j}|j||||}t|||t|jj|dS)N) r[rrrrhrqrPr)r])r r;rGr/r0rrr r r _loaded_gen_constants z#VGenericEngine._loaded_gen_constantcCs|j}|dkr"|d|||fn|d|||f|d|d||d||d|d||d |d |dd |f|d |d dS)Nrz& if ((%s) > 0 || (long)(%s) != %dL) {z1 if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {z char buf[64];z if ((%s) <= 0)z) sprintf(buf, "%%ld", (long)(%s));z elsez2 sprintf(buf, "%%lu", (unsigned long)(%s));z: sprintf(out_error, "%s has the real value %s, not %s",z "%s", buf, "%d");dz return -1;z })r)r rGrr%r r r rs   z(VGenericEngine._check_int_constant_valuecCsr|jjdd}|jjdd}|j||}|jj|d}||dkrn|jj|}tjdkrft|d}t|dS)Nzchar[]rz int(*)(char*)r zutf-8)r ) rrrorustringrr$rr)r r/rrwrsrrerrorr r r rs     z'VGenericEngine._load_known_int_constantcCs|jdd}d||fS)N$Z___D_z _cffi_e_%s_%s)replace)r r&rGr r r _enum_funcnames zVGenericEngine._enum_funcnameenumcCs|jr&x|jD]}|jd|qWdS|j||}|jj||j}|d||dx&t|j|jD]\}}|j ||qhW|d|d|dS)NTzint %s(char *out_error)rTz return 0;rU) r enumeratorsrWrr r]rr^ enumvaluesr)r r;rGr& enumeratorrr% enumvaluer r r rs    z&VGenericEngine._generate_gen_enum_declcsLjr0fddjD}t|_d_nj||}j|dS)Ncsg|]}jd|qS)T)rh)r9r)r/r r;r r r<sz4VGenericEngine._loading_gen_enum..T)rrrmrZpartial_resolvedrr)r r;rGr/r&rrr )r/r r;r rs   z VGenericEngine._loading_gen_enumcCs<x6t|j|jD]$\}}t|||t|jj|qWdS)N)r^rrrqrPr)r])r r;rGr/r0rrr r r rs zVGenericEngine._loaded_gen_enumcCs&|dkrd}n|}|jd||ddS)Nz...T)r)rW)r r;rGrr r r _generate_gen_macro_decl sz'VGenericEngine._generate_gen_macro_declcCsF|dkrd}n|}|jd||||d}t|||t|jj|dS)Nz...T)r)rhrqrPr)r])r r;rGr/r0rrr r r _loaded_gen_macro)s  z VGenericEngine._loaded_gen_macrocCst|tjrx|jdkr\|j}d|f}|jj||d||d|d|f|dtj|j}|j d||ntj|}|j d||dd dS) Nz...z_cffi_sizeof_%szsize_t %s(void)rTz return sizeof(%s);rUFr)r) r[rrrrr r]rjitemrW)r r;rGr%rtp_ptrr r r _generate_gen_variable_decl6s       z*VGenericEngine._generate_gen_variable_declcs^t|tjr|jdkrd|f}|jjdd}|j||}|}|jj|j} t ||jj | \} } | dkrt d||jf|j | }tj |j} |jd| ||} |jdk r|jj|}|jj|| } t||| t|jj|dSd|}|jj|jd|d}|j||}|fd d }fd d }tt||t||t|jj|dS) Nz...z_cffi_sizeof_%szsize_t(*)(void)rz/bad size: %r does not seem to be an array of %sFz _cffi_var_%sz *(*)(void)csdS)Nrr )r0)ptrr r gettergsz3VGenericEngine._loaded_gen_variable..gettercs |d<dS)Nrr )r0r)rr r setterisz3VGenericEngine._loaded_gen_variable..setter)r[rrrrrrornrdivmodrrZresolve_lengthrjrhcastrqrPr)r]rOproperty)r r;rGr/r0rrsrsizeZ BItemTyperrestrrZBArrayrrr )rr _loaded_gen_variableHs:            z#VGenericEngine._loaded_gen_variable)r)r)NrN)N)r)r)5r,r-r.Z _class_keyZ_gen_python_modulerrrrrr(r3r?r"r4rLrMZ_generate_gen_typedef_declZ_loading_gen_typedefZ_loaded_gen_typedefreZ_loading_gen_functionrtrpr|r~rrrrr{r}rrrrrWrZ_loading_gen_constantrhrrrrrrrrZ_loading_gen_macrorrZ_loading_gen_variablerr r r r r sf    -$1"  $      rad #include #include #include #include #include /* XXX for ssize_t on some platforms */ /* this block of #ifs should be kept exactly identical between c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py and cffi/_cffi_include.h */ #if defined(_MSC_VER) # include /* for alloca() */ # if _MSC_VER < 1600 /* MSVC < 2010 */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; typedef __int8 int_least8_t; typedef __int16 int_least16_t; typedef __int32 int_least32_t; typedef __int64 int_least64_t; typedef unsigned __int8 uint_least8_t; typedef unsigned __int16 uint_least16_t; typedef unsigned __int32 uint_least32_t; typedef unsigned __int64 uint_least64_t; typedef __int8 int_fast8_t; typedef __int16 int_fast16_t; typedef __int32 int_fast32_t; typedef __int64 int_fast64_t; typedef unsigned __int8 uint_fast8_t; typedef unsigned __int16 uint_fast16_t; typedef unsigned __int32 uint_fast32_t; typedef unsigned __int64 uint_fast64_t; typedef __int64 intmax_t; typedef unsigned __int64 uintmax_t; # else # include # endif # if _MSC_VER < 1800 /* MSVC < 2013 */ # ifndef __cplusplus typedef unsigned char _Bool; # endif # endif #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif #endif ) rrr5rrrrobjectrr!r r r r s  __pycache__/verifier.cpython-36.pyc000064400000021547152344566040013237 0ustar003 MZ+@sddlZddlZddlZddlZddlZddlmZddlmZddlm Z ej dkrhddl Z ddZ nddlZd dZ ej dkrejZnGd d d ejZGd d d eZdZddZdaddZddZdddZddZddZdS)N)__version_verifier_modules__) ffiplatform)VerificationErrorcCstjjddS)N) importlib machineryEXTENSION_SUFFIXESr r /usr/lib64/python3.6/verifier.py_extension_suffixes sr cCsddtjDS)NcSs g|]\}}}|tjkr|qSr )impZ C_EXTENSION).0suffix_typer r r sz'_extension_suffixes..)r Z get_suffixesr r r r r scseZdZfddZZS)NativeIOcs(t|tr|jd}tt|j|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||_||_|s4tj| } t||} | ||_|jj | | |_ |j | | |_ |rz|rxt dndjtjddt|| g|j}tjdkr|jd}ttj|dddd@}|jd jd }ttj|d ddd@}|jd jd }d ||jj||f}td}|p0t|_tjj|j|||_ tjj|j|||_!||_"d|_#d|_$dS)NzKfeature not supported with ffi.verify(), but only with ffi.set_source(): %sz)can't specify both 'modulename' and 'tag'rzutf-8rlZ0xLr0z_cffi_%s_%s%s%sF)r)%Z_parserZ_uses_new_featurerffipreamblerZflatten_locate_engine_class_vengineZpatch_extension_kwdsflagsmake_relative_tokwds TypeErrorjoinsysversionrZ _cdefsources version_inforhexbinasciiZcrc32lstriprstripZ _class_key_get_so_suffixes_caller_dir_pycachetmpdirospathsourcefilenamemodulefilename ext_package _has_source _has_module)rr'r(r9Z modulenamer>tagforce_generic_engineZsource_extensionr+ relative_tor-Zflattened_kwdsZ vengine_classkeyZk1Zk2rr r r __init__ sD           zVerifier.__init__c Cs8|jj&|jr |dkr td|j|WdQRXdS)zkWrite the C source code. It is produced in 'self.sourcefilename', which can be tweaked beforehand.Nzsource code already written)r'_lockr?r _write_source)rfiler r r write_sourceHs  zVerifier.write_sourcec Cs<|jj*|jrtd|js&|j|jWdQRXdS)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'rFr@rr?rG_compile_module)rr r r compile_moduleQs  zVerifier.compile_modulec CsF|jj4|js4|j|js4|js,|j|j|jSQRXdS)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'rFr@_locate_moduler?rGrJ _load_library)rr r r load_library[s zVerifier.load_librarycCsBtjj|j}|jddd}|jdr>ttdr>|dd}|S)N.rrZ_dgettotalrefcountr$)r:r;basenamer=splitendswithhasattrr0)rrRr r r get_module_nameks  zVerifier.get_module_namec CsVtj|js0|jj|js&|jWdQRXtj|j}|j}tj ||f|j S)N) rZ_hack_at_distutilsr?r'rFrGZmaybe_relative_pathr<rV get_extensionr-)rZ sourcenamemodnamer r r rWvs  zVerifier.get_extensioncCs|jjS)N)r*Z_gen_python_module)rr r r generates_python_modulesz Verifier.generates_python_modulecs~|rztjj|rztjj||j}xTtjD]J}||kr,||}t|ttfs\t d|ffdd|D}|||<q,W|S)Nz&keyword '%s' should be a list or tuplecsg|]}tjj|qSr )r:r;r/)rfn)dirnamer r rsz-Verifier.make_relative_to..) r:r;r[copyrZLIST_OF_FILE_NAMESrlisttupler.)rr-rCrDZlstr )r[r r,s    zVerifier.make_relative_toc Cstjj|jst|jrHyt|jdddg}Wntk r>dSX|j}nd}|jj |j |t }|dkrndS||_|jj d|_ dS)N__doc__T)r:r;isfiler=r> __import__ ImportError__path__r* find_modulerVr7Z collect_typesr@)rZpkgr;filenamer r r rLs zVerifier._locate_modulec Cs$||j_z|jjWd|j`XdS)N)r*Z_fZwrite_source_to_f)rrHr r r _write_source_toszVerifier._write_source_tocCs|dk r|j|nt}|j||j}tjj|jrbt|jd}|j|k }WdQRXnd}|rt |jt|jd}|j |WdQRXd|_ dS)NrTw) rfrgetvaluer:r;existsr<openread _ensure_dirrr?)rrHfZ source_datafpZ needs_writtenr r r rGs   zVerifier._write_sourcec Csptjj|j}tj||j}ytj||j}Wnt k rHd}YnX|sft |jt j ||jd|_ dS)NFT)r:r;r[r<rcompilerWsamefiler=OSErrorrmshutilZmover@)rr9ZoutputfilenameZsamer r r rJs  zVerifier._compile_modulecCs0|js t|jdk r"|jj|jS|jjSdS)N)r@AssertionErrorr+r*rN)rr r r rMs  zVerifier._load_library)NNNr!Fr"NN)N)N)rrrrErIrKrNrVrWrYr,rLrfrGrJrMr r r r r s &      r Fc Cs|trd}|sPdtjkrd}n4y ddl}Wntk r@d}YnX|j|k rPd}|rfddlm}|jSddlm }|j SdS)NTZ__pypy__r?r) vengine_gen) vengine_cpy) _FORCE_GENERIC_ENGINEr0builtin_module_names _cffi_backendrbZ_backendr!rvZVGenericEnginerwZVCPythonEngine)r'rBrzrvrwr r r r)s       r)cCsHtrtStjjd}|r|Stjdjj}tjj tjj tjj |dS)NZ CFFI_TMPDIRr$ __pycache__) _TMPDIRr:environgetr0 _getframef_code co_filenamer;abspathr/r[)resultrer r r r8s r8cCs|adS)z:Set the temporary directory to use instead of __pycache__.N)r|)r[r r r set_tmpdirsrcCs0|pt}ytj|}Wntk r,dSX|r8d}ntdj}xd|D]\}|jjdrL|jj|sz|jjdrLytjtj j ||WqLtk rYqLXqLWtj j |dg}xn|D]f}yJxDtj|D]6}tj j ||}tj j |r|j |qtj|qWWqtk r&YqXqWdS)z}Clean up the temporary directory by removing all files in it called `_cffi_*.{c,so}` as well as the `build` subdirectory.Nz.crZ_cffi_Zbuild) r8r:listdirrrr7lower startswithrTunlinkr;r/isdirappend)r9Zkeep_soZfilelistrrZZ clean_dirdirr r r cleanup_tmpdirs2     rcCs&t}|s"tjdkrdg}ndg}|S)NZwin32z.pydz.so)r r0platform)suffixesr r r r7$s  r7cCs,tjj|}|r(tjj| r(tj|dS)N)r:r;r[rmakedirs)rer[r r r rm/s rm)rr)r)NF)r0r:r4rsior!rrerrorrr2Zimportlib.machineryrr r StringIOrBytesIOobjectr rxr)r|r8rrr7rmr r r r s*(      ?   __pycache__/cffi_opcode.cpython-36.opt-1.pyc000064400000011107152344566040014612 0ustar003 MZ\4@s<ddlmZGdddeZddZdZdZdZdZd Z d Z d Z d Z d Z dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#dZ$d Z%d Z&d Z'd!Z(d Z)d"Z*d Z+d#Z,dZ-d$Z.dZ/d%Z0dZ1d&Z2dZ3d'Z4dZ5d(Z6dZ7d)Z8dZ9d*Z:dZ;d+ZdZ?d-Z@dZAd.ZBdZCd/ZDd0ZEd1ZFd2ZGd3ZHd4ZId5ZJd6ZKd7ZLd8ZMd9ZNdDZOdEZPdFZQdGZReee!e#e%eee e"e$e&e'e(e)eJeKee*eLeMe+e,e-e.e/e0e1e2e3e4e5e6e7e8e9e:e;ee?e@eAeBeCeDeEeFeGeHeId:3ZSdZTdZUdZVdZWd"ZXeYd;d<dHDZZiZ[xDe\e]j^D]2\Z_Z`e_jadBrebe`ecre_ddCe[e`<qWdCS)I)VerificationErrorc@s,eZdZddZddZddZddZd S) CffiOpcCs||_||_dS)N)oparg)selfrrr#/usr/lib64/python3.6/cffi_opcode.py__init__szCffiOp.__init__cCs.|jdkrd|jfSt|j}d||jfS)Nz(_cffi_opcode_t)(%s)z_CFFI_OP(_CFFI_OP_%s, %s))rr CLASS_NAME)r classnamerrr as_c_exprs   zCffiOp.as_c_exprcCsn|jdkr>|jjr>t|j}|dkr6td|jft|St|jtrZtd|jft|jd>|jBS)Nz"cannot emit %r: limited to 2**31-1zcannot emit to Python: %rl) rrisdigitint OverflowErrorformat_four_bytes isinstancestrr)rvaluerrras_python_bytess   zCffiOp.as_python_bytescCstj|j|j}d||jfS)Nz(%s %s))r getrr)rr rrr__str__szCffiOp.__str__N)__name__ __module__ __qualname__r r rrrrrrrs rcCs,d|d?d@|d?d@|d?d@|d@fS)Nz\x%02X\x%02X\x%02X\x%02Xrr)Znumrrrrs    r r!#%')r r rr "$&(*+,-./01234)3charZshortrZlongz long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long longfloatZdoublez long doublezfloat _Complexzdouble _ComplexZ_BoolZwchar_tZchar16_tZchar32_tZint8_tZuint8_tZint16_tZuint16_tZint32_tZuint32_tZint64_tZuint64_tZintptr_tZ uintptr_tZ ptrdiff_tZsize_tZssize_tZ int_least8_tZ uint_least8_tZ int_least16_tZuint_least16_tZ int_least32_tZuint_least32_tZ int_least64_tZuint_least64_tZ int_fast8_tZ uint_fast8_tZ int_fast16_tZ uint_fast16_tZ int_fast32_tZ uint_fast32_tZ int_fast64_tZ uint_fast64_tZintmax_tZ uintmax_tcCsg|]}d|t|fqS)Z_CFFI_)globals).0Z_keyrrr srSF_UNIONF_CHECK_FIELDSF_PACKED F_EXTERNALF_OPAQUEZOP_NrY)rTrUrVrWrX)derrorrobjectrrZ OP_PRIMITIVEZ OP_POINTERZOP_ARRAYZ OP_OPEN_ARRAYZOP_STRUCT_UNIONZOP_ENUMZ OP_FUNCTIONZOP_FUNCTION_ENDZOP_NOOPZ OP_BITFIELDZ OP_TYPENAMEZOP_CPYTHON_BLTN_VZOP_CPYTHON_BLTN_NZOP_CPYTHON_BLTN_OZ OP_CONSTANTZOP_CONSTANT_INTZ OP_GLOBAL_VARZOP_DLOPEN_FUNCZOP_DLOPEN_CONSTZOP_GLOBAL_VAR_FZOP_EXTERN_PYTHONZ PRIM_VOIDZ PRIM_BOOLZ PRIM_CHARZ PRIM_SCHARZ PRIM_UCHARZ PRIM_SHORTZ PRIM_USHORTZPRIM_INTZ PRIM_UINTZ PRIM_LONGZ PRIM_ULONGZ PRIM_LONGLONGZPRIM_ULONGLONGZ PRIM_FLOATZ PRIM_DOUBLEZPRIM_LONGDOUBLEZ PRIM_WCHARZ PRIM_INT8Z PRIM_UINT8Z PRIM_INT16Z PRIM_UINT16Z PRIM_INT32Z PRIM_UINT32Z PRIM_INT64Z PRIM_UINT64Z PRIM_INTPTRZ PRIM_UINTPTRZ PRIM_PTRDIFFZ PRIM_SIZEZ PRIM_SSIZEZPRIM_INT_LEAST8ZPRIM_UINT_LEAST8ZPRIM_INT_LEAST16ZPRIM_UINT_LEAST16ZPRIM_INT_LEAST32ZPRIM_UINT_LEAST32ZPRIM_INT_LEAST64ZPRIM_UINT_LEAST64ZPRIM_INT_FAST8ZPRIM_UINT_FAST8ZPRIM_INT_FAST16ZPRIM_UINT_FAST16ZPRIM_INT_FAST32ZPRIM_UINT_FAST32ZPRIM_INT_FAST64ZPRIM_UINT_FAST64Z PRIM_INTMAXZ PRIM_UINTMAXZPRIM_FLOATCOMPLEXZPRIM_DOUBLECOMPLEXZ PRIM_CHAR16Z PRIM_CHAR32Z _NUM_PRIMZ _UNKNOWN_PRIMZ_UNKNOWN_FLOAT_PRIMZ_UNKNOWN_LONG_DOUBLEZ_IO_FILE_STRUCTZPRIMITIVE_TO_INDEXrTrUrVrWrXdictZG_FLAGSr listrQitems_nameZ_value startswithrrrrrrs  __pycache__/__init__.cpython-36.opt-1.pyc000064400000000663152344566040014116 0ustar003 MZ@sBdddddgZddlmZddlmZmZmZmZdZd Z d Z d S)FFIVerificationErrorVerificationMissing CDefErrorFFIError)r)rrrrz1.11.5 z0.8.6N)rrr) __all__Zapirerrorrrrr __version__Z__version_info__Z__version_verifier_modules__r r /usr/lib64/python3.6/__init__.pys  __pycache__/commontypes.cpython-36.pyc000064400000003503152344566040013771 0ustar003 MZ @sddlZddlmZddlmZiZyddlmZeeWnek rPYnXej dded<ded <x"ej j D]Z e j d rte ee <qtW[ iZd d Zd dZejdkrejedS)N)model)FFIError)_get_common_typesFILEZ_IO_FILEZ_BoolboolZ_tc Csyt|Stk rtj||}t|ts:|d}}nZ|tjjkrXtj|d}}n<|dkrpt d|fn$||krt d|f|j |\}}t|tj st ||ft|<||fSXdS)Nrzset-unicode-neededzFThe Windows type %r is only available after you call ffi.set_unicode()zUnsupported type: %r. Please look at http://cffi.readthedocs.io/en/latest/cdef.html#ffi-cdef-limitations and file an issue if you think this type should really be supported.) _CACHEKeyError COMMON_TYPESget isinstancestrr PrimitiveTypeALL_PRIMITIVE_TYPESrZparse_type_and_qualsZBaseTypeByIdentityAssertionError)parserZ commontypeZcdeclresultZqualsr#/usr/lib64/python3.6/commontypes.pyresolve_common_types&       rc CsRtjddddgtjdtjdtjtjdgd d dgdd d d d d d d d d d S)NZ_UNICODE_STRINGZLengthZ MaximumLengthZBufferzunsigned shortZwchar_trzUNICODE_STRING *zconst UNICODE_STRING *zset-unicode-needed) ZUNICODE_STRINGZPUNICODE_STRINGZPCUNICODE_STRINGZTBYTEZTCHARZLPCTSTRZPCTSTRZLPTSTRZPTSTRZPTBYTEZPTCHARrr)rZ StructTyperZ PointerTyperrrrwin_common_types7s& rZwin32)sysrerrorrr Z _cffi_backendr ImportErrorZ unknown_typerrZ_typeendswithrrrplatformupdaterrrrs&       __pycache__/setuptools_ext.cpython-36.pyc000064400000015024152344566040014516 0ustar003 MZ @snddlZddlZyeWnek r0eZYnXddZddZddZdd Zd d Z d d Z ddZ dS)NcCsddlm}||dS)Nr)DistutilsSetupError)Zdistutils.errorsr)msgrr&/usr/lib64/python3.6/setuptools_ext.pyerror s rc Cs@t|}|j}WdQRX|d7}t||d}t|||dS)N exec)openreadcompiler)filenameZglobfsrccoderrrexecfiles   rc Csddlm}t|ts*tdt|jft|}y|jd\}}Wn"t k rftd|fYnXt j j |sd}|j ddd }t j j |rd |d|f}td ||fd |d }t||y ||}Wn$tk rtd||fYnXt||s|}t||s0td|t|jft|dsJtd|f|j\} } } } |jrr| j} |j| | dkrt||| nt||| | | | dS)Nr)FFIzEargument to 'cffi_modules=...' must be a str or a list of str, not %r:z3%r must be of the form 'path/build.py:ffi_variable'./z.pyz (rewrite cffi_modules to [%r])z#%r does not name an existing file%sZ__cffi__)__name____file__z!%r: object %r not found in modulez"%r is not an FFI instance (got %r)_assigned_sourcez*%r: the set_source() method was not called)Zcffi.apir isinstance basestringrtyperstrsplit ValueErrorospathexistsreplacerKeyErrorhasattrrZ_windows_unicodecopyZ_apply_windows_unicode_add_py_module _add_c_module) distZmod_specrZbuild_file_nameZ ffi_var_nameextZ rewrittenZmod_varsffi module_namesourcesource_extensionkwdsrrradd_cffi_modulesL            r/c Cspd|krlttd rltjdkrlddl}y(t|jjdd}|dkrLd|d<Wntk rjd|d<YnX|S) a Add py_limited_api to kwds if setuptools >= 26 is in use. Do not alter the setting if it already exists. Setuptools takes care of ignoring the flag on Python 2 and PyPy. CPython itself should ignore the flag in a debugging version (by not listing .abi3.so in the extensions it supports), but it doesn't so far, creating troubles. That's why we check for "not hasattr(sys, 'gettotalrefcount')" (the 2.7 compatible equivalent of 'd' not in sys.abiflags). (http://bugs.python.org/issue28401) On Windows, it's better not to use py_limited_api until issue #355 can be resolved (by having virtualenv copy PYTHON3.DLL). See also the start of _cffi_include.h. Zpy_limited_apigettotalrefcountZwin32rNrT)r$sysplatform setuptoolsint __version__ partitionr) Extensionr.r4Zsetuptools_major_versionrrr_set_py_limited_apiIs  r9c sddlm}ddlm}ddlmddlmddlm dg}|j |j dgt ||}|f|d |d fd d |j dkrg|_ |j j|jjd |Gfd dd} | |jd <dS)Nr)r8) build_ext)mkpath)log) recompilerz $PLACEHOLDERsources)namer>cs\tjj|}jd|||dk r:|j|}|sXjd|S)Nzgenerating cffi module %rzalready up-to-date)rr joininfoZ make_c_source)Ztmpdirpre_runZc_fileupdated)r)r*r<r;r+r=r,r-rrmake_modts  z_add_c_module..make_modr:cseZdZfddZdS)z)_add_c_module..build_ext_make_modcs:jddkr,t|dd}|j|jd<j|dS)Nrz $PLACEHOLDERrB)r>getattrZ build_temprun)selfrB) base_classr)rDrrrFs z-_add_c_module..build_ext_make_mod.runN)r __module__ __qualname__rFr)rHr)rDrrbuild_ext_make_modsrK)N)Zdistutils.corer8setuptools.command.build_extr:distutils.dir_utilr; distutilsr<cffir=extendpopr9Z ext_modulesappendcmdclassget) r(r*r+r,r-r.r8r:Z allsourcesrKr) rHr)r*r<rDr;r+r=r,r-rr'gs         r'csddlmddlm}ddlm}ddlmddlm fdd|j j d |Gfd d d }||j d <|j dkrg|_ |j j |j j d |Gfd dd}||j d <dS)Nr)r;)build_py)r:)r<)r=cs>jd|tjj|j|}|s:jddS)Nzgenerating cffi module %rzalready up-to-date)rArr dirnameZmake_py_source)Zpy_filerC)r*r<r;r+r=rr generate_mods z$_add_py_module..generate_modrUcseZdZfddZdS)z)_add_py_module..build_py_make_modcs@j|jd}|dd7<tjj|jf|dS)Nrz.py)rFrrr r@Z build_lib)rG module_path)rHrWr+rrrFs  z-_add_py_module..build_py_make_mod.runN)rrIrJrFr)rHrWr+rrbuild_py_make_modsr[r:cseZdZfddZdS)z*_add_py_module..build_ext_make_modcsbj||jr^jd}dj|dd}|jd}|j|}|dd}tjj||dS)NrrXrUz.pyrYrY)rFZinplacerr@Zget_finalized_commandZget_package_dirrr )rGrZpackagerUZ package_dir file_name) base_class_2rWr+rrrFs     z._add_py_module..build_ext_make_mod.runN)rrIrJrFr)r^rWr+rrrKsrK)rMr;Zsetuptools.command.build_pyrUrLr:rNr<rOr=rSrTZ py_modulesrR)r(r*r+rUr:r[rKr)rHr^r*rWr<r;r+r=rr&s        r&cCs8|dks tt|tr|g}x|D]}t||q"WdS)N cffi_modules)AssertionErrorrrr/)r(attrvalueZ cffi_modulerrrr_s    r_) rr2r NameErrorrrrr/r9r'r&r_rrrrs  -.1__pycache__/vengine_cpy.cpython-36.opt-1.pyc000064400000101463152344566040014665 0ustar003 MZ@s@ddlZddlZddlmZddlmZGdddeZdZdS)N)model)VerificationErrorc@seZdZdZdZddZddZddZd d Zdnd d Z ddZ ddZ ddZ doddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(ZeZeZeZeZeZd)d*Zd+d,Zd-d.ZeZd/d0Z eZ!d1d2Z"d3d4Z#d5d6Z$d7d8Z%eZ&d9d:Z'd;d<Z(d=d>Z)d?d@Z*dAdBZ+dCdDZ,dEdFZ-dGdHZ.eZ/dIdJZ0dKdLZ1dMdNZ2dOdPZ3dpdSdTZ4dUdVZ5dWdXZ6eZ7eZ8eZ9dqdYdZZ:d[d\Z;drd^d_Zd`daZ?dbdcZ@dddeZAeZBeZCeZDeZEdfdgZFdhdiZGeZHeZIdjdkZJdldmZKdS)sVCPythonEnginexTcCs||_|j|_i|_i|_dS)N)verifierffi_struct_pending_verification_types_of_builtin_functions)selfrr #/usr/lib64/python3.6/vengine_cpy.py__init__ szVCPythonEngine.__init__cCsdS)Nr )r kwdsr r r patch_extension_kwdssz#VCPythonEngine.patch_extension_kwdsc CsPytj||\}}}Wntk r*dSX|dk r<|j|d|krLdS|S)Nr)imp find_module ImportErrorclose)r Z module_namepathZ so_suffixesffilenameZdescrr r r rs zVCPythonEngine.find_modulecCsi|_|jddS)NZ collecttype) _typesdict _generate)r r r r collect_types$szVCPythonEngine.collect_typescCs|jj|ddS)N )Z_fwrite)r Zwhatr r r _prnt(szVCPythonEngine._prntcCs |j|S)N)r)r typer r r _gettypenum+szVCPythonEngine._gettypenumcCs:t|tj s|jdkr6||jkr6t|j}||j|<dS)Nz long double) isinstancer PrimitiveTypenamerlen)r tpZnumr r r _do_collect_type/s    zVCPythonEngine._do_collect_typecCs|jddg|_|j}|t|||jj||jd|j||d|jd|d|d|d||jj}|jd}|d ||d |d |d ||d |d|d|d|d||d|d||d|d|d|d|d|d|f|d|d|d|d|d||d||d|d||d|d|d||d|d |d!|f|d |d"|d||d#dS)$Nz ((void)lib,0)declz&static PyMethodDef _cffi_methods[] = {methodz3 {"_cffi_setup", _cffi_setup, METH_VARARGS, NULL},z) {NULL, NULL, 0, NULL} /* Sentinel */z};Fz#if PY_MAJOR_VERSION >= 3z.static struct PyModuleDef _cffi_module_def = {z PyModuleDef_HEAD_INIT,z "%s",z NULL,z -1,z _cffi_methods,z NULL, NULL, NULL, NULLZPyMODINIT_FUNCzPyInit_%s(void){z PyObject *lib;z+ lib = PyModule_Create(&_cffi_module_def);z if (lib == NULL)z return NULL;z# if (%s < 0 || _cffi_init() < 0) {z Py_DECREF(lib);z }z return lib;}z#elsez init%s(void)z+ lib = Py_InitModule("%s", _cffi_methods);z return;z! if (%s < 0 || _cffi_init() < 0)z return;z#endif) r_chained_list_constantsrcffimod_headerrZpreambler_generate_setup_customget_module_name)r prntmodnameZ constantsr r r write_source_to_f6st          z VCPythonEngine.write_source_to_fNcsvtjzttdrtj}zvy6ttdr<|dk rsz/VCPythonEngine.load_library..csg|] }|qSr r )r4i) revmappingr r r7scs&eZdZZjZgZfddZdS)z/VCPythonEngine.load_library..FFILibrarycsjt|jS)N) _cffi_dirlist__dict__)r ) FFILibraryr r __dir__sz7VCPythonEngine.load_library..FFILibrary.__dir__N)__name__ __module__ __qualname__Z_cffi_python_modulerZ _cffi_ffir:r>r )r=moduler r r r=sr=rz0reimporting %r might overwrite older definitionsZloaded)library)r acquire_lockhasattrsysr2r3Z load_dynamicrr.Zmodulefilenamerr release_lock_loaddictritemsranger$r;mapr_get_cached_btypeobjectZ _cffi_setupwarningswarnZ_cffi_original_ffir Z_cffi_types_of_builtin_funcs)r flagsZprevious_flagseerrorlstrCrOr )r=rBr9r r load_librarys<      zVCPythonEngine.load_librarycCs$dd|jjjjD}|j|S)NcSsg|]\}\}}||fqSr r )r4r5r%Zqualr r r r7sz4VCPythonEngine._get_declarations..)rZ_parserZ _declarationsrJsort)r rTr r r _get_declarationssz VCPythonEngine._get_declarationscCsx|jD]\}}|jdd\}}yt|d||f}Wn tk rXtd|YnXy|||Wq tk r}ztj||WYdd}~Xq Xq WdS)N rz_generate_cpy_%s_%sznot implemented in verify(): %r)rWsplitgetattrAttributeErrorr Exceptionrattach_exception_info)r step_namer#r%kindrealnamer(rRr r r rs zVCPythonEngine._generatec Ksx||jD]p\}}|jdd\}}t|d||f}y||||f|Wq tk rx} ztj| |WYdd} ~ Xq Xq WdS)NrXrz _%s_cpy_%s)rWrYrZr\rr]) r rBr^rr#r%r_r`r(rRr r r rHs zVCPythonEngine._loadcCsdS)Nr )r r%r#r r r _generate_nothingsz VCPythonEngine._generate_nothingcKsdS)Nr )r r%r#rBrr r r _loaded_noopszVCPythonEngine._loaded_noopcCs*d}t|tjrT|jr2|jdkr2d}d|j}nd|jd|jjddf}d}nt|tjrt|j||||dSt|tj tj fr|j d ||j ||f|j d |dSt|tj rd |jd}d |j |}d }nt||j d||||f|j d||jd|f|j d |dS)Nr_BoolZ_cffi_to_c_intz, %sz(%s)_cffi_to_c_%srX_z-1z6 if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)z %s;z(%s)_cffi_to_c_pointerz, _cffi_type(%d)ZNULLz %s = %s(%s%s);z' if (%s == (%s)%s && PyErr_Occurred()))r!rr"is_integer_typer# get_c_namereplace PointerType"_convert_funcarg_to_c_ptr_or_array StructOrUnionEnumTyperr FunctionPtrTypeNotImplementedError)r r%fromvartovarerrcodeZextraargZ converterZerrvaluer r r _convert_funcarg_to_cs4     z$VCPythonEngine._convert_funcarg_to_ccCst|tjr|jddS)NzPy_ssize_t datasize)r!rrhadd)r r% localvarsr r r _extra_local_variabless z%VCPythonEngine._extra_local_variablescCs|jd|jd|j|||f|jd|jd|jd||jd|f|jd|f|jd||j||f|jd||jd dS) Nz1 datasize = _cffi_prepare_pointer_call_argument(z( _cffi_type(%d), %s, (char **)&%s);z if (datasize != 0) {z if (datasize < 0)z %s;z" %s = alloca((size_t)datasize);z, memset((void *)%s, 0, (size_t)datasize);zL if (_cffi_convert_array_from_object((char *)%s, _cffi_type(%d), %s) < 0)z })rr )r r%rnrorpr r r ris   z1VCPythonEngine._convert_funcarg_to_c_ptr_or_arraycCst|tjr`|jr,|jdkr,d||jfS|jdkrLd|jjdd|fSd||j|fSnt|tjtjfrd||j|fSt|tj rd||jtj|j fSt|tj r|j dkrt d |j|fd ||j|fSt|tjrd||j|fSt|dS) Nrcz_cffi_from_c_int(%s, %s)z long doublez_cffi_from_c_%s(%s)rXrdz/_cffi_from_c_deref((char *)&%s, _cffi_type(%d))z0_cffi_from_c_pointer((char *)%s, _cffi_type(%d))z!'%s' is used as %s, but is opaquez0_cffi_from_c_struct((char *)&%s, _cffi_type(%d)))r!rr"rer#rgr rhrl ArrayTypeitemrjfldnames TypeError _get_c_namerkrm)r r%varcontextr r r _convert_expr_from_c)s.     z#VCPythonEngine._convert_expr_from_ccCs<|jr|j|n&x|jD]}|j|qW|j|jdS)N)ellipsisr&argsresult)r r%r#rr r r "_generate_cpy_function_collecttypePs   z1VCPythonEngine._generate_cpy_function_collecttypec Csp|jr|jd||dS|j}t|j}|dkr6d}n|dkrDd}nd}|d|d||f|d d |}x.t|jD] \}}|d |jd ||q|Wt} x|jD]}|j|| qWx| D]} |d | fqWt |j t j sd } d|}|d |j jd|nd} t|jdkrt t|j} x| D]}|d|q:W||dd||djdd| Df|d|x6t|jD](\}}|j|d|d|d|qW|d|d|d| |djddt t|jDf|d|d ||d!|dkr.|d"| rN|d#|j|j d$d%n|d&|d'|d(|dS))NFrZnoargrZarg0r~zstatic PyObject *z(_cffi_f_%s(PyObject *self, PyObject *%s)r)zargument of %sz %s;z x%dz result = z result of %sz resultrz PyObject *arg%d;z+ if (!PyArg_ParseTuple(args, "%s:%s", %s))Oz, cSsg|] }d|qS)z&arg%dr )r4r8r r r r7sz>VCPythonEngine._generate_cpy_function_decl..z return NULL;zarg%dzx%dz return NULLz Py_BEGIN_ALLOW_THREADSz _cffi_restore_errno();z { %s%s(%s); }cSsg|] }d|qS)zx%dr )r4r8r r r r7sz _cffi_save_errno();z Py_END_ALLOW_THREADSz (void)self; /* unused */z (void)noarg; /* unused */z return %s;rz result typez Py_INCREF(Py_None);z return Py_None;r*)r}_generate_cpy_constrr$r~ enumeraterfsetrtr!rrZVoidTyperKjoinrqr|) r r%r#r/numargsZargnamer{r8rrsr'Z result_coderngr r r _generate_cpy_function_decl[sr    " $ z*VCPythonEngine._generate_cpy_function_declcCsL|jr dSt|j}|dkr"d}n|dkr0d}nd}|jd|||fdS)NrZ METH_NOARGSrZMETH_OZ METH_VARARGSz {"%s", _cffi_f_%s, %s, NULL},)r}r$r~r)r r%r#rmethr r r _generate_cpy_function_methods z,VCPythonEngine._generate_cpy_function_methodcCs.|jr dSt||}t|||||j|<dS)N)r}rZsetattrr )r r%r#rBrCfuncr r r _loaded_cpy_functions   z#VCPythonEngine._loaded_cpy_functioncCs|j|d|dS)Nstruct)_generate_struct_or_union_decl)r r%r#r r r _generate_cpy_struct_declsz(VCPythonEngine._generate_cpy_struct_declcCs|j|d|dS)Nr) _generate_struct_or_union_method)r r%r#r r r _generate_cpy_struct_methodsz*VCPythonEngine._generate_cpy_struct_methodcCs|j|d||dS)Nr)_loading_struct_or_union)r r%r#rBr r r _loading_cpy_structsz"VCPythonEngine._loading_cpy_structcKs|j|dS)N)_loaded_struct_or_union)r r%r#rBrr r r _loaded_cpy_structsz!VCPythonEngine._loaded_cpy_structcCs|j|d|dS)Nunion)r)r r%r#r r r _generate_cpy_union_declsz'VCPythonEngine._generate_cpy_union_declcCs|j|d|dS)Nr)r)r r%r#r r r _generate_cpy_union_methodsz)VCPythonEngine._generate_cpy_union_methodcCs|j|d||dS)Nr)r)r r%r#rBr r r _loading_cpy_unionsz!VCPythonEngine._loading_cpy_unioncKs|j|dS)N)r)r r%r#rBrr r r _loaded_cpy_unionsz VCPythonEngine._loaded_cpy_unionc Cs|jdkrdSd||f}d||f}d||fj}|j}|d||f|d|d|dx|jD]\}} } } t| tjr| js| dkr|d |qny$|d | jd d || d |fWqnt k r} z|dt | WYdd} ~ XqnXqnW|d|d|d|f|d|d||d|d||dxt|jD]h\}} } } | dkrrqX|d||ft| tj r| j dkr|d| j n|d||fqXW|d|d|d|d|d|d|d|f|d|dS) Nz_cffi_check_%s_%sz_cffi_layout_%s_%sz%s %szstatic void %s(%s *p)r)z8 /* only to generate compile-time warnings or errors */z (void)p;rz (void)((p->%s) << 1);z { %s = &p->%s; (void)tmp; }z*tmpzfield %r)Zqualsz /* %s */r*zstatic PyObject *z#%s(PyObject *self, PyObject *noarg)z, struct _cffi_aligncheck { char x; %s y; };z static Py_ssize_t nums[] = {z sizeof(%s),z) offsetof(struct _cffi_aligncheck, y),z offsetof(%s, %s),z 0, /* %s */z sizeof(((%s *)0)->%s),z -1z };z (void)self; /* unused */z (void)noarg; /* unused */z' return _cffi_get_struct_layout(nums);z3 /* the next line is not executed, but compiled */z %s(0);)rwstripr enumfieldsr!rr"rerfrstrrulengthry) r r%prefixr#Z checkfuncnamelayoutfuncnamecnamer/fnameftypefbitsizefqualrRr r r rsX    &   z-VCPythonEngine._generate_struct_or_union_declcCs0|jdkrdSd||f}|jd||fdS)Nz_cffi_layout_%s_%sz {"%s", %s, METH_NOARGS, NULL},)rwr)r r%rr#rr r r rs   z/VCPythonEngine._generate_struct_or_union_methodc Cs|jdkrdSd||f}t||}|}t|tjr|jr|d}|d} |ddd} |ddd} |j| | || f|_nd||fj} || f|j |<dS)Nz_cffi_layout_%s_%srrz%s %s) rwrZr!rrjpartialZ force_flattenZ fixedlayoutrr ) r r%rr#rBrZfunctionlayoutZ totalsizeZtotalalignmentZfieldofsZ fieldsizerr r r r s   z'VCPythonEngine._loading_struct_or_unionc Cs|jdkrdS|jj|||jkrdd}|j}|j|}|jj|\}}||d|j|d||d|j|dd}x|jD]v\}} } } | dkrq||||j||d|f||ddkr|j| } |||d|j| d |f|d7}qWdS) NcSs||krtd|||fdS)Nz'%s (we have %d, but C compiler says %d))r)Z realvalueZ expectedvaluemsgr r r check(sz5VCPythonEngine._loaded_struct_or_union..checkrzwrong total sizerzwrong total alignmentrzwrong offset for field %rzwrong size for field %r) rwrrMr popsizeofZalignofrZoffsetof) r r%rrZBStructrrr8rrrrZBFieldr r r r!s,        z&VCPythonEngine._loaded_struct_or_unioncCs.t|tjr|j||dn|j|d|dS)Nr)r!rrk_generate_cpy_enum_declr)r r%r#r r r _generate_cpy_anonymous_declEs z+VCPythonEngine._generate_cpy_anonymous_declcCst|tjs|j|d|dS)Nr)r!rrkr)r r%r#r r r _generate_cpy_anonymous_methodKs z-VCPythonEngine._generate_cpy_anonymous_methodcCs0t|tjr|j|||n|j|d||dS)Nr)r!rrk_loading_cpy_enumr)r r%r#rBr r r _loading_cpy_anonymousOs z%VCPythonEngine._loading_cpy_anonymouscKs.t|tjr |j|||f|n |j|dS)N)r!rrk_loaded_cpy_enumr)r r%r#rBrr r r _loaded_cpy_anonymousUs z$VCPythonEngine._loaded_cpy_anonymousconstFc Cs`|j} d||f} | d| | d| d| d|sT| d|pD|jd|n|dk rh|j|||s|dkr~d |} n|} | d | f| d |j|d d fn | d|| d| d|r | d| d| d|f| d| d| d| d| d|| d| d| d| d|j|| d|j|<| d| dS)Nz _cffi_%s_%szstatic int %s(PyObject *lib)r)z PyObject *o;z int res;z %s;z irz&z i = (%s);z o = %s;r8z variable typez! o = _cffi_from_c_int_const(%s);z if (o == NULL)z return -1;z {z PyObject *o1 = o;z8 o = Py_BuildValue("On", o1, (Py_ssize_t)sizeof(%s));z Py_DECREF(o1);z if (o == NULL)z return -1;z }z- res = PyObject_SetAttrString(lib, "%s", o);z Py_DECREF(o);z if (res < 0)z return %s;z(lib)r*)rrf_check_int_constant_valuer|r+) r is_intr#r%categoryvartpdelayedsize_too check_valuer/funcnameZrealexprr r r r^sL         z"VCPythonEngine._generate_cpy_constcCs&t|tjo|j}|s"|j|dS)N)r!rr"rer&)r r%r#rr r r "_generate_cpy_constant_collecttypesz1VCPythonEngine._generate_cpy_constant_collecttypecCs&t|tjo|j}|j|||dS)N)r!rr"rer)r r%r#rr r r _generate_cpy_constant_declsz*VCPythonEngine._generate_cpy_constant_declcCs|j}|dkr"|d|||fn|d|||f|d|d||d||d|d||d |d |d |||f|d |d dS)Nrz& if ((%s) > 0 || (long)(%s) != %dL) {z1 if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {z char buf[64];z if ((%s) <= 0)z. snprintf(buf, 63, "%%ld", (long)(%s));z elsez7 snprintf(buf, 63, "%%lu", (unsigned long)(%s));z) PyErr_Format(_cffi_VerificationError,z6 "%s%s has the real value %s, not %s",z( "%s", "%s", buf, "%d");z return -1;z })r)r r#r6Z err_prefixr/r r r rs$  z(VCPythonEngine._check_int_constant_valuecCs|jdd}d||fS)N$Z___D_z _cffi_e_%s_%s)rg)r rr#r r r _enum_funcnames zVCPythonEngine._enum_funcnameenumcCs|jr*x|jD]}|jd|ddqWdS|j||}|j}|d||dx,t|j|jD]\}}|j||d|q`W|d|jd|d|jd<|d |dS) NTF)rzstatic int %s(PyObject *lib)r)z enum %s: z return %s;z(lib)r*) r enumeratorsrrrzip enumvaluesrr+)r r%r#r enumeratorrr/ enumvaluer r r rs   z&VCPythonEngine._generate_cpy_enum_declcs.|jr*fdd|jD}t||_d|_dS)Ncsg|]}t|qSr )rZ)r4r)rBr r r7sz4VCPythonEngine._loading_cpy_enum..T)rrtuplerZpartial_resolved)r r%r#rBrr )rBr rs    z VCPythonEngine._loading_cpy_enumcCs,x&t|j|jD]\}}t|||qWdS)N)rrrr)r r%r#rBrCrrr r r rszVCPythonEngine._loaded_cpy_enumcCs&|dkrd}n|}|jd||ddS)Nz...T)r)r)r r%r#rr r r _generate_cpy_macro_declsz'VCPythonEngine._generate_cpy_macro_declcCs2t|tjrtj|j}n tj|}|j|dS)N)r!rrurhrvr&)r r%r#tp_ptrr r r "_generate_cpy_variable_collecttypes  z1VCPythonEngine._generate_cpy_variable_collecttypecCsTt|tjr4tj|j}|jd||||jdkdntj|}|jd||dddS)NFz...)rrrz)r)r!rrurhrvrr)r r%r#rr r r _generate_cpy_variable_decls     z*VCPythonEngine._generate_cpy_variable_declc st||}t|tjr|jdkrp|\}}|jj|j}t||jj |\}} | dkrft d||jf|j |}|jdk r|jj|} |jj | |}t |||dS|t||fdd} fdd} t t||t| | t|jj|dS)Nz...rz/bad size: %r does not seem to be an array of %scsdS)Nrr )rC)ptrr r gettersz3VCPythonEngine._loaded_cpy_variable..gettercs |d<dS)Nrr )rCr6)rr r settersz3VCPythonEngine._loaded_cpy_variable..setter)rZr!rrurrrMrvdivmodrrZresolve_lengthcastrdelattrrpropertyr:append) r r%r#rBrCr6sizeZ BItemTyperrestZBArrayrrr )rr _loaded_cpy_variables,          z#VCPythonEngine._loaded_cpy_variablecCs4|j}|d|d|d|jd|ddS)Nz,static int _cffi_setup_custom(PyObject *lib)r)z return %s;Tr*)rr+)r r/r r r r-s z%VCPythonEngine._generate_setup_custom)r)N)NrNTFN)r)r)Lr?r@rAZ _class_keyZ_gen_python_modulerrrrrr r&r1rUrWrrHrarbrqrtrir|Z!_generate_cpy_typedef_collecttypeZ_generate_cpy_typedef_declZ_generate_cpy_typedef_methodZ_loading_cpy_typedefZ_loaded_cpy_typedefrrrZ_loading_cpy_functionrZ _generate_cpy_struct_collecttyperrrrZ_generate_cpy_union_collecttyperrrrrrrrZ#_generate_cpy_anonymous_collecttyperrrrrrrZ_generate_cpy_constant_methodZ_loading_cpy_constantZ_loaded_cpy_constantrrrZ_generate_cpy_enum_collecttypeZ_generate_cpy_enum_methodrrrZ_generate_cpy_macro_collecttypeZ_generate_cpy_macro_methodZ_loading_cpy_macroZ_loaded_cpy_macrorrZ_generate_cpy_variable_methodZ_loading_cpy_variablerr-r r r r r s [ : $ I  3"  .   "rat# #include #include /* this block of #ifs should be kept exactly identical between c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py and cffi/_cffi_include.h */ #if defined(_MSC_VER) # include /* for alloca() */ # if _MSC_VER < 1600 /* MSVC < 2010 */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; typedef __int8 int_least8_t; typedef __int16 int_least16_t; typedef __int32 int_least32_t; typedef __int64 int_least64_t; typedef unsigned __int8 uint_least8_t; typedef unsigned __int16 uint_least16_t; typedef unsigned __int32 uint_least32_t; typedef unsigned __int64 uint_least64_t; typedef __int8 int_fast8_t; typedef __int16 int_fast16_t; typedef __int32 int_fast32_t; typedef __int64 int_fast64_t; typedef unsigned __int8 uint_fast8_t; typedef unsigned __int16 uint_fast16_t; typedef unsigned __int32 uint_fast32_t; typedef unsigned __int64 uint_fast64_t; typedef __int64 intmax_t; typedef unsigned __int64 uintmax_t; # else # include # endif # if _MSC_VER < 1800 /* MSVC < 2013 */ # ifndef __cplusplus typedef unsigned char _Bool; # endif # endif #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif #endif #if PY_MAJOR_VERSION < 3 # undef PyCapsule_CheckExact # undef PyCapsule_GetPointer # define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule)) # define PyCapsule_GetPointer(capsule, name) \ (PyCObject_AsVoidPtr(capsule)) #endif #if PY_MAJOR_VERSION >= 3 # define PyInt_FromLong PyLong_FromLong #endif #define _cffi_from_c_double PyFloat_FromDouble #define _cffi_from_c_float PyFloat_FromDouble #define _cffi_from_c_long PyInt_FromLong #define _cffi_from_c_ulong PyLong_FromUnsignedLong #define _cffi_from_c_longlong PyLong_FromLongLong #define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong #define _cffi_from_c__Bool PyBool_FromLong #define _cffi_to_c_double PyFloat_AsDouble #define _cffi_to_c_float PyFloat_AsDouble #define _cffi_from_c_int_const(x) \ (((x) > 0) ? \ ((unsigned long long)(x) <= (unsigned long long)LONG_MAX) ? \ PyInt_FromLong((long)(x)) : \ PyLong_FromUnsignedLongLong((unsigned long long)(x)) : \ ((long long)(x) >= (long long)LONG_MIN) ? \ PyInt_FromLong((long)(x)) : \ PyLong_FromLongLong((long long)(x))) #define _cffi_from_c_int(x, type) \ (((type)-1) > 0 ? /* unsigned */ \ (sizeof(type) < sizeof(long) ? \ PyInt_FromLong((long)x) : \ sizeof(type) == sizeof(long) ? \ PyLong_FromUnsignedLong((unsigned long)x) : \ PyLong_FromUnsignedLongLong((unsigned long long)x)) : \ (sizeof(type) <= sizeof(long) ? \ PyInt_FromLong((long)x) : \ PyLong_FromLongLong((long long)x))) #define _cffi_to_c_int(o, type) \ ((type)( \ sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \ : (type)_cffi_to_c_i8(o)) : \ sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \ : (type)_cffi_to_c_i16(o)) : \ sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \ : (type)_cffi_to_c_i32(o)) : \ sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \ : (type)_cffi_to_c_i64(o)) : \ (Py_FatalError("unsupported size for type " #type), (type)0))) #define _cffi_to_c_i8 \ ((int(*)(PyObject *))_cffi_exports[1]) #define _cffi_to_c_u8 \ ((int(*)(PyObject *))_cffi_exports[2]) #define _cffi_to_c_i16 \ ((int(*)(PyObject *))_cffi_exports[3]) #define _cffi_to_c_u16 \ ((int(*)(PyObject *))_cffi_exports[4]) #define _cffi_to_c_i32 \ ((int(*)(PyObject *))_cffi_exports[5]) #define _cffi_to_c_u32 \ ((unsigned int(*)(PyObject *))_cffi_exports[6]) #define _cffi_to_c_i64 \ ((long long(*)(PyObject *))_cffi_exports[7]) #define _cffi_to_c_u64 \ ((unsigned long long(*)(PyObject *))_cffi_exports[8]) #define _cffi_to_c_char \ ((int(*)(PyObject *))_cffi_exports[9]) #define _cffi_from_c_pointer \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10]) #define _cffi_to_c_pointer \ ((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11]) #define _cffi_get_struct_layout \ ((PyObject *(*)(Py_ssize_t[]))_cffi_exports[12]) #define _cffi_restore_errno \ ((void(*)(void))_cffi_exports[13]) #define _cffi_save_errno \ ((void(*)(void))_cffi_exports[14]) #define _cffi_from_c_char \ ((PyObject *(*)(char))_cffi_exports[15]) #define _cffi_from_c_deref \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16]) #define _cffi_to_c \ ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17]) #define _cffi_from_c_struct \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18]) #define _cffi_to_c_wchar_t \ ((wchar_t(*)(PyObject *))_cffi_exports[19]) #define _cffi_from_c_wchar_t \ ((PyObject *(*)(wchar_t))_cffi_exports[20]) #define _cffi_to_c_long_double \ ((long double(*)(PyObject *))_cffi_exports[21]) #define _cffi_to_c__Bool \ ((_Bool(*)(PyObject *))_cffi_exports[22]) #define _cffi_prepare_pointer_call_argument \ ((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23]) #define _cffi_convert_array_from_object \ ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24]) #define _CFFI_NUM_EXPORTS 25 typedef struct _ctypedescr CTypeDescrObject; static void *_cffi_exports[_CFFI_NUM_EXPORTS]; static PyObject *_cffi_types, *_cffi_VerificationError; static int _cffi_setup_custom(PyObject *lib); /* forward */ static PyObject *_cffi_setup(PyObject *self, PyObject *args) { PyObject *library; int was_alive = (_cffi_types != NULL); (void)self; /* unused */ if (!PyArg_ParseTuple(args, "OOO", &_cffi_types, &_cffi_VerificationError, &library)) return NULL; Py_INCREF(_cffi_types); Py_INCREF(_cffi_VerificationError); if (_cffi_setup_custom(library) < 0) return NULL; return PyBool_FromLong(was_alive); } static int _cffi_init(void) { PyObject *module, *c_api_object = NULL; module = PyImport_ImportModule("_cffi_backend"); if (module == NULL) goto failure; c_api_object = PyObject_GetAttrString(module, "_C_API"); if (c_api_object == NULL) goto failure; if (!PyCapsule_CheckExact(c_api_object)) { PyErr_SetNone(PyExc_ImportError); goto failure; } memcpy(_cffi_exports, PyCapsule_GetPointer(c_api_object, "cffi"), _CFFI_NUM_EXPORTS * sizeof(void *)); Py_DECREF(module); Py_DECREF(c_api_object); return 0; failure: Py_XDECREF(module); Py_XDECREF(c_api_object); return -1; } #define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num)) /**********/ ) rFrrrrSrrNrr,r r r r s  u__pycache__/error.cpython-36.opt-1.pyc000064400000002273152344566040013507 0ustar003 MZ@sDGdddeZGdddeZGdddeZGdddeZdS) c@s eZdZdS)FFIErrorN)__name__ __module__ __qualname__rr/usr/lib64/python3.6/error.pyrsrc@seZdZddZdS) CDefErrorc Cs\y*|jd}|jj}|jj}d||f}Wntttfk rHd}YnXd||jdfS)Nz%s:%d: z%s%s)argsZcoordfilelineAttributeError TypeError IndexError)selfZ current_declfilenameZlinenumprefixrrr__str__s  zCDefError.__str__N)rrrrrrrrrsrc@seZdZdZdS)VerificationErrorz- An error raised when verification fails N)rrr__doc__rrrrrsrc@seZdZdZdS)VerificationMissingzl An error raised when incomplete structures are passed into cdef, but no verification has been done N)rrrrrrrrrsrN) Exceptionrrrrrrrrs __pycache__/recompiler.cpython-36.pyc000064400000133175152344566040013566 0ustar003 MZ@sddlZddlZddlZddlmZmZddlmZddlTdZ dZ dZ Gd d d Z Gd d d Z Gd ddZGdddZGdddZGdddZejd0krejZnGdddejZddZd1ddZd2ddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd3d,d-Zd.d/ZdS)4N) ffiplatformmodel)VerificationError)*i&i'i(c@s&eZdZd ddZddZddZdS) GlobalExprrcCs"||_||_||_||_||_dS)N)nameaddresstype_opsize check_value)selfrr r r r r"/usr/lib64/python3.6/recompiler.py__init__ s zGlobalExpr.__init__cCsd|j|j|jj|jfS)Nz' { "%s", (void *)%s, %s, (void *)%s },)rr r as_c_exprr )r rrrrszGlobalExpr.as_c_exprcCsd|jj|j|jfS)Nz b'%s%s',%d)r as_python_bytesrr )r rrras_python_exprszGlobalExpr.as_python_exprN)rr)__name__ __module__ __qualname__rrrrrrrr s rc@s,eZdZddZddZddZddZd S) FieldExprcCs"||_||_||_||_||_dS)N)r field_offset field_sizefbitsize field_type_op)r rrrrrrrrrs zFieldExpr.__init__cCs>dt|j}d|j|jfd||jfd||jjfS)N z { "%s", %s, z %s %s, z %s %s },)lenrrrrr)r Zspacesrrrr#szFieldExpr.as_c_exprcCstdS)N)NotImplementedError)r rrrr)szFieldExpr.as_python_exprcCsD|jjtkrd}n|jjtkr*t|j}ntd|jj||jfS)Nz b'%s%s%s') ropOP_NOOP OP_BITFIELDformat_four_bytesrrrr)r Z size_exprrrras_field_python_expr,s    zFieldExpr.as_field_python_exprN)rrrrrrr$rrrrrsrc@s$eZdZddZddZddZdS)StructUnionExprc Cs4||_||_||_||_||_||_||_||_dS)N)r type_indexflagsr alignmentcommentfirst_field_indexc_fields) r rr&r'r r(r)r*r+rrrr8szStructUnionExpr.__init__cCsPd|j|j|jfd|j|jfd|jt|jf|jrFd|jnddS)Nz { "%s", %d, %s,z %s, %s, z%d, %d z /* %s */ rz},) rr&r'r r(r*rr+r))r rrrrCszStructUnionExpr.as_c_exprcCs>t|jt}dd|jD}dt|jt||jdj|fS)NcSsg|] }|jqSr)r$).0Zc_fieldrrr Lsz2StructUnionExpr.as_python_expr..z(b'%s%s%s',%s),)evalr'ZG_FLAGSr+r#r&rjoin)r r'Z fields_exprrrrrJs  zStructUnionExpr.as_python_exprN)rrrrrrrrrrr%7s r%c@s$eZdZddZddZddZdS)EnumExprcCs"||_||_||_||_||_dS)N)rr&r signedallenums)r rr&r r2r3rrrrUs zEnumExpr.__init__cCsd|j|j|j|j|jfS)Nz1 { "%s", %d, _cffi_prim_int(%s, %s), "%s" },)rr&r r2r3)r rrrr\szEnumExpr.as_c_exprcCsNdtdtd td td td td tdti|j|j f}dt |j t ||j |j fS)Nrrzb'%s%s%s\x00%s')rr)rr)r4r)r4r)r5r)r5r)r6r)r6r)Z PRIM_UINT8Z PRIM_INT8Z PRIM_UINT16Z PRIM_INT16Z PRIM_UINT32Z PRIM_INT32Z PRIM_UINT64Z PRIM_INT64r r2r#r&rr3)r prim_indexrrrras  zEnumExpr.as_python_exprN)rrrrrrrrrrr1Tsr1c@s$eZdZddZddZddZdS) TypenameExprcCs||_||_dS)N)rr&)r rr&rrrrmszTypenameExpr.__init__cCsd|j|jfS)Nz { "%s", %d },)rr&)r rrrrqszTypenameExpr.as_c_exprcCsdt|j|jfS)Nzb'%s%s')r#r&r)r rrrrtszTypenameExpr.as_python_exprN)rrrrrrrrrrr8lsr8c@seZdZdZdddZddZddZd d Zd d Zd ddddgZ ddZ dddZ ddZ ddZ ddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Zd=d>Zd?d@Z ddBdCZ!dDdEZ"dFdGZ#dHdIZ$e$Z%dJdKZ&dLdMZ'e'Z(dNdOZ)e)Z*dPdQZ+dRdSZ,dTdUZ-ddWdXZ.dYdZZ/d[d\Z0d]d^Z1d_d`Z2ddadbZ3dcddZ4dedfZ5dgdhZ6didjZ7dkdlZ8dmdnZ9dodpZ:dqdrZ;dsdtZZ?dwdxZ@dydzZAd{d|ZBd}d~ZCddZDeDZEZFddZGddZHddZIddZJddZKddZLddZMeMZNeMZOddZPddZQddZReRZSddZTdAS) RecompilerrFcCs||_||_||_t|_dS)N)ffi module_nametarget_is_python VERSION_BASE_version)r r:r;r<rrrr~szRecompiler.__init__cCst|j||_dS)N)maxr>)r Zverrrr needs_versionszRecompiler.needs_versionc CsPi|_|jdt|jtd}g|_x|D]}|jr*|j|dksFtt|j|j|<|jj|xZ|j D]P}t |t j t j t jt jt jfst|j|dkrt|j|j|<|jj|qjW|jjdq*WxZ|D]R}|j o|j|dkrt|j|j|<|jj||jr|jdk r|jjdqWd|jjks>ti|_i|_x@|D]8}t |t jrnd|j|<nt |t jrPd|j|<qPWx.tt|jdddD]\}}||j|<qWx.tt|jdddD]\}}||j|<qWx0|D](}t|d|jj}|||j|qWx |jD]}t |ts&tq&Wt|j|_dS) NZ collecttype)keyZENDLENcSs|jS)N)r)tprrrsz/Recompiler.collect_type_table..cSs|jS)N)r)rCrrrrDsZ_emit_bytecode_) _typesdict _generatesortedstr cffi_typesZis_raw_functionAssertionErrorrappendargs isinstancerVoidTypeBasePrimitiveType PointerTypeStructOrUnionOrEnumFunctionPtrTypeZ is_array_typelengthvalues_struct_unions_enums StructOrUnionEnumType enumerategetattr __class__rCffiOptuple)r Z all_declsrCtp1imethodr rrrcollect_type_tablesZ            zRecompiler.collect_type_tablecCst|tjs2t|tr.x|D]}|j|qWdS||jkrd|j|<t|tjrb|j|jntt|tjr|j dk r||j j j krxN|j D] \}}}}|j|j|||qWn x|jD]\}}|j|qWdS)N)rMrZBaseTypeByIdentityr]_do_collect_typerErRas_raw_functionrWfldtypesr:_parser_included_declarations enumfields _field_typeZ _get_items)r rCxZname1r^_rrrrbs         zRecompiler._do_collect_typec Cs|jjjj}xt|D]\}\}}|jdd\}}yt|d||f}Wn tk rjtd|YnXy||_ |||Wqt k r} zt j | |WYdd} ~ XqXqWdS)Nrrz_generate_cpy_%s_%sz"not implemented in recompile(): %r) r:reZ _declarationsitemsrGsplitrZAttributeErrorr_current_quals ExceptionrZattach_exception_info) r step_namelstrrCqualsZkindZrealnamer`errrrFs zRecompiler._generateglobalfield struct_unionenumtypenamecCs:i|_x|jD]}g|j|<qWt|_|jd|jx>|jD]4}|j|}|dkrh|jdddt||j|<qBW|jd}x:|jj D],\}}|t |kst ||j |j kst qWt |t |jkst |jd}x<|j j D].\}}|t |kst ||j |j kst qWt |t |j ks6t dS)NZctxrucSs|jS)N)r)entryrrrrDsz0Recompiler.collect_step_tables..)rArvrw)_lsts ALL_STEPSset_seen_struct_unionsrF_add_missing_struct_unionssortr]rUrkrrJrrV)r rprqrCr_rrrcollect_step_tabless*      zRecompiler.collect_step_tablesrcCs|jj|ddS)N )_fwrite)r Zwhatrrr_prntszRecompiler._prntcCs:|jr|dkst|j|n|dk s*t|j||dS)N)r<rJwrite_py_source_to_fwrite_c_source_to_f)r fpreamblerrrwrite_source_to_f s    zRecompiler.write_source_to_fcCs0ttjjtjjt|d}|j}|j|S)Nr)openospathr0dirname__file__ readlinesclose)r filenameglinesrrr_rel_readlinesszRecompiler._rel_readlinesc Csz||_|j}|jjdk r |d|jd}|jd}|jd|||d<|dj||jjddI}|jjdk r0|d|jf|d |j |jj|d |d |d |f|d |d|f|d|d|f|d|jd}|jd}|jd|||d<|dj||j t |d|||||d||dt dd|j jD}xLt|jD]>\}}d} ||krd||j} |d||j| fqW|js|d|d|t|_|jdi} xn|jD]d} |j| } t| | | <| | dkr|d| | fx| D]} || jqBW|d|qW|jjr|d xr|jjD]f}y|jdd!\}}Wn(tk rtd"|j|fYnX|dkrtd#|d$|fqW|d%|d||d&|d'x8|jD].} | | dkr@|d(| n |d)| q Wx.|jD]$} | d*krZ|d+| | | fqZW|jjr|d,n|d-|d.t|jfd}|jr|dO}|d/||d||d0|d1|d||d |d2|d3|f|d4|jrD|d5|d6|d7|d8|j|d9|d:|d;|d|d<|d=|d>|d?|d@|f|dA|dB|f|dC|dC|d |d2|dD|f|d4|dE|j|jf|d<|d|d2|dF|f|d4|dG|j|jf|d<|d||d0|dH|dd|_dS)JNz#define _CFFI_USE_EMBEDDINGz_cffi_include.hz#include "parse_c_type.h" zparse_c_type.hrr.z#define _CFFI_MODULE_NAME "%s"z1static const char _CFFI_PYTHON_STARTUP_CODE[] = {z0 };z#ifdef PYPY_VERSIONz5# define _CFFI_PYTHON_STARTUP_FUNC _cffi_pypyinit_%sz#elif PY_MAJOR_VERSION >= 3z-# define _CFFI_PYTHON_STARTUP_FUNC PyInit_%sz#elsez*# define _CFFI_PYTHON_STARTUP_FUNC init%sz#endifz _embedding.hz#include "_cffi_errors.h" z_cffi_errors.hz>/************************************************************/zstatic void *_cffi_types[] = {cSsg|]\}}||fqSrr)r,rCr_rrrr-Gsz2Recompiler.write_c_source_to_f..z // z/* %2d */ %s,%sz 0z};declrz.static const struct _cffi_%s_s _cffi_%ss[] = {z.static const char * const _cffi_includes[] = {r4zQffi object %r includes %r, but the latter has not been prepared with set_source()zMnot implemented yet: ffi.include() of a Python-based ffi inside a C-based ffiz "%s",z NULLz?static const struct _cffi_type_context_s _cffi_type_context = {z _cffi_types,z _cffi_%ss,z NULL, /* no %ss */ruz %d, /* num_%ss */z _cffi_includes,z NULL, /* no includes */z %d, /* num_types */z %d, /* flags */z#ifdef __GNUC__z?# pragma GCC visibility push(default) /* for -fvisibility= */ZPyMODINIT_FUNCz"_cffi_pypyinit_%s(const void *p[]){z% if (((intptr_t)p[0]) >= 0x0A03) {zQ _cffi_call_python_org = (void(*)(struct _cffi_externpy_s *, char *))p[1];z }z p[0] = (const void *)0x%x;z p[1] = &_cffi_type_context;z#if PY_MAJOR_VERSION >= 3z return NULL;}z# ifdef _MSC_VERz PyMODINIT_FUNCz# if PY_MAJOR_VERSION >= 3z% PyInit_%s(void) { return NULL; }z# elsez init%s(void) { }z# endifzPyInit_%s(void)z5 return _cffi_init("%s", 0x%x, &_cffi_type_context);z init%s(void)z. _cffi_init("%s", 0x%x, &_cffi_type_context);z# pragma GCC visibility pop)rrr: _embeddingrindexr0r;rl_print_string_literal_in_arrayr@VERSION_EMBEDDEDdictrErkrYrI _get_c_namerr|_seen_constantsrFr{rzr_included_ffis_assigned_sourcermr _num_externpyr>)r rrprntrr_Zbase_module_nametypeindex2typer r)Znumsrprqryffi_to_includeincluded_module_nameincluded_sourcer'rrrrs                         zRecompiler.write_c_source_to_fcsbt|trd|fSt|ttfrZfdd|D}t|dkrJ|jdddj|fS|jS)Nzb'%s'csg|]}j|qSr)_to_py)r,item)r rrr-sz%Recompiler._to_py..rrz(%s)r.)rMrHlistr]rrKr0r)r riZrepr)r rrs    zRecompiler._to_pyc Cs||_|j}|d|dt|jjp(f}xzt|D]n}|jj|}y|jdd\}}Wn&tk rtd|j|fYnX|dk rtd|d||fq6W||d|j f|d|j fd|_ t |j |_ d d |j D}|d |j d j|ftd d |jjD} xH|jD]>} |j| } t| dkr*| dkr*|d| |j | fq*W|dkr|ddjdd t|Df|ddS)Nz# auto-generated filezimport _cffi_backendr4zQffi object %r includes %r, but the latter has not been prepared with set_source()zMnot implemented yet: ffi.include() of a C-based ffi inside a Python-based ffizfrom %s import ffi as _ffi%dzffi = _cffi_backend.FFI('%s',z _version = 0x%x,cSsg|] }|jqSr)r)r,r rrrr-sz3Recompiler.write_py_source_to_f..z _types = %s,rcSsg|]\}}||fqSrr)r,rCr_rrrr-srruz _%ss = %s,z _includes = (%s,),z, cSsg|] }d|qS)z_ffi%dr)r,r_rrrr-s))rrrr:rrangerrmrr;r>r]rIrr0rrErkr{rz) r rrZ num_includesr_rrrZ types_lstrrprqrrrrsB     zRecompiler.write_py_source_to_fcCs |j|S)N)rE)r typerrr _gettypenumszRecompiler._gettypenumc Cspd}t|tjr|j r|jr<|jdkr.z return NULL;zarg%dz return NULLz Py_BEGIN_ALLOW_THREADSz _cffi_restore_errno();cSsg|] }d|qS)zx%dr)r,r_rrrr-sz { %s%s(%s); }z _cffi_save_errno();z Py_END_ALLOW_THREADSz (void)self; /* unused */z (void)noarg; /* unused */z return %s;resultz result typez Py_INCREF(Py_None);z return Py_None;z#elsecSs t|tjpt|tjo|jS)N)rMrrW PrimitiveTyper)rrrrneed_indirections  z@Recompiler._generate_cpy_function_decl..need_indirectionFrTz %sx%dz%sx%dz *resultz *result = z%s_cffi_f_%s(%s)z return result;z# define _cffi_f_%s _cffi_d_%sz#endif)r<rJrMrrRr_generate_cpy_constant_declrrrLrYrKrr0abirrNr|rrrrinsertZ void_type)r rCrrnumargsZargname argumentsZcall_argumentsrr_rrepr_argumentsrname_and_argumentsZ result_codeargrrZ result_declrngr differenceZ indirectionZ tp_resultrrr_generate_cpy_function_decls                      z&Recompiler._generate_cpy_function_declcCs|jr|j r|j||dS|j|j}t|j}|jrBt}n |dkrPt}n|dkr^t }nt }|j dj t |d|t||d|ddS)Nrrrtz _cffi_f_%sz _cffi_d_%s)r )rr<_generate_cpy_constant_ctxrErcrrLZOP_DLOPEN_FUNCZOP_CPYTHON_BLTN_NZOP_CPYTHON_BLTN_OZOP_CPYTHON_BLTN_VrzrKrr\)r rCrr&rZ meth_kindrrr_generate_cpy_function_ctx#s     z%Recompiler._generate_cpy_function_ctxcCsTt|tjrP|j}|dkr0|jd}d||f}|j|d||j}tj||}|S)Nz...rz_cffi_array_len(((%s)0)->%s)z%s[0])rMrrrSrrhr)r Z tp_structZ field_nameZtp_field actual_lengthZptr_struct_nametp_itemrrrrh9s     zRecompiler._field_typecCs0|j||jr,x|jD]}|j|qWdS)N)rbr<anonymous_struct_fields_struct_collecttype)r rCfldtyperrrrEs zRecompiler._struct_collecttypec Cs<|jdkrdS|j}d|f}|d|d||f|d|d|dx|jD]\}}}} y||jsv|dkr|d|||fwXx2t|tjr|jdks|jd kr|j}|d }qW|d |j d d || d|fWqXt k r} z|dt | WYdd} ~ XqXXqXW|d|d||f|dS)Nz_cffi_checkfld_%sZ_CFFI_UNUSED_FNzstatic void %s(%s *p)rz8 /* only to generate compile-time warnings or errors */z (void)p;rz> (void)((p->%s) | 0); /* check that '%s.%s' is an integer */z...z[0]z { %s = &p->%s; (void)tmp; }z*tmpzfield %r)rrz /* %s */rz(struct _cffi_align_%s { char x; %s y; };) rdrrgrrMrrrSrrrrH) r rCrrrZ checkfuncnameZfnameZftyperfqualrsrrr _struct_declLs6   &zRecompiler._struct_declNc Cs|j|}d}g}t|tjr(|jd|jdkr@|jdd}||jjjkr|dksd||jjjkr|jdkrpn|j st |j rn |jd|j r|jdn|jdd}dj |pd }g}|dkrZ|j } t|j| } x | D]\} } } }|j|| | } |j| d |j| ft}| d kr6t}d | }nL|dksZt| tjr`| jdkr`d }n"d|dkrv|jdn|j| f}|dks| d krd }n,|dk rd|j| f}nd|jd| f}|jt| ||| t||j| qWt|jd}|jdj||dkr$d}d}d}n4|dk r@d|jf}d}nd|f}d|f}d}nd }d}d}|}|jdjt|j||||||||jj |dS) NZ _CFFI_F_UNIONZ_CFFI_F_OPAQUEZopaqueZ_CFFI_F_CHECK_FIELDSZ_CFFI_F_PACKEDZ_CFFI_F_EXTERNALZexternal|0z field '%s.%s'rz %d /* bits */z (size_t)-1zsizeof(((%s)0)->%s)rz"((char *)&((%s)0)->%s) - (char *)0zoffsetof(%s, %s)rruz (size_t)-2r4Zunnamedzsizeof(*(%s)0)z-1 /* unknown alignment */z sizeof(%s)z"offsetof(struct _cffi_align_%s, y)rrvrr)!rErMrZ UnionTyperKrdr:rerfpartialanyrZpackedr0r<rrgrh_check_not_opaquerr!r"rrSrrr\rrzextendr%r}r)r rCrrrr&Zreason_for_not_expandingr'r+Zexpand_anonymous_struct_unionrgZfldnamerrrr r offsetr*Zalignr)rrrrms                       zRecompiler._struct_ctxcCs>xt|tjr|j}qWt|tjr:|jdkr:td|dS)Nz0%s is of an opaque type (not declared in cdef()))rMrrrrWrdr)r rClocationrrrrs  zRecompiler._check_not_opaquecCst|jj}|jdddx|D]\}}||jkr$|jrJtd|f|jjdrx|jddj rx|jdd}n4|jdkr|j dkrd}|j |dntd |f|j |d|q$WdS) NcSs|dS)Nrr)Ztp_orderrrrrDsz7Recompiler._add_missing_struct_unions..)rAzDinternal inconsistency: %r is partial but was not seen at this point$rZ_IO_FILEFILEzinternal inconsistency: %r) rrUrkrr}rrr startswithisdigitZ forcenamerr)r rqrCorderrrrrr~s   z%Recompiler._add_missing_struct_unionscCs|j|dS)N)r)r rCrrrr _generate_cpy_struct_collecttypesz+Recompiler._generate_cpy_struct_collecttypecCs2|jd}d|kr"||jddfS|d|fSdS)Nrrrj)rr)r rCrrrr _struct_namess zRecompiler._struct_namescCs|j|f|j|dS)N)rr)r rCrrrr_generate_cpy_struct_declsz$Recompiler._generate_cpy_struct_declcCs|j|f|j|dS)N)rr)r rCrrrr_generate_cpy_struct_ctxsz#Recompiler._generate_cpy_struct_ctxcCs(t|tjr|j||n |j|dS)N)rMrrX_generate_cpy_enum_collecttyper)r rCrrrr#_generate_cpy_anonymous_collecttypes z.Recompiler._generate_cpy_anonymous_collecttypecCs.t|tjr|j|n|j||d|dS)Ntypedef_)rMrrX_generate_cpy_enum_declr)r rCrrrr_generate_cpy_anonymous_decls  z'Recompiler._generate_cpy_anonymous_declcCs0t|tjr|j||n|j||d|dS)Nr)rMrrX _enum_ctxr)r rCrrrr_generate_cpy_anonymous_ctxs z&Recompiler._generate_cpy_anonymous_ctxconstcCs||f|jkrtd||f|jj||f|j}d||f}|r|d||d|d|f|d||f|dk r|dkrd|f}|d |f|d |d |d n>|dkst|d ||d|d|jd|f|d |dS)Nz duplicate declaration of %s '%s'z _cffi_%s_%sz$static int %s(unsigned long long *o)rz int n = (%s) <= 0;zI *o = (unsigned long long)((%s) | 0); /* check that %s is an integer */rz%dUz" if (!_cffi_check_int(*o, n, %s))z n |= 2;z return n;rzstatic void %s(char *o)z *(%s)o = %s;r)rrrrrJr)r is_intrrCcategoryr rfuncnamerrr_generate_cpy_consts2       zRecompiler._generate_cpy_constcCs"|j}| s|jr|j|dS)N)rr<rb)r rCrrrrr"_generate_cpy_constant_collecttype&s z-Recompiler._generate_cpy_constant_collecttypecCs|j}|j|||dS)N)rr )r rCrrrrrr+sz&Recompiler._generate_cpy_constant_declcCs`|j r|jrttd}n$|jr(t}nt}|j|}t||}|jdjt |d||dS)Nrrtz_cffi_const_%sr) r<rr\OP_CONSTANT_INTZOP_DLOPEN_CONSTZ OP_CONSTANTrErzrKr)r rCrr Z const_kindr&rrrr/s    z%Recompiler._generate_cpy_constant_ctxcCs|j|dS)N)rb)r rCrrrrr?sz)Recompiler._generate_cpy_enum_collecttypecCs x|jD]}|jd|qWdS)NT) enumeratorsr )r rCr enumeratorrrrrBs z"Recompiler._generate_cpy_enum_declc Cs|j|}ttd }|jr"|jx:t|j|jD](\}}|jdj t |d|||dq2W|dk rd|kr|j rd|}d|}n4|j |j g} |j j | }tt|j j| d dk}d j|j} |jd j t|j|||| dS) Nrrtz_cffi_const_%s)r rz sizeof(%s)z ((%s)-1) <= 0rr.rwrr)rEr\OP_ENUMr<Zcheck_not_partialzipr Z enumvaluesrzrKrZbuild_baseinttyper:Zsizeofintcastr0r1r) r rCrr&r rZ enumvaluer r2Zbasetpr3rrrrFs"        zRecompiler._enum_ctxcCs|j||jdS)N)rr)r rCrrrr_generate_cpy_enum_ctx[sz!Recompiler._generate_cpy_enum_ctxcCsdS)Nr)r rCrrrr_generate_cpy_macro_collecttypeasz*Recompiler._generate_cpy_macro_collecttypecCs&|dkrd}n|}|jd||ddS)Nz...T)r )r )r rCrr rrr_generate_cpy_macro_decldsz#Recompiler._generate_cpy_macro_declcCsT|dkr"|jrtd|fd}n|}ttd}|jdjt|d|||ddS)Nz...zGcannot use the syntax '...' in '#define %s ...' when using the ABI moderrtz_cffi_const_%s)r r)r<rr\r rzrKr)r rCrr r rrr_generate_cpy_macro_ctxks    z"Recompiler._generate_cpy_macro_ctxcCsFt|tjrB|j}|dkr$d|f}|j|jd|}tj||}|S)Nz...z_cffi_array_len(%s)z%s[0])rMrrrSrr)r rCZ global_namerrrrrr|s   zRecompiler._global_typecCs|j|j||dS)N)rbr)r rCrrrr"_generate_cpy_variable_collecttypesz-Recompiler._generate_cpy_variable_collecttypecCs|j}|j||}t|tjr4|jdkr4|j}d}nd}d|f}|d|j||jd|d|d||f|d|dS) Nr&z*_cffi_var_%s(void)zstatic )rrrz return %s(%s);r) rrrMrrrSrrrn)r rCrrZ ampersandrrrr_generate_cpy_variable_decls  z&Recompiler._generate_cpy_variable_declcCsL|j||}|j|}|jr"t}nt}|jdjt|d|t||dS)Nrtz _cffi_var_%s) rrEr<Z OP_GLOBAL_VARZOP_GLOBAL_VAR_FrzrKrr\)r rCrr&r rrr_generate_cpy_variable_ctxs   z%Recompiler._generate_cpy_variable_ctxcCst|tjst|j|dS)N)rMrrRrJrb)r rCrrrr'_generate_cpy_extern_python_collecttypesz2Recompiler._generate_cpy_extern_python_collecttypecCs|j}t|jtjrd}nd|}d|jjd|f}|d||d|j||f|g}d|}x0t|jD]"\}} | jd||} |j | qtWd j |} | pd } d || f} |j d krd | } dd} t t |jdd}| |jrt |d}t|jtjr*d|jjd||jjd|f}|d||jj| f|d|d||dxft|jD]X\}} d|} t| tjs| | rd| } tj| } |d| jd|d| fqjW|d|t|jtjs|d|jjdf|d||jd7_dS)Nrz result of %sz(int)sizeof(%s)rz3static struct _cffi_externpy_s _cffi_externpy__%s =z { "%s.%s", %s };zargument of %sz a%dz, rz%s(%s) __stdcallz_cffi_stdcall cSst|tjo|jdkS)Nz long double)rMrrr)rCrrrmay_need_128_bitss z9Recompiler._extern_python_decl..may_need_128_bitsr6z!sizeof(%s) > %d ? sizeof(%s) : %dz%s%srz char a[%s];z char *p = a;za%drz *(%s)(p + %d) = %s;rz, _cffi_call_python(&_cffi_externpy__%s, p);z return *(%s)p;rr)rrMrrrNrr;rYrLrKr0rr?rrWrPr)r rCrZ tag_and_spacerZsize_of_resultrrr_rrrrrZ size_of_arrr_extern_python_declsX          " zRecompiler._extern_python_declcCs|j||ddS)Nzstatic )r)r rCrrrr _generate_cpy_extern_python_declsz+Recompiler._generate_cpy_extern_python_declcCs|j||ddS)NzCFFI_DLLEXPORT )r)r rCrrrr#_generate_cpy_dllexport_python_declsz.Recompiler._generate_cpy_dllexport_python_declcCs|j||ddS)Nr)r)r rCrrrr'_generate_cpy_extern_python_plus_c_declsz2Recompiler._generate_cpy_extern_python_plus_c_declcCsR|jrtd|jrtd|j|}tt|}|jdjt |d|||dS)Nz,cannot use 'extern "Python"' in the ABI modez$a vararg function is extern "Python"rtz&_cffi_externpy__%s) r<rrrrEr\ZOP_EXTERN_PYTHONrzrKr)r rCrr&r rrr_generate_cpy_extern_python_ctxs   z*Recompiler._generate_cpy_extern_python_ctxcCsz|j}|dxf|jdD]X}|d|jd}x6|D].}t|dkrT||d}|dt|f7}q8W||qWdS)Nz=// # NB. this is not a string because of a size limit in MSVCTz// rLz%d,)r splitlinesrstriprord)r srlineZ printed_linecrrrrs  z)Recompiler._print_string_literal_in_arraycCsttt|j|<dS)N)r\ OP_PRIMITIVEZ PRIM_VOIDrI)r rCrrrr_emit_bytecode_VoidTypesz"Recompiler._emit_bytecode_VoidTypecCst|j}tt||j|<dS)N)ZPRIMITIVE_TO_INDEXrr\r+rI)r rCrr7rrr_emit_bytecode_PrimitiveTypes z'Recompiler._emit_bytecode_PrimitiveTypecCs(d|j|j|jf}tt||j|<dS)Nzl_cffi_prim_int(sizeof(%s), ( ((%s)-1) | 0 /* check that %s is an integer type */ ) <= 0))rr\r+rI)r rCrr(rrr!_emit_bytecode_UnknownIntegerTypesz,Recompiler._emit_bytecode_UnknownIntegerTypecCs$d|j|jf}tt||j|<dS)Nzd_cffi_prim_float(sizeof(%s) * (((%s)1) / 2) * 2 /* integer => 0, float => 1 */ ))rr\r+rI)r rCrr(rrr_emit_bytecode_UnknownFloatTypesz*Recompiler._emit_bytecode_UnknownFloatTypecCstt|j|j|j|<|d7}xR|jD]H}|j|}||krht|tjrX|j ||ntt ||j|<|d7}q(Wt |j }|j dk r|j dkr|dO}ntd|j ftt||j|<dS)Nrrr4zabi=%r)r\Z OP_FUNCTIONrErrIrLrMrrr-r!rrrrZOP_FUNCTION_END)r rCrr^Z realindexr'rrr_emit_bytecode_RawFunctionType#s        z)Recompiler._emit_bytecode_RawFunctionTypecCstt|j|j|j|<dS)N)r\ OP_POINTERrErrI)r rCrrrr_emit_bytecode_PointerType6sz%Recompiler._emit_bytecode_PointerTypecCs"|j}tt|j||j|<dS)N)rcr\r1rErI)r rCrrawrrr_emit_bytecode_FunctionPtrType<sz)Recompiler._emit_bytecode_FunctionPtrTypecCs|j|j}|jdkr(tt||j|<nf|jdkrNtdt|jddfn@|j|ddksdt tt ||j|<tdt|j|j|d<dS)Nz...zitype %s badly placed: the '...' array length can only be used on global arrays or on fields of structuresz/*...*/rrB) rErrSr\Z OP_OPEN_ARRAYrIrrHrrJZOP_ARRAY)r rCrZ item_indexrrr_emit_bytecode_ArrayType@s   z#Recompiler._emit_bytecode_ArrayTypecCs|j|}tt||j|<dS)N)rUr\ZOP_STRUCT_UNIONrI)r rCrZ struct_indexrrr_emit_bytecode_StructTypeNs z$Recompiler._emit_bytecode_StructTypecCs|j|}tt||j|<dS)N)rVr\rrI)r rCrZ enum_indexrrr_emit_bytecode_EnumTypeSs z"Recompiler._emit_bytecode_EnumType)F)r)N)NrN)N)Urrrrrr@rarbrFr{rrrrrrrrrrrrrrrrrrrrrhrrrrr~rZ_generate_cpy_union_collecttyperrZ_generate_cpy_union_declrZ_generate_cpy_union_ctxrrrr r rrrrrrrrrrrrrrZ*_generate_cpy_dllexport_python_collecttypeZ._generate_cpy_extern_python_plus_c_collecttyperr r!r"r#Z"_generate_cpy_dllexport_python_ctxZ&_generate_cpy_extern_python_plus_c_ctxrr,r-r.r/r0r2Z_emit_bytecode_ConstPointerTypeZ_emit_bytecode_NamedPointerTyper4r5r6Z_emit_bytecode_UnionTyper7rrrrr9{s : , 4-#  ! Q     5 r9cseZdZfddZZS)NativeIOcs(t|tr|jd}tt|j|dS)Nascii)rMZunicodeencodesuperr9r)r r()r[rrr\s  zNativeIO.write)rrrr __classcell__rr)r[rr9[sr9c Cs|rtd|ft|||dkd}|j|jt}|j|||j}y@t|d }|jt |d|krvt WdQRX|rtddSt k rd|t j f} t| d}|j |WdQRXyt j| |Wn,tk r t j|t j| |YnXd SXdS) Nz generating %s)r<rrz(already up-to-date)Fz%s.~%dwT)printr9rarr9rgetvaluerreadrIOErrorrgetpidrrenameOSErrorunlink) r:r;rZ target_fileverboseZ recompilerroutputf1Ztmp_filerrr_make_c_or_py_sourceas4     rJFcCs|dk s tt|||||S)N)rJrJ)r:r;rZ target_c_filerGrrr make_c_source}s  rKcCst||d||S)N)rJ)r:r;Ztarget_py_filerGrrrmake_py_sources rLc Csj|jd}y$tjtjj|f|ddWntk rBYnX|d|7<tjj|f||fS)Nrrrr)rlrmakedirsrr0rE)Z outputdirmodname extensionpartsrrr_modname_to_files $rQcCs*t||}|j|||ft||||S)N)rZrKsetattr) patchlistclsrZnew_metholdrrr _patch_meths  rVcCs(x"t|D]\}}}t|||q WdS)N)reversedrR)rSrTrZold_methrrr_unpatch_methssrXcs\tjdkr(ddlm}t||dddtjdkrXddlm}fd d }t||d |dS) Nwin32r) MSVCCompilerZ_remove_visual_c_refcSs|S)Nr)r Z manifest_filerrrrDsz&_patch_for_embedding..darwin) CCompilercs<d|jkr,t|j|_|jjd}d|j|<|f||S)Nz-bundlez -dynamiclib)Z linker_sorr)r rLkwdsr_)old_link_shared_objectrrmy_link_shared_objects     z3_patch_for_embedding..my_link_shared_objectZlink_shared_object)sysplatformZdistutils.msvc9compilerrZrVZdistutils.ccompilerr\)rSrZr\r_r)r^r_patch_for_embeddings      rbcslddlm}jdrRdd tjdkr6d7ntjdkrJd7nd 7t||d fd d dS)Nr) build_extz.*r4rYz.dllr[z.dylibz.soZget_ext_filenamecsS)Nr)r Zext_name)targetrrrDsz#_patch_for_target..r)Zdistutils.command.build_extrcendswithr`rarV)rSrdrcr)rdr_patch_for_targets       rfrT.cc  Kst|ts|jd}|jr$|j| |dk r^|jdk } | rF|j| |dkrzt|||\}} |rl|g| } tj j | }n|}| dkr| rd|} nd} t j ||f| }t |||||d}|rTg}tj}zn| rt|| dkrt|| |r|dkrd}nd}td|tj j|ftj|t jd||| }Wdtj|t|X|S||fSn<|dkrxt||d \}}t||||d}|r|Sd|fSdS) Nr:z%s.*r)rGrzthe current directory isz setting the current directory toz%s %rz.py)rMrHr;Z_windows_unicodeZ_apply_windows_unicoderZ_apply_embedding_fixrQrrr0rZ get_extensionrKgetcwdrbrfr?abspathchdircompilerXrL)r:r;rtmpdirZcall_c_compilerZc_fileZsource_extensionZextradirZcompiler_verboserddebugr]Z embeddingrPZ ext_c_fileZextupdatedrScwdmsgoutputfilenamerjrrr recompilesb                  rrc Osddlm}ddl}|tjks,td|f|jdt|t|||f||}|j ||}xFt |j D]8} | j dsht |j | } | t || tk rht|| | qhWdd} | |_x6t |D]*} | j d rt|j |  rt|| tqW|jS)Nr)udirzmodule name conflict: %rrlrjc_stdS)N)r)rLr]rrrtypeof_disabledsz _verify..typeof_disabled)Z testing.udirrsimpr`modulesrJ setdefaultrHrrZ load_dynamicdirr:rrZobjectrRZ_typeofhasattrNotImplementedlib) r:r;rrLr]rsrurqmodulerattrrtrrr_verifys$     r)r8)F)F)rTNrgNrNN) rr`iorrrerrorrZ cffi_opcoder=rrrrr%r1r8r9 version_infoStringIOr9BytesIOrJrKrLrQrVrXrbrfrrrrrrrsH f    <__pycache__/ffiplatform.cpython-36.pyc000064400000006556152344566040013740 0ustar003 MZ@sddlZddlZddlmZdddddgZffd d Zdd d Zdd dZyddlm Z Wne k rvddZ YnXddZ ye e fZddlZWn ek re ZddlZYnXddZddZddZdS)N)VerificationErrorsourcesZ include_dirsZ library_dirsZ extra_objectsZdependscKsLtddlm}|g}x|D]}|jtjj|qW|f||d|S)Nr) Extension)namer)_hack_at_distutilsdistutils.corerappendospathnormpath)Z srcfilenamemodnamerkwdsrZ allsourcessrcr#/usr/lib64/python3.6/ffiplatform.py get_extensions   rc Csfttjj}zt||||}tjj|}Wdx.|jD]"\}}tjj||kr:|tj|<q:WX|S)z-Compile a C extension module using distutils.N) rr environcopy_buildr abspathitemsget)tmpdirextcompiler_verbosedebugZ saved_environZoutputfilenamekeyvaluerrrcompiles rc Csddlm}ddl}ddl}|d|gi}|j|jd}|dkrLtjj}d|f|d<d |d<d|f|d <d|f|d <yR|j j dpd}z.|j j ||j d|j d} | j\} Wd|j j |XWnD|jj|jjfk r} ztd | jj| fWYdd} ~ XnX| S) Nr) DistributionZ ext_modulesZ build_ext ffiplatformrTforceZ build_libZ build_tempz%s: %s)r!T)rr Zdistutils.errorsZ distutils.logZparse_config_filesZget_option_dictsysflagsrlogZ set_thresholdZ set_verbosityZ run_commandZget_command_objZ get_outputserrorsZ CompileErrorZ LinkErrorr __class____name__) rrrrr Z distutilsZdistZoptionsZ old_levelZcmd_objZsonameerrrr s.        &r)samefilecCstjj|tjj|kS)N)r r r)f1f2rrrr*Asr*c Cstjj|s|S|}g}xn|}tjj|\}}||ks<| r@|S|j|y$t|tjrl|jtjj|SWqt k rYqXqWdS)N) r r isabssplitr r*curdirreversejoinOSError)r dirnamesZprevdirrrrrmaybe_relative_pathDs    r5cCst|tr"|jdt||fnt|trrt|j}|jdt|x|D]}t||t|||qPWndt|tt fr|jdt|xB|D]}t||qWn*t|t r|jd|fnt d|fdS)Nz%ds%sz%ddz%dlz%diz7the keywords to verify() contains unsupported object %r) isinstancestrwritelendictsortedkeys_flattenlisttuple int_or_long TypeError)xfr<rrrrrr=_s        r=cCstj}t|||jS)N) cStringIOStringIOr=getvalue)rBrCrrrflattenrs rGc Cs0tjdkr,y ddl}Wntk r*YnXdS)NZwin32r)r#platform setuptools ImportError)rIrrrrws   r)rN)rN)r#r errorrZLIST_OF_FILE_NAMESrrrZos.pathr*rJr5intZlongr@rD NameErrorior=rGrrrrrs(     __pycache__/commontypes.cpython-36.opt-1.pyc000064400000003415152344566040014732 0ustar003 MZ @sddlZddlmZddlmZiZyddlmZeeWnek rPYnXej dded<ded <x"ej j D]Z e j d rte ee <qtW[ iZd d Zd dZejdkrejedS)N)model)FFIError)_get_common_typesFILEZ_IO_FILEZ_BoolboolZ_tc Csyt|Stk rtj||}t|ts:|d}}nZ|tjjkrXtj|d}}n<|dkrpt d|fn$||krt d|f|j |\}}||ft|<||fSXdS)Nrzset-unicode-neededzFThe Windows type %r is only available after you call ffi.set_unicode()zUnsupported type: %r. Please look at http://cffi.readthedocs.io/en/latest/cdef.html#ffi-cdef-limitations and file an issue if you think this type should really be supported.) _CACHEKeyError COMMON_TYPESget isinstancestrr PrimitiveTypeALL_PRIMITIVE_TYPESrZparse_type_and_quals)parserZ commontypeZcdeclresultZqualsr#/usr/lib64/python3.6/commontypes.pyresolve_common_types$       rc CsRtjddddgtjdtjdtjtjdgd d dgdd d d d d d d d d d S)NZ_UNICODE_STRINGZLengthZ MaximumLengthZBufferzunsigned shortZwchar_trzUNICODE_STRING *zconst UNICODE_STRING *zset-unicode-needed) ZUNICODE_STRINGZPUNICODE_STRINGZPCUNICODE_STRINGZTBYTEZTCHARZLPCTSTRZPCTSTRZLPTSTRZPTSTRZPTBYTEZPTCHARrr)rZ StructTyperZ PointerTyperrrrwin_common_types7s& rZwin32)sysrerrorrr Z _cffi_backendr ImportErrorZ unknown_typerrZ_typeendswithrrrplatformupdaterrrrs&       __pycache__/model.cpython-36.opt-1.pyc000064400000045551152344566040013464 0ustar003 MZ0T@sddlZddlZddlmZddlmZmZmZdZdZ dZ ddZ Gd d d e Z Gd d d e ZGd ddeZeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZeeZdd ZeeZGd!d"d"eZGd#d$d$eZeed%dZGd&d'd'e ZGd(d)d)eZ Gd*d+d+e Z!Gd,d-d-e Z"Gd.d/d/eZ#dN) allocate_lock) CDefErrorVerificationErrorVerificationMissingcCs@|t@rd|j}|t@r(d|j}|t@r)r)rrrr__repr__AszBaseTypeByIdentity.__repr__csfddjDS)Ncsg|]}|t|fqSr)getattr).0name)rrr Esz1BaseTypeByIdentity._get_items..)_attrs_)rr)rr _get_itemsDszBaseTypeByIdentity._get_itemsN)rrr)F) __name__ __module__ __qualname__ is_array_typeis_raw_functionr!rr"r#r,r-r3rrrrrs  rc@s$eZdZddZddZddZdS)BaseTypecCs|j|jko|j|jkS)N) __class__r3)rotherrrr__eq__Js zBaseType.__eq__cCs ||k S)Nr)rr;rrr__ne__NszBaseType.__ne__cCst|jt|jfS)N)hashr:tupler3)rrrr__hash__QszBaseType.__hash__N)r4r5r6r<r=r@rrrrr9Hsr9c@s eZdZfZddZddZdS)VoidTypecCs d|_dS)Nzvoid&)r)rrrr__init__XszVoidType.__init__cCs t||dS)NZ new_void_type) global_cache)rr(r)rrrr&[szVoidType.build_backend_typeN)r4r5r6r2rBr&rrrrrAUsrAc@seZdZddZdS)BasePrimitiveTypecCsdS)NFr)rrrris_complex_typebsz!BasePrimitiveType.is_complex_typeN)r4r5r6rErrrrrDasrDc4@seZdZdZdddddddddddddddddddddddddddddddddddddddddddddddddddd3ZddZd d Zd d Zd dZddZ ddZ dS) PrimitiveTyper0cifj)3charZshortintlongz long longz signed charz unsigned charzunsigned shortz unsigned intz unsigned longzunsigned long longfloatZdoublez long doublezfloat _Complexzdouble _ComplexZ_BoolZwchar_tZchar16_tZchar32_tZint8_tZuint8_tZint16_tZuint16_tZint32_tZuint32_tZint64_tZuint64_tZ int_least8_tZ uint_least8_tZ int_least16_tZuint_least16_tZ int_least32_tZuint_least32_tZ int_least64_tZuint_least64_tZ int_fast8_tZ uint_fast8_tZ int_fast16_tZ uint_fast16_tZ int_fast32_tZ uint_fast32_tZ int_fast64_tZ uint_fast64_tZintptr_tZ uintptr_tZintmax_tZ uintmax_tZ ptrdiff_tZsize_tZssize_tcCs||_|d|_dS)Nr)r0r)rr0rrrrBszPrimitiveType.__init__cCs|j|jdkS)NrG)ALL_PRIMITIVE_TYPESr0)rrrr is_char_typeszPrimitiveType.is_char_typecCs|j|jdkS)NrH)rOr0)rrrrr#szPrimitiveType.is_integer_typecCs|j|jdkS)NrI)rOr0)rrrr is_float_typeszPrimitiveType.is_float_typecCs|j|jdkS)NrJ)rOr0)rrrrrEszPrimitiveType.is_complex_typecCst||d|jS)NZnew_primitive_type)rCr0)rr(r)rrrr&sz PrimitiveType.build_backend_typeN)r0) r4r5r6r2rOrBrPr#rQrEr&rrrrrFfstrFc@s(eZdZd ZddZddZddZdS) UnknownIntegerTyper0cCs||_|d|_dS)Nr)r0r)rr0rrrrBszUnknownIntegerType.__init__cCsdS)NTr)rrrrr#sz"UnknownIntegerType.is_integer_typecCstd|jdS)Nz4integer type '%s' can only be used after compilation)NotImplementedErrorr0)rr(r)rrrr&sz%UnknownIntegerType.build_backend_typeN)r0)r4r5r6r2rBr#r&rrrrrRsrRc@s eZdZdZddZddZdS)UnknownFloatTyper0cCs||_|d|_dS)Nr)r0r)rr0rrrrBszUnknownFloatType.__init__cCstd|jdS)Nz2float type '%s' can only be used after compilation)rSr0)rr(r)rrrr&sz#UnknownFloatType.build_backend_typeN)r0)r4r5r6r2rBr&rrrrrTsrTc@seZdZdZd ddZdS) BaseFunctionTypeargsr ellipsisabiNcCs||_||_||_||_dd|jD}|jr8|jd|p@dg}|jdj|f}|dk r||dd|d|dd}|jjjd||_dS) NcSsg|] }|jqSr)r)r/argrrrr1sz-BaseFunctionType.__init__..z...voidz, rrr) rVr rWrXappend _base_patternjoinrr)rrVr rWrXZreprargsrrrrrBs   zBaseFunctionType.__init__)rVr rWrX)N)r4r5r6r2rBrrrrrUsrUc@s$eZdZdZdZddZddZdS)RawFunctionTypez(&)(%s)TcCstd|fdS)NzPcannot render the type %r: it is a function type, not a pointer-to-function type)r)rr(r)rrrr&sz"RawFunctionType.build_backend_typecCst|j|j|j|jS)N)FunctionPtrTyperVr rWrX)rrrras_function_pointersz#RawFunctionType.as_function_pointerN)r4r5r6r\r8r&r`rrrrr^sr^c@s eZdZdZddZddZdS)r_z(*&)(%s)cCs|jj||}g}x |jD]}|j|j||qWf}|jdkrl|jsly|jjf}Wntk rjYnXt ||dt |||jf|S)NZ __stdcallZnew_function_type) r r,rVr[rXrW_backendZ FFI_STDCALLAttributeErrorrCr?)rr(r)r rVtpZabi_argsrrrr&s  z"FunctionPtrType.build_backend_typecCst|j|j|j|jS)N)r^rVr rWrX)rrrras_raw_functionszFunctionPtrType.as_raw_functionN)r4r5r6r\r&rdrrrrr_sr_c@s"eZdZd Zd ddZddZdS) PointerTypetotyper rcCs>||_||_t|d}|jr*d|jf}|jjd||_dS)Nz *&z(%s)r)rfr rr7r rr)rrfr ZextrarrrrBs  zPointerType.__init__cCs |jj||dd}t||d|S)NT)r*new_pointer_type)rfr,rC)rr(r)ZBItemrrrr&szPointerType.build_backend_typeN)rfr )r)r4r5r6r2rBr&rrrrres recCs t|tS)N)rer )rfrrrConstPointerTypesrhc@seZdZdZdddZdS) NamedPointerTyperfr0rcCs"tj|||||_|d|_dS)Nr)rerBr0r)rrfr0r rrrrBszNamedPointerType.__init__N)rfr0)r)r4r5r6r2rBrrrrrisric@s,eZdZd ZdZddZddZdd Zd S) ArrayTypeitemlengthTcCsF||_||_|dkrd}n|dkr(d}nd|}|jjjd||_dS)Nz&[]z...z &[/*...*/]z&[%s]r)rkrlrr)rrkrlZbracketsrrrrB)szArrayType.__init__cCs t|j|S)N)rjrk)rZ newlengthrrrresolve_length6szArrayType.resolve_lengthcCsJ|jdkrtd|f|jj||t|jj||}t||d||jS)Nz...z)cannot render the type %r: unknown lengthZnew_array_type)rlrrkr,rerC)rr(r)ZBPtrItemrrrr&9s   zArrayType.build_backend_typeN)rkrl)r4r5r6r2r7rBrmr&rrrrrj%s  rjrKc@s,eZdZd ZdZddZddZddZdS) StructOrUnionOrEnumr0NcCs$|jpd|j|jf}|d|_dS)Nz%s %sr) forcenamekindr0r)rr0rrrbuild_c_name_with_markerHsz,StructOrUnionOrEnum.build_c_name_with_markercCs||_|jdS)N)rorq)rrorrrforce_the_nameLsz"StructOrUnionOrEnum.force_the_namecCs|jddS)Nr)r)rrrrget_official_namePsz%StructOrUnionOrEnum.get_official_name)r0)r4r5r6r2rorqrrrtrrrrrnDs rnc@sjeZdZdZdZdZdZdddZddZdd d Z d d Z dd dZ ddZ ddZ ddZddZdS) StructOrUnionNrFcCs*||_||_||_||_||_|jdS)N)r0fldnamesfldtypes fldbitsizefldqualsrq)rr0rvrwrxryrrrrB[s zStructOrUnion.__init__ccsB|jdk r>x2t|j|jD] \}}|dkrt|tr|VqWdS)Nr)rwziprv isinstanceru)rr0typerrranonymous_struct_fieldscs z%StructOrUnion.anonymous_struct_fieldsTccs|j}|dkrdt|j}xbt|j|j|j|D]J\}}}}|dkrnt|trn|rnx&|jD] }|Vq^Wq2||||fVq2WdS)Nrr)r) rylenrvrzrwrxr{ru enumfields)rZexpand_anonymous_struct_unionryr0r|bitsizer r rrrris  zStructOrUnion.enumfieldsc Cs~g}g}g}g}x@|jD]4\}}}}|j||j||j||j|qWt||_t||_t||_t||_dS)N)rr[r?rvrwrxry) rnamestypesZbitsizesryr0r|rr rrr force_flattenws      zStructOrUnion.force_flattencCs$tj||||}|s |j|||S)N)rnr,finish_backend_type)rr(r)r*r+rrrr,s   zStructOrUnion.get_cached_btypecs|jr$|jdkr td|jfdSj|}d|_|jdkrBn|jdkrfdd|jD}tt|j||j }d}|j rd}j j |||d d |nfg}|j\}}} } x&t t|jD]} || } |j| } t| to| jdkrn| jj}t| j|\}}|dkr<|jd |j|j| p4d f| j|} |jd| | f|j| dd|_| j}t| tr| jdkrn8j|}|| kr|jd |j|j| pd || f|j|qWtt|j||j |}j j |||| | d|_dS)Nrz(recursive structure declaration for '%s'rcsg|]}|jqSr)r,)r/rc)r(r)rrr1sz5StructOrUnion.finish_backend_type..rz...zfield '%s.%s' has a bogus size?z{}z=field '%s.%s' is declared as %d bytes, but is really %d bytesrsrs) completedrSr0r$rw fixedlayoutlistrzrvrxpackedraZcomplete_struct_or_unionranger~r{rjrlrkr,divmodsizeof_verification_errorrmr[)rr(r)r+rwZlstZsflagsZfieldofsZ fieldsizeZ totalsizeZtotalalignmentrHZfsizeZftypeZ BItemTypeZnlenZnrestZ BFieldTypeZ bitemsizer)r(r)rrs`                z!StructOrUnion.finish_backend_typecCs t|dS)N)r)rmsgrrrrsz!StructOrUnion._verification_errorcCs |jr|jdkrt|jdS)N)partialrrr)rrrrcheck_not_partialszStructOrUnion.check_not_partialcCs.|j|j|t||d|j|j|dS)Nz new_%s_type)key)rr[rCrprt)rr(r)rrrr&s z StructOrUnion.build_backend_type)N)T)F)r4r5r6rrrrrBr}rrr,rrrr&rrrrruUs   <ruc@seZdZdZdS) StructTypestructN)r4r5r6rprrrrrsrc@seZdZdZdS) UnionTypeunionN)r4r5r6rprrrrrsrc@sBeZdZdZdZdZdddZddZdd Zd d Z d d Z dS)EnumTypeenumFNcCs$||_||_||_||_|jdS)N)r0 enumerators enumvalues baseinttyperq)rr0rrrrrrrBs zEnumType.__init__cCs4tj|||jdkr0|j}d|jdd|_dS)Nrr_)rnrrrortr)rror0rrrrrs  zEnumType.force_the_namecCs|jr|j rt|jdS)N)rpartial_resolvedrr)rrrrrszEnumType.check_not_partialc Cs4|j|j||}t||d|j|j|j||dS)NZ new_enum_type)r)rbuild_baseinttyperCrtrr)rr(r)Z base_btyperrrr&s  zEnumType.build_backend_typec CsH|jdk r|jj||S|jr4t|j}t|j}nDddl}y tjWntk r\YnX|j d|j d}}|dkrd}t d}t d}nd}t d}t d}|j||} |j||} |j | } |j | } |d d| d>ko|dd| |>kr| S|d d| d>kr4|dd| |>kr4| St d |j dS) NrzU%r has no values explicitly defined; guessing that it is equivalent to 'unsigned int'rrLrMz unsigned intz unsigned longrz=%s values don't all fit into either 'long' or 'unsigned long'rsrs)rr,rminmaxwarningsZ__warningregistry__clear NameErrorwarnrrFrr) rr(r)Zsmallest_valueZ largest_valuerZsignZ candidate1Z candidate2Zbtype1Zbtype2Zsize1Zsize2rrrrs@          zEnumType.build_baseinttype)N) r4r5r6rprrrBrrrr&rrrrrrs rcCs2|dkrd|}t|ddd}|j|d|_|S)Nz$%s unknown_type)rrrorigin)r0 structnamercrrrr*s  rcCs(|dkrd|}t|ddd}t||S)Nz$$%s)rri)r0rrcrrrunknown_ptr_type2src CsFt|tjrtSt(tt|ds2tjt|_ t|j SQRXdS)N __typecache) r{r ModuleType_typecache_cffi_backend global_lockhasattrr|weakrefWeakValueDictionaryr)Zbackendrrr get_typecache<s  rc Os|jd||f}y |j|Stk r.YnXyt|j||}Wn4tk rx}ztd|||fWYdd}~XnX|j}t(|j|} | dkr|||<|S| SWdQRXdS)Nrz %s: %r: %s)popZ _typecacher%r.rarSrget) Zsrctyper(funcnamerVkwdsrresecacheZres1rrrrCGs  $ rCcCstd|d|S)N?rg)rC)r(r+rrr pointer_cache^srcCs@|jrsN  /  L  G   __pycache__/recompiler.cpython-36.opt-1.pyc000064400000132115152344566040014516 0ustar003 MZ@sddlZddlZddlZddlmZmZddlmZddlTdZ dZ dZ Gd d d Z Gd d d Z Gd ddZGdddZGdddZGdddZejd0krejZnGdddejZddZd1ddZd2ddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd3d,d-Zd.d/ZdS)4N) ffiplatformmodel)VerificationError)*i&i'i(c@s&eZdZd ddZddZddZdS) GlobalExprrcCs"||_||_||_||_||_dS)N)nameaddresstype_opsize check_value)selfrr r r r r"/usr/lib64/python3.6/recompiler.py__init__ s zGlobalExpr.__init__cCsd|j|j|jj|jfS)Nz' { "%s", (void *)%s, %s, (void *)%s },)rr r as_c_exprr )r rrrrszGlobalExpr.as_c_exprcCsd|jj|j|jfS)Nz b'%s%s',%d)r as_python_bytesrr )r rrras_python_exprszGlobalExpr.as_python_exprN)rr)__name__ __module__ __qualname__rrrrrrrr s rc@s,eZdZddZddZddZddZd S) FieldExprcCs"||_||_||_||_||_dS)N)r field_offset field_sizefbitsize field_type_op)r rrrrrrrrrs zFieldExpr.__init__cCs>dt|j}d|j|jfd||jfd||jjfS)N z { "%s", %s, z %s %s, z %s %s },)lenrrrrr)r Zspacesrrrr#szFieldExpr.as_c_exprcCstdS)N)NotImplementedError)r rrrr)szFieldExpr.as_python_exprcCsD|jjtkrd}n|jjtkr*t|j}ntd|jj||jfS)Nz b'%s%s%s') ropOP_NOOP OP_BITFIELDformat_four_bytesrrrr)r Z size_exprrrras_field_python_expr,s    zFieldExpr.as_field_python_exprN)rrrrrrr$rrrrrsrc@s$eZdZddZddZddZdS)StructUnionExprc Cs4||_||_||_||_||_||_||_||_dS)N)r type_indexflagsr alignmentcommentfirst_field_indexc_fields) r rr&r'r r(r)r*r+rrrr8szStructUnionExpr.__init__cCsPd|j|j|jfd|j|jfd|jt|jf|jrFd|jnddS)Nz { "%s", %d, %s,z %s, %s, z%d, %d z /* %s */ rz},) rr&r'r r(r*rr+r))r rrrrCszStructUnionExpr.as_c_exprcCs>t|jt}dd|jD}dt|jt||jdj|fS)NcSsg|] }|jqSr)r$).0Zc_fieldrrr Lsz2StructUnionExpr.as_python_expr..z(b'%s%s%s',%s),)evalr'ZG_FLAGSr+r#r&rjoin)r r'Z fields_exprrrrrJs  zStructUnionExpr.as_python_exprN)rrrrrrrrrrr%7s r%c@s$eZdZddZddZddZdS)EnumExprcCs"||_||_||_||_||_dS)N)rr&r signedallenums)r rr&r r2r3rrrrUs zEnumExpr.__init__cCsd|j|j|j|j|jfS)Nz1 { "%s", %d, _cffi_prim_int(%s, %s), "%s" },)rr&r r2r3)r rrrr\szEnumExpr.as_c_exprcCsNdtdtd td td td td tdti|j|j f}dt |j t ||j |j fS)Nrrzb'%s%s%s\x00%s')rr)rr)r4r)r4r)r5r)r5r)r6r)r6r)Z PRIM_UINT8Z PRIM_INT8Z PRIM_UINT16Z PRIM_INT16Z PRIM_UINT32Z PRIM_INT32Z PRIM_UINT64Z PRIM_INT64r r2r#r&rr3)r prim_indexrrrras  zEnumExpr.as_python_exprN)rrrrrrrrrrr1Tsr1c@s$eZdZddZddZddZdS) TypenameExprcCs||_||_dS)N)rr&)r rr&rrrrmszTypenameExpr.__init__cCsd|j|jfS)Nz { "%s", %d },)rr&)r rrrrqszTypenameExpr.as_c_exprcCsdt|j|jfS)Nzb'%s%s')r#r&r)r rrrrtszTypenameExpr.as_python_exprN)rrrrrrrrrrr8lsr8c@seZdZdZdddZddZddZd d Zd d Zd ddddgZ ddZ dddZ ddZ ddZ ddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Zd=d>Zd?d@Z ddBdCZ!dDdEZ"dFdGZ#dHdIZ$e$Z%dJdKZ&dLdMZ'e'Z(dNdOZ)e)Z*dPdQZ+dRdSZ,dTdUZ-ddWdXZ.dYdZZ/d[d\Z0d]d^Z1d_d`Z2ddadbZ3dcddZ4dedfZ5dgdhZ6didjZ7dkdlZ8dmdnZ9dodpZ:dqdrZ;dsdtZZ?dwdxZ@dydzZAd{d|ZBd}d~ZCddZDeDZEZFddZGddZHddZIddZJddZKddZLddZMeMZNeMZOddZPddZQddZReRZSddZTdAS) RecompilerrFcCs||_||_||_t|_dS)N)ffi module_nametarget_is_python VERSION_BASE_version)r r:r;r<rrrr~szRecompiler.__init__cCst|j||_dS)N)maxr>)r Zverrrr needs_versionszRecompiler.needs_versioncCsi|_|jdt|jtd}g|_xt|D]l}|jr*t|j|j|<|jj|x8|jD].}|j|dkrzt|j|j|<|jj|qXW|jjdq*WxZ|D]R}|j r|j|dkrt|j|j|<|jj||j r|j dk r|jjdqWi|_ i|_ x@|D]8}t |tjr&d|j |<nt |tjrd|j |<qWx.tt|j dddD]\}}||j |<q\Wx.tt|j dddD]\}}||j |<qWx0|D](}t|d|jj}|||j|qWx|jD]}qWt|j|_dS) NZ collecttype)keyZENDZLENcSs|jS)N)r)tprrrsz/Recompiler.collect_type_table..cSs|jS)N)r)rBrrrrCsZ_emit_bytecode_) _typesdict _generatesortedstr cffi_typesZis_raw_functionrappendargsZ is_array_typelength_struct_unions_enums isinstancer StructOrUnionEnumType enumerategetattr __class__rtuple)r Z all_declsrBtp1imethodr rrrcollect_type_tablesL            zRecompiler.collect_type_tablecCst|tjs2t|tr.x|D]}|j|qWdS||jkrd|j|<t|tjrb|j|jntt|tjr|j dk r||j j j krxN|j D] \}}}}|j|j|||qWn x|jD]\}}|j|qWdS)N)rNrZBaseTypeByIdentityrT_do_collect_typerDFunctionPtrTypeas_raw_functionrOfldtypesr:_parser_included_declarations enumfields _field_typeZ _get_items)r rBxZname1rU_rrrrYs         zRecompiler._do_collect_typec Cs|jjjj}xt|D]\}\}}|jdd\}}yt|d||f}Wn tk rjtd|YnXy||_ |||Wqt k r} zt j | |WYdd} ~ XqXqWdS)Nrrz_generate_cpy_%s_%sz"not implemented in recompile(): %r) r:r]Z _declarationsitemsrFsplitrRAttributeErrorr_current_quals ExceptionrZattach_exception_info) r step_namelstrrBqualsZkindZrealnamerWerrrrEs zRecompiler._generateglobalfield struct_unionenumtypenamecCsi|_x|jD]}g|j|<qWt|_|jd|jx>|jD]4}|j|}|dkrh|jdddt||j|<qBW|jd}x|jj D]\}}qW|jd}x|j j D]\}}qWdS)NZctxrmcSs|jS)N)r)entryrrrrCsz0Recompiler.collect_step_tables..)rArnro) _lsts ALL_STEPSset_seen_struct_unionsrE_add_missing_struct_unionssortrTrLrcrM)r rhrirBrVrrrcollect_step_tabless$      zRecompiler.collect_step_tablesrcCs|jj|ddS)N )_fwrite)r Zwhatrrr_prntszRecompiler._prntcCs"|jr|j|n |j||dS)N)r<write_py_source_to_fwrite_c_source_to_f)r fpreamblerrrwrite_source_to_f s zRecompiler.write_source_to_fcCs0ttjjtjjt|d}|j}|j|S)Nr)openospathr0dirname__file__ readlinesclose)r filenameglinesrrr_rel_readlinesszRecompiler._rel_readlinesc Csz||_|j}|jjdk r |d|jd}|jd}|jd|||d<|dj||jjddI}|jjdk r0|d|jf|d |j |jj|d |d |d |f|d |d|f|d|d|f|d|jd}|jd}|jd|||d<|dj||j t |d|||||d||dt dd|j jD}xLt|jD]>\}}d} ||krd||j} |d||j| fqW|js|d|d|t|_|jdi} xn|jD]d} |j| } t| | | <| | dkr|d| | fx| D]} || jqBW|d|qW|jjr|d xr|jjD]f}y|jdd!\}}Wn(tk rtd"|j|fYnX|dkrtd#|d$|fqW|d%|d||d&|d'x8|jD].} | | dkr@|d(| n |d)| q Wx.|jD]$} | d*krZ|d+| | | fqZW|jjr|d,n|d-|d.t|jfd}|jr|dO}|d/||d||d0|d1|d||d |d2|d3|f|d4|jrD|d5|d6|d7|d8|j|d9|d:|d;|d|d<|d=|d>|d?|d@|f|dA|dB|f|dC|dC|d |d2|dD|f|d4|dE|j|jf|d<|d|d2|dF|f|d4|dG|j|jf|d<|d||d0|dH|dd|_dS)JNz#define _CFFI_USE_EMBEDDINGz_cffi_include.hz#include "parse_c_type.h" zparse_c_type.hrr.z#define _CFFI_MODULE_NAME "%s"z1static const char _CFFI_PYTHON_STARTUP_CODE[] = {z0 };z#ifdef PYPY_VERSIONz5# define _CFFI_PYTHON_STARTUP_FUNC _cffi_pypyinit_%sz#elif PY_MAJOR_VERSION >= 3z-# define _CFFI_PYTHON_STARTUP_FUNC PyInit_%sz#elsez*# define _CFFI_PYTHON_STARTUP_FUNC init%sz#endifz _embedding.hz#include "_cffi_errors.h" z_cffi_errors.hz>/************************************************************/zstatic void *_cffi_types[] = {cSsg|]\}}||fqSrr)r,rBrVrrrr-Gsz2Recompiler.write_c_source_to_f..z // z/* %2d */ %s,%sz 0z};declrz.static const struct _cffi_%s_s _cffi_%ss[] = {z.static const char * const _cffi_includes[] = {r4zQffi object %r includes %r, but the latter has not been prepared with set_source()zMnot implemented yet: ffi.include() of a Python-based ffi inside a C-based ffiz "%s",z NULLz?static const struct _cffi_type_context_s _cffi_type_context = {z _cffi_types,z _cffi_%ss,z NULL, /* no %ss */rmz %d, /* num_%ss */z _cffi_includes,z NULL, /* no includes */z %d, /* num_types */z %d, /* flags */z#ifdef __GNUC__z?# pragma GCC visibility push(default) /* for -fvisibility= */ZPyMODINIT_FUNCz"_cffi_pypyinit_%s(const void *p[]){z% if (((intptr_t)p[0]) >= 0x0A03) {zQ _cffi_call_python_org = (void(*)(struct _cffi_externpy_s *, char *))p[1];z }z p[0] = (const void *)0x%x;z p[1] = &_cffi_type_context;z#if PY_MAJOR_VERSION >= 3z return NULL;}z# ifdef _MSC_VERz PyMODINIT_FUNCz# if PY_MAJOR_VERSION >= 3z% PyInit_%s(void) { return NULL; }z# elsez init%s(void) { }z# endifzPyInit_%s(void)z5 return _cffi_init("%s", 0x%x, &_cffi_type_context);z init%s(void)z. _cffi_init("%s", 0x%x, &_cffi_type_context);z# pragma GCC visibility pop)rzr|r: _embeddingrindexr0r;rd_print_string_literal_in_arrayr@VERSION_EMBEDDEDdictrDrcrQrH _get_c_namerrt_seen_constantsrErsrrr_included_ffis_assigned_sourcerer _num_externpyr>)r rrprntrrVZbase_module_nametypeindex2typer r)Znumsrhrirqffi_to_includeincluded_module_nameincluded_sourcer'rrrr~s                         zRecompiler.write_c_source_to_fcsbt|trd|fSt|ttfrZfdd|D}t|dkrJ|jdddj|fS|jS)Nzb'%s'csg|]}j|qSr)_to_py)r,item)r rrr-sz%Recompiler._to_py..rrz(%s)r.)rNrGlistrTrrIr0r)r raZrepr)r rrs    zRecompiler._to_pyc Cs||_|j}|d|dt|jjp(f}xzt|D]n}|jj|}y|jdd\}}Wn&tk rtd|j|fYnX|dk rtd|d||fq6W||d|j f|d|j fd|_ t |j |_ d d |j D}|d |j d j|ftd d |jjD} xH|jD]>} |j| } t| dkr*| dkr*|d| |j | fq*W|dkr|ddjdd t|Df|ddS)Nz# auto-generated filezimport _cffi_backendr4zQffi object %r includes %r, but the latter has not been prepared with set_source()zMnot implemented yet: ffi.include() of a C-based ffi inside a Python-based ffizfrom %s import ffi as _ffi%dzffi = _cffi_backend.FFI('%s',z _version = 0x%x,cSsg|] }|jqSr)r)r,r rrrr-sz3Recompiler.write_py_source_to_f..z _types = %s,rcSsg|]\}}||fqSrr)r,rBrVrrrr-srrmz _%ss = %s,z _includes = (%s,),z, cSsg|] }d|qS)z_ffi%dr)r,rVrrrr-s))rzr|rr:rrangerrerr;r>rTrHrr0rrDrcrsrr) r rrZ num_includesrVrrrZ types_lstrrhrirrrr}sB     zRecompiler.write_py_source_to_fcCs |j|S)N)rD)r typerrr _gettypenumszRecompiler._gettypenumc Cspd}t|tjr|j r|jr<|jdkr.z return NULL;zarg%dz return NULLz Py_BEGIN_ALLOW_THREADSz _cffi_restore_errno();cSsg|] }d|qS)zx%dr)r,rVrrrr-sz { %s%s(%s); }z _cffi_save_errno();z Py_END_ALLOW_THREADSz (void)self; /* unused */z (void)noarg; /* unused */z return %s;resultz result typez Py_INCREF(Py_None);z return Py_None;z#elsecSs t|tjpt|tjo|jS)N)rNrrO PrimitiveTyper)rrrrneed_indirections  z@Recompiler._generate_cpy_function_decl..need_indirectionFrTz %sx%dz%sx%dz *resultz *result = z%s_cffi_f_%s(%s)z return result;z# define _cffi_f_%s _cffi_d_%sz#endif)r_generate_cpy_constant_declr|rrJrQrIrr0abirrNrVoidTypertrrrrinsertZ void_type)r rBrrnumargsZargname argumentsZcall_argumentsrrVrrepr_argumentsrname_and_argumentsZ result_codeargrrZ result_declrngr differenceZ indirectionZ tp_resultrrr_generate_cpy_function_decls                     z&Recompiler._generate_cpy_function_declcCs|jr|j r|j||dS|j|j}t|j}|jrBt}n |dkrPt}n|dkr^t }nt }|j dj t |d|t||d|ddS)Nrrrlz _cffi_f_%sz _cffi_d_%s)r )rr<_generate_cpy_constant_ctxrDr[rrJZOP_DLOPEN_FUNCZOP_CPYTHON_BLTN_NZOP_CPYTHON_BLTN_OZOP_CPYTHON_BLTN_VrrrIrCffiOp)r rBrr&rZ meth_kindrrr_generate_cpy_function_ctx#s     z%Recompiler._generate_cpy_function_ctxcCsTt|tjrP|j}|dkr0|jd}d||f}|j|d||j}tj||}|S)Nz...rz_cffi_array_len(((%s)0)->%s)z%s[0])rNrrrKrr`r)r Z tp_structZ field_nameZtp_field actual_lengthZptr_struct_nametp_itemrrrr`9s     zRecompiler._field_typecCs0|j||jr,x|jD]}|j|qWdS)N)rYr<anonymous_struct_fields_struct_collecttype)r rBfldtyperrrrEs zRecompiler._struct_collecttypec Cs<|jdkrdS|j}d|f}|d|d||f|d|d|dx|jD]\}}}} y||jsv|dkr|d|||fwXx2t|tjr|jdks|jd kr|j}|d }qW|d |j d d || d|fWqXt k r} z|dt | WYdd} ~ XqXXqXW|d|d||f|dS)Nz_cffi_checkfld_%sZ_CFFI_UNUSED_FNzstatic void %s(%s *p)rz8 /* only to generate compile-time warnings or errors */z (void)p;rz> (void)((p->%s) | 0); /* check that '%s.%s' is an integer */z...z[0]z { %s = &p->%s; (void)tmp; }z*tmpzfield %r)rjz /* %s */rz(struct _cffi_align_%s { char x; %s y; };) r\r|r_rrNrrrKrrrrG) r rBrrrZ checkfuncnameZfnameZftyperfqualrkrrr _struct_declLs6   &zRecompiler._struct_declNc Cs|j|}d}g}t|tjr(|jd|jdkr@|jdd}||jjjkr|dksd||jjjkr|jdkrpn|j st |j rn |jd|j r|jdn|jdd}dj |pd }g}|dkrZ|j } t|j| } x | D]\} } } }|j|| | } |j| d |j| ft}| d kr6t}d | }nL|dksZt| tjr`| jdkr`d }n"d|dkrv|jdn|j| f}|dks| d krd }n,|dk rd|j| f}nd|jd| f}|jt| ||| t||j| qWt|jd}|jdj||dkr$d}d}d}n4|dk r@d|jf}d}nd|f}d|f}d}nd }d}d}|}|jdjt|j||||||||jj |dS) NZ _CFFI_F_UNIONZ_CFFI_F_OPAQUEZopaqueZ_CFFI_F_CHECK_FIELDSZ_CFFI_F_PACKEDZ_CFFI_F_EXTERNALZexternal|0z field '%s.%s'rz %d /* bits */z (size_t)-1zsizeof(((%s)0)->%s)rz"((char *)&((%s)0)->%s) - (char *)0zoffsetof(%s, %s)rrmz (size_t)-2r4Zunnamedzsizeof(*(%s)0)z-1 /* unknown alignment */z sizeof(%s)z"offsetof(struct _cffi_align_%s, y)rrnrr)!rDrNrZ UnionTyperIr\r:r]r^partialanyrZpackedr0r<rr_r`_check_not_opaquerr!r"rrKrrrrrrextendr%rur)r rBrrrr&Zreason_for_not_expandingr'r+Zexpand_anonymous_struct_unionr_Zfldnamerrrr r offsetr*Zalignr)rrrrms                       zRecompiler._struct_ctxcCs>xt|tjr|j}qWt|tjr:|jdkr:td|dS)Nz0%s is of an opaque type (not declared in cdef()))rNrrrrOr\r)r rBlocationrrrrs  zRecompiler._check_not_opaquecCst|jj}|jdddx|D]\}}||jkr$|jrJtd|f|jjdrx|jddj rx|jdd}n4|jdkr|j dkrd}|j |dntd |f|j |d|q$WdS) NcSs|dS)Nrr)Ztp_orderrrrrCsz7Recompiler._add_missing_struct_unions..)rAzDinternal inconsistency: %r is partial but was not seen at this point$rZ_IO_FILEFILEzinternal inconsistency: %r) rrLrcrwrurrr startswithisdigitZ forcenamerr)r rirBorderrrrrrvs   z%Recompiler._add_missing_struct_unionscCs|j|dS)N)r)r rBrrrr _generate_cpy_struct_collecttypesz+Recompiler._generate_cpy_struct_collecttypecCs2|jd}d|kr"||jddfS|d|fSdS)Nrrrb)rr)r rBrrrr _struct_namess zRecompiler._struct_namescCs|j|f|j|dS)N)rr)r rBrrrr_generate_cpy_struct_declsz$Recompiler._generate_cpy_struct_declcCs|j|f|j|dS)N)rr)r rBrrrr_generate_cpy_struct_ctxsz#Recompiler._generate_cpy_struct_ctxcCs(t|tjr|j||n |j|dS)N)rNrrP_generate_cpy_enum_collecttyper)r rBrrrr#_generate_cpy_anonymous_collecttypes z.Recompiler._generate_cpy_anonymous_collecttypecCs.t|tjr|j|n|j||d|dS)Ntypedef_)rNrrP_generate_cpy_enum_declr)r rBrrrr_generate_cpy_anonymous_decls  z'Recompiler._generate_cpy_anonymous_declcCs0t|tjr|j||n|j||d|dS)Nr)rNrrP _enum_ctxr)r rBrrrr_generate_cpy_anonymous_ctxs z&Recompiler._generate_cpy_anonymous_ctxconstcCs||f|jkrtd||f|jj||f|j}d||f}|r|d||d|d|f|d||f|dk r|dkrd|f}|d |f|d |d |d n2|d ||d|d|jd|f|d |dS)Nz duplicate declaration of %s '%s'z _cffi_%s_%sz$static int %s(unsigned long long *o)rz int n = (%s) <= 0;zI *o = (unsigned long long)((%s) | 0); /* check that %s is an integer */rz%dUz" if (!_cffi_check_int(*o, n, %s))z n |= 2;z return n;rzstatic void %s(char *o)z *(%s)o = %s;r)rrrr|r)r is_intrrBcategoryr rfuncnamerrr_generate_cpy_consts0      zRecompiler._generate_cpy_constcCs"|j}| s|jr|j|dS)N)rr<rY)r rBrrrrr"_generate_cpy_constant_collecttype&s z-Recompiler._generate_cpy_constant_collecttypecCs|j}|j|||dS)N)rr)r rBrrrrrr+sz&Recompiler._generate_cpy_constant_declcCs`|j r|jrttd}n$|jr(t}nt}|j|}t||}|jdjt |d||dS)Nrrlz_cffi_const_%sr) r<rrOP_CONSTANT_INTZOP_DLOPEN_CONSTZ OP_CONSTANTrDrrrIr)r rBrr Z const_kindr&rrrr/s    z%Recompiler._generate_cpy_constant_ctxcCs|j|dS)N)rY)r rBrrrrr?sz)Recompiler._generate_cpy_enum_collecttypecCs x|jD]}|jd|qWdS)NT) enumeratorsr)r rBr enumeratorrrrrBs z"Recompiler._generate_cpy_enum_declc Cs|j|}ttd }|jr"|jx:t|j|jD](\}}|jdj t |d|||dq2W|dk rd|kr|j rd|}d|}n4|j |j g} |j j | }tt|j j| d dk}d j|j} |jd j t|j|||| dS) Nrrlz_cffi_const_%s)r rz sizeof(%s)z ((%s)-1) <= 0rr.rorr)rDrOP_ENUMr<Zcheck_not_partialzipr Z enumvaluesrrrIrZbuild_baseinttyper:Zsizeofintcastr0r1r) r rBrr&r r Z enumvaluer r2Zbasetpr3rrrrFs"        zRecompiler._enum_ctxcCs|j||jdS)N)rr)r rBrrrr_generate_cpy_enum_ctx[sz!Recompiler._generate_cpy_enum_ctxcCsdS)Nr)r rBrrrr_generate_cpy_macro_collecttypeasz*Recompiler._generate_cpy_macro_collecttypecCs&|dkrd}n|}|jd||ddS)Nz...T)r )r)r rBrr rrr_generate_cpy_macro_decldsz#Recompiler._generate_cpy_macro_declcCsT|dkr"|jrtd|fd}n|}ttd}|jdjt|d|||ddS)Nz...zGcannot use the syntax '...' in '#define %s ...' when using the ABI moderrlz_cffi_const_%s)r r)r<rrrrrrIr)r rBrr r rrr_generate_cpy_macro_ctxks    z"Recompiler._generate_cpy_macro_ctxcCsFt|tjrB|j}|dkr$d|f}|j|jd|}tj||}|S)Nz...z_cffi_array_len(%s)z%s[0])rNrrrKrr)r rBZ global_namerrrrrr|s   zRecompiler._global_typecCs|j|j||dS)N)rYr)r rBrrrr"_generate_cpy_variable_collecttypesz-Recompiler._generate_cpy_variable_collecttypecCs|j}|j||}t|tjr4|jdkr4|j}d}nd}d|f}|d|j||jd|d|d||f|d|dS) Nr&z*_cffi_var_%s(void)zstatic )rjrz return %s(%s);r) r|rrNrrrKrrrf)r rBrrZ ampersandrrrr_generate_cpy_variable_decls  z&Recompiler._generate_cpy_variable_declcCsL|j||}|j|}|jr"t}nt}|jdjt|d|t||dS)Nrlz _cffi_var_%s) rrDr<Z OP_GLOBAL_VARZOP_GLOBAL_VAR_FrrrIrr)r rBrr&r rrr_generate_cpy_variable_ctxs   z%Recompiler._generate_cpy_variable_ctxcCs|j|dS)N)rY)r rBrrrr'_generate_cpy_extern_python_collecttypesz2Recompiler._generate_cpy_extern_python_collecttypecCs|j}t|jtjrd}nd|}d|jjd|f}|d||d|j||f|g}d|}x0t|jD]"\}} | jd||} |j | qtWd j |} | pd } d || f} |j d krd | } dd} t t |jdd}| |jrt |d}t|jtjr*d|jjd||jjd|f}|d||jj| f|d|d||dxft|jD]X\}} d|} t| tjs| | rd| } tj| } |d| jd|d| fqjW|d|t|jtjs|d|jjdf|d||jd7_dS)Nrz result of %sz(int)sizeof(%s)rz3static struct _cffi_externpy_s _cffi_externpy__%s =z { "%s.%s", %s };zargument of %sz a%dz, rz%s(%s) __stdcallz_cffi_stdcall cSst|tjo|jdkS)Nz long double)rNrrr)rBrrrmay_need_128_bitss z9Recompiler._extern_python_decl..may_need_128_bitsr6z!sizeof(%s) > %d ? sizeof(%s) : %dz%s%srz char a[%s];z char *p = a;za%drz *(%s)(p + %d) = %s;rz, _cffi_call_python(&_cffi_externpy__%s, p);z return *(%s)p;rr)r|rNrrrrr;rQrJrIr0rr?rrOrr)r rBrZ tag_and_spacerZsize_of_resultrrrVrrrrrZ size_of_arrr_extern_python_declsX          " zRecompiler._extern_python_declcCs|j||ddS)Nzstatic )r)r rBrrrr _generate_cpy_extern_python_declsz+Recompiler._generate_cpy_extern_python_declcCs|j||ddS)NzCFFI_DLLEXPORT )r)r rBrrrr#_generate_cpy_dllexport_python_declsz.Recompiler._generate_cpy_dllexport_python_declcCs|j||ddS)Nr)r)r rBrrrr'_generate_cpy_extern_python_plus_c_declsz2Recompiler._generate_cpy_extern_python_plus_c_declcCsR|jrtd|jrtd|j|}tt|}|jdjt |d|||dS)Nz,cannot use 'extern "Python"' in the ABI modez$a vararg function is extern "Python"rlz&_cffi_externpy__%s) r<rrrrDrZOP_EXTERN_PYTHONrrrIr)r rBrr&r rrr_generate_cpy_extern_python_ctxs   z*Recompiler._generate_cpy_extern_python_ctxcCsz|j}|dxf|jdD]X}|d|jd}x6|D].}t|dkrT||d}|dt|f7}q8W||qWdS)Nz=// # NB. this is not a string because of a size limit in MSVCTz// rLz%d,)r| splitlinesrstriprord)r srlineZ printed_linecrrrrs  z)Recompiler._print_string_literal_in_arraycCsttt|j|<dS)N)r OP_PRIMITIVEZ PRIM_VOIDrH)r rBrrrr_emit_bytecode_VoidTypesz"Recompiler._emit_bytecode_VoidTypecCst|j}tt||j|<dS)N)ZPRIMITIVE_TO_INDEXrrr'rH)r rBrr7rrr_emit_bytecode_PrimitiveTypes z'Recompiler._emit_bytecode_PrimitiveTypecCs(d|j|j|jf}tt||j|<dS)Nzl_cffi_prim_int(sizeof(%s), ( ((%s)-1) | 0 /* check that %s is an integer type */ ) <= 0))rrr'rH)r rBrr$rrr!_emit_bytecode_UnknownIntegerTypesz,Recompiler._emit_bytecode_UnknownIntegerTypecCs$d|j|jf}tt||j|<dS)Nzd_cffi_prim_float(sizeof(%s) * (((%s)1) / 2) * 2 /* integer => 0, float => 1 */ ))rrr'rH)r rBrr$rrr_emit_bytecode_UnknownFloatTypesz*Recompiler._emit_bytecode_UnknownFloatTypecCstt|j|j|j|<|d7}xR|jD]H}|j|}||krht|tjrX|j ||ntt ||j|<|d7}q(Wt |j }|j dk r|j dkr|dO}ntd|j ftt||j|<dS)Nrrr4zabi=%r)rZ OP_FUNCTIONrDrrHrJrNrrr)r!r rrrZOP_FUNCTION_END)r rBrrUZ realindexr'rrr_emit_bytecode_RawFunctionType#s        z)Recompiler._emit_bytecode_RawFunctionTypecCstt|j|j|j|<dS)N)r OP_POINTERrDrrH)r rBrrrr_emit_bytecode_PointerType6sz%Recompiler._emit_bytecode_PointerTypecCs"|j}tt|j||j|<dS)N)r[rr-rDrH)r rBrrawrrr_emit_bytecode_FunctionPtrType<sz)Recompiler._emit_bytecode_FunctionPtrTypecCs||j|j}|jdkr(tt||j|<nP|jdkrNtdt|jddfn*tt ||j|<tdt|j|j|d<dS)Nz...zitype %s badly placed: the '...' array length can only be used on global arrays or on fields of structuresz/*...*/r) rDrrKrZ OP_OPEN_ARRAYrHrrGrZOP_ARRAY)r rBrZ item_indexrrr_emit_bytecode_ArrayType@s   z#Recompiler._emit_bytecode_ArrayTypecCs|j|}tt||j|<dS)N)rLrZOP_STRUCT_UNIONrH)r rBrZ struct_indexrrr_emit_bytecode_StructTypeNs z$Recompiler._emit_bytecode_StructTypecCs|j|}tt||j|<dS)N)rMrr rH)r rBrZ enum_indexrrr_emit_bytecode_EnumTypeSs z"Recompiler._emit_bytecode_EnumType)F)r)N)NrN)N)Urrrrrr@rXrYrErsrxr|rrr~rr}rrrrrrrrrrrrrr`rrrrrvrZ_generate_cpy_union_collecttyperrZ_generate_cpy_union_declrZ_generate_cpy_union_ctxrrrrrrrrrrrrrrrrrrrZ*_generate_cpy_dllexport_python_collecttypeZ._generate_cpy_extern_python_plus_c_collecttyperrrrrZ"_generate_cpy_dllexport_python_ctxZ&_generate_cpy_extern_python_plus_c_ctxrr(r)r*r+r,r.Z_emit_bytecode_ConstPointerTypeZ_emit_bytecode_NamedPointerTyper0r1r2Z_emit_bytecode_UnionTyper3rrrrr9{s : , 4-#  ! Q     5 r9cseZdZfddZZS)NativeIOcs(t|tr|jd}tt|j|dS)Nascii)rNZunicodeencodesuperr5r{)r r$)rSrrr{\s  zNativeIO.write)rrrr{ __classcell__rr)rSrr5[sr5c Cs|rtd|ft|||dkd}|j|jt}|j|||j}y@t|d }|jt |d|krvt WdQRX|rtddSt k rd|t j f} t| d}|j |WdQRXyt j| |Wn,tk r t j|t j| |YnXd SXdS) Nz generating %s)r<rrz(already up-to-date)Fz%s.~%dwT)printr9rXrxr5rgetvaluerreadrIOErrorrgetpidr{renameOSErrorunlink) r:r;rZ target_fileverboseZ recompilerroutputf1Ztmp_filerrr_make_c_or_py_sourceas4     rFFcCst|||||S)N)rF)r:r;rZ target_c_filerCrrr make_c_source}s rGcCst||d||S)N)rF)r:r;Ztarget_py_filerCrrrmake_py_sources rHc Csj|jd}y$tjtjj|f|ddWntk rBYnX|d|7<tjj|f||fS)Nrrrr)rdrmakedirsrr0rA)Z outputdirmodname extensionpartsrrr_modname_to_files $rMcCs*t||}|j|||ft||||S)N)rRrIsetattr) patchlistclsrZnew_metholdrrr _patch_meths  rRcCs(x"t|D]\}}}t|||q WdS)N)reversedrN)rOrPrZold_methrrr_unpatch_methssrTcs\tjdkr(ddlm}t||dddtjdkrXddlm}fd d }t||d |dS) Nwin32r) MSVCCompilerZ_remove_visual_c_refcSs|S)Nr)r Z manifest_filerrrrCsz&_patch_for_embedding..darwin) CCompilercs<d|jkr,t|j|_|jjd}d|j|<|f||S)Nz-bundlez -dynamiclib)Z linker_sorr)r rJkwdsrV)old_link_shared_objectrrmy_link_shared_objects     z3_patch_for_embedding..my_link_shared_objectZlink_shared_object)sysplatformZdistutils.msvc9compilerrVrRZdistutils.ccompilerrX)rOrVrXr[r)rZr_patch_for_embeddings      r^cslddlm}jdrRdd tjdkr6d7ntjdkrJd7nd 7t||d fd d dS)Nr) build_extz.*r4rUz.dllrWz.dylibz.soZget_ext_filenamecsS)Nr)r Zext_name)targetrrrCsz#_patch_for_target..r)Zdistutils.command.build_extr_endswithr\r]rR)rOr`r_r)r`r_patch_for_targets       rbrT.cc  Kst|ts|jd}|jr$|j| |dk r^|jdk } | rF|j| |dkrzt|||\}} |rl|g| } tj j | }n|}| dkr| rd|} nd} t j ||f| }t |||||d}|rTg}tj}zn| rt|| dkrt|| |r|dkrd}nd}td|tj j|ftj|t jd||| }Wdtj|t|X|S||fSn<|dkrxt||d \}}t||||d}|r|Sd|fSdS) Nr6z%s.*r)rCrzthe current directory isz setting the current directory toz%s %rz.py)rNrGr7Z_windows_unicodeZ_apply_windows_unicoderZ_apply_embedding_fixrMrrr0rZ get_extensionrGgetcwdr^rbr;abspathchdircompilerTrH)r:r;rtmpdirZcall_c_compilerZc_fileZsource_extensionZextradirZcompiler_verboser`debugrYZ embeddingrLZ ext_c_fileZextupdatedrOcwdmsgoutputfilenamerbrrr recompilesb                  rnc Osddlm}ddl}|jdt|t|||f||}|j||}xFt|jD]8} | j dsPt |j| } | t || t k rPt || | qPWdd} | |_ x6t|D]*} | j d rt|j|  rt || tqW|jS)Nr)udirrhrbc_stdS)N)r)rJrYrrrtypeof_disabledsz _verify..typeof_disabled)Z testing.udirroimp setdefaultrGrnZ load_dynamicdirr:rrRobjectrNZ_typeofhasattrNotImplementedlib) r:r;rrJrYrorqrmmodulerattrrprrr_verifys     rz)r4)F)F)rTNrcNrNN) rr\iorrrerrorrZ cffi_opcoder=rrrrr%r1r8r9 version_infoStringIOr5BytesIOrFrGrHrMrRrTr^rbrnrzrrrrsH f    <_embedding.h000064400000037236152344566040007016 0ustar00 /***** Support code for embedding *****/ #ifdef __cplusplus extern "C" { #endif #if defined(_WIN32) # define CFFI_DLLEXPORT __declspec(dllexport) #elif defined(__GNUC__) # define CFFI_DLLEXPORT __attribute__((visibility("default"))) #else # define CFFI_DLLEXPORT /* nothing */ #endif /* There are two global variables of type _cffi_call_python_fnptr: * _cffi_call_python, which we declare just below, is the one called by ``extern "Python"`` implementations. * _cffi_call_python_org, which on CPython is actually part of the _cffi_exports[] array, is the function pointer copied from _cffi_backend. After initialization is complete, both are equal. However, the first one remains equal to &_cffi_start_and_call_python until the very end of initialization, when we are (or should be) sure that concurrent threads also see a completely initialized world, and only then is it changed. */ #undef _cffi_call_python typedef void (*_cffi_call_python_fnptr)(struct _cffi_externpy_s *, char *); static void _cffi_start_and_call_python(struct _cffi_externpy_s *, char *); static _cffi_call_python_fnptr _cffi_call_python = &_cffi_start_and_call_python; #ifndef _MSC_VER /* --- Assuming a GCC not infinitely old --- */ # define cffi_compare_and_swap(l,o,n) __sync_bool_compare_and_swap(l,o,n) # define cffi_write_barrier() __sync_synchronize() # if !defined(__amd64__) && !defined(__x86_64__) && \ !defined(__i386__) && !defined(__i386) # define cffi_read_barrier() __sync_synchronize() # else # define cffi_read_barrier() (void)0 # endif #else /* --- Windows threads version --- */ # include # define cffi_compare_and_swap(l,o,n) \ (InterlockedCompareExchangePointer(l,n,o) == (o)) # define cffi_write_barrier() InterlockedCompareExchange(&_cffi_dummy,0,0) # define cffi_read_barrier() (void)0 static volatile LONG _cffi_dummy; #endif #ifdef WITH_THREAD # ifndef _MSC_VER # include static pthread_mutex_t _cffi_embed_startup_lock; # else static CRITICAL_SECTION _cffi_embed_startup_lock; # endif static char _cffi_embed_startup_lock_ready = 0; #endif static void _cffi_acquire_reentrant_mutex(void) { static void *volatile lock = NULL; while (!cffi_compare_and_swap(&lock, NULL, (void *)1)) { /* should ideally do a spin loop instruction here, but hard to do it portably and doesn't really matter I think: pthread_mutex_init() should be very fast, and this is only run at start-up anyway. */ } #ifdef WITH_THREAD if (!_cffi_embed_startup_lock_ready) { # ifndef _MSC_VER pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&_cffi_embed_startup_lock, &attr); # else InitializeCriticalSection(&_cffi_embed_startup_lock); # endif _cffi_embed_startup_lock_ready = 1; } #endif while (!cffi_compare_and_swap(&lock, (void *)1, NULL)) ; #ifndef _MSC_VER pthread_mutex_lock(&_cffi_embed_startup_lock); #else EnterCriticalSection(&_cffi_embed_startup_lock); #endif } static void _cffi_release_reentrant_mutex(void) { #ifndef _MSC_VER pthread_mutex_unlock(&_cffi_embed_startup_lock); #else LeaveCriticalSection(&_cffi_embed_startup_lock); #endif } /********** CPython-specific section **********/ #ifndef PYPY_VERSION #include "_cffi_errors.h" #define _cffi_call_python_org _cffi_exports[_CFFI_CPIDX] PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(void); /* forward */ static void _cffi_py_initialize(void) { /* XXX use initsigs=0, which "skips initialization registration of signal handlers, which might be useful when Python is embedded" according to the Python docs. But review and think if it should be a user-controllable setting. XXX we should also give a way to write errors to a buffer instead of to stderr. XXX if importing 'site' fails, CPython (any version) calls exit(). Should we try to work around this behavior here? */ Py_InitializeEx(0); } static int _cffi_initialize_python(void) { /* This initializes Python, imports _cffi_backend, and then the present .dll/.so is set up as a CPython C extension module. */ int result; PyGILState_STATE state; PyObject *pycode=NULL, *global_dict=NULL, *x; state = PyGILState_Ensure(); /* Call the initxxx() function from the present module. It will create and initialize us as a CPython extension module, instead of letting the startup Python code do it---it might reimport the same .dll/.so and get maybe confused on some platforms. It might also have troubles locating the .dll/.so again for all I know. */ (void)_CFFI_PYTHON_STARTUP_FUNC(); if (PyErr_Occurred()) goto error; /* Now run the Python code provided to ffi.embedding_init_code(). */ pycode = Py_CompileString(_CFFI_PYTHON_STARTUP_CODE, "", Py_file_input); if (pycode == NULL) goto error; global_dict = PyDict_New(); if (global_dict == NULL) goto error; if (PyDict_SetItemString(global_dict, "__builtins__", PyThreadState_GET()->interp->builtins) < 0) goto error; x = PyEval_EvalCode( #if PY_MAJOR_VERSION < 3 (PyCodeObject *) #endif pycode, global_dict, global_dict); if (x == NULL) goto error; Py_DECREF(x); /* Done! Now if we've been called from _cffi_start_and_call_python() in an ``extern "Python"``, we can only hope that the Python code did correctly set up the corresponding @ffi.def_extern() function. Otherwise, the general logic of ``extern "Python"`` functions (inside the _cffi_backend module) will find that the reference is still missing and print an error. */ result = 0; done: Py_XDECREF(pycode); Py_XDECREF(global_dict); PyGILState_Release(state); return result; error:; { /* Print as much information as potentially useful. Debugging load-time failures with embedding is not fun */ PyObject *ecap; PyObject *exception, *v, *tb, *f, *modules, *mod; PyErr_Fetch(&exception, &v, &tb); ecap = _cffi_start_error_capture(); f = PySys_GetObject((char *)"stderr"); if (f != NULL && f != Py_None) { PyFile_WriteString( "Failed to initialize the Python-CFFI embedding logic:\n\n", f); } if (exception != NULL) { PyErr_NormalizeException(&exception, &v, &tb); PyErr_Display(exception, v, tb); } Py_XDECREF(exception); Py_XDECREF(v); Py_XDECREF(tb); if (f != NULL && f != Py_None) { PyFile_WriteString("\nFrom: " _CFFI_MODULE_NAME "\ncompiled with cffi version: 1.11.5" "\n_cffi_backend module: ", f); modules = PyImport_GetModuleDict(); mod = PyDict_GetItemString(modules, "_cffi_backend"); if (mod == NULL) { PyFile_WriteString("not loaded", f); } else { v = PyObject_GetAttrString(mod, "__file__"); PyFile_WriteObject(v, f, 0); Py_XDECREF(v); } PyFile_WriteString("\nsys.path: ", f); PyFile_WriteObject(PySys_GetObject((char *)"path"), f, 0); PyFile_WriteString("\n\n", f); } _cffi_stop_error_capture(ecap); } result = -1; goto done; } PyAPI_DATA(char *) _PyParser_TokenNames[]; /* from CPython */ static int _cffi_carefully_make_gil(void) { /* This does the basic initialization of Python. It can be called completely concurrently from unrelated threads. It assumes that we don't hold the GIL before (if it exists), and we don't hold it afterwards. (What it really does used to be completely different in Python 2 and Python 3, with the Python 2 solution avoiding the spin-lock around the Py_InitializeEx() call. However, after recent changes to CPython 2.7 (issue #358) it no longer works. So we use the Python 3 solution everywhere.) This initializes Python by calling Py_InitializeEx(). Important: this must not be called concurrently at all. So we use a global variable as a simple spin lock. This global variable must be from 'libpythonX.Y.so', not from this cffi-based extension module, because it must be shared from different cffi-based extension modules. We choose _PyParser_TokenNames[0] as a completely arbitrary pointer value that is never written to. The default is to point to the string "ENDMARKER". We change it temporarily to point to the next character in that string. (Yes, I know it's REALLY obscure.) */ #ifdef WITH_THREAD char *volatile *lock = (char *volatile *)_PyParser_TokenNames; char *old_value; while (1) { /* spin loop */ old_value = *lock; if (old_value[0] == 'E') { assert(old_value[1] == 'N'); if (cffi_compare_and_swap(lock, old_value, old_value + 1)) break; } else { assert(old_value[0] == 'N'); /* should ideally do a spin loop instruction here, but hard to do it portably and doesn't really matter I think: PyEval_InitThreads() should be very fast, and this is only run at start-up anyway. */ } } #endif /* call Py_InitializeEx() */ { PyGILState_STATE state = PyGILState_UNLOCKED; if (!Py_IsInitialized()) _cffi_py_initialize(); else state = PyGILState_Ensure(); PyEval_InitThreads(); PyGILState_Release(state); } #ifdef WITH_THREAD /* release the lock */ while (!cffi_compare_and_swap(lock, old_value + 1, old_value)) ; #endif return 0; } /********** end CPython-specific section **********/ #else /********** PyPy-specific section **********/ PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(const void *[]); /* forward */ static struct _cffi_pypy_init_s { const char *name; void (*func)(const void *[]); const char *code; } _cffi_pypy_init = { _CFFI_MODULE_NAME, (void(*)(const void *[]))_CFFI_PYTHON_STARTUP_FUNC, _CFFI_PYTHON_STARTUP_CODE, }; extern int pypy_carefully_make_gil(const char *); extern int pypy_init_embedded_cffi_module(int, struct _cffi_pypy_init_s *); static int _cffi_carefully_make_gil(void) { return pypy_carefully_make_gil(_CFFI_MODULE_NAME); } static int _cffi_initialize_python(void) { return pypy_init_embedded_cffi_module(0xB011, &_cffi_pypy_init); } /********** end PyPy-specific section **********/ #endif #ifdef __GNUC__ __attribute__((noinline)) #endif static _cffi_call_python_fnptr _cffi_start_python(void) { /* Delicate logic to initialize Python. This function can be called multiple times concurrently, e.g. when the process calls its first ``extern "Python"`` functions in multiple threads at once. It can also be called recursively, in which case we must ignore it. We also have to consider what occurs if several different cffi-based extensions reach this code in parallel threads---it is a different copy of the code, then, and we can't have any shared global variable unless it comes from 'libpythonX.Y.so'. Idea: * _cffi_carefully_make_gil(): "carefully" call PyEval_InitThreads() (possibly with Py_InitializeEx() first). * then we use a (local) custom lock to make sure that a call to this cffi-based extension will wait if another call to the *same* extension is running the initialization in another thread. It is reentrant, so that a recursive call will not block, but only one from a different thread. * then we grab the GIL and (Python 2) we call Py_InitializeEx(). At this point, concurrent calls to Py_InitializeEx() are not possible: we have the GIL. * do the rest of the specific initialization, which may temporarily release the GIL but not the custom lock. Only release the custom lock when we are done. */ static char called = 0; if (_cffi_carefully_make_gil() != 0) return NULL; _cffi_acquire_reentrant_mutex(); /* Here the GIL exists, but we don't have it. We're only protected from concurrency by the reentrant mutex. */ /* This file only initializes the embedded module once, the first time this is called, even if there are subinterpreters. */ if (!called) { called = 1; /* invoke _cffi_initialize_python() only once, but don't set '_cffi_call_python' right now, otherwise concurrent threads won't call this function at all (we need them to wait) */ if (_cffi_initialize_python() == 0) { /* now initialization is finished. Switch to the fast-path. */ /* We would like nobody to see the new value of '_cffi_call_python' without also seeing the rest of the data initialized. However, this is not possible. But the new value of '_cffi_call_python' is the function 'cffi_call_python()' from _cffi_backend. So: */ cffi_write_barrier(); /* ^^^ we put a write barrier here, and a corresponding read barrier at the start of cffi_call_python(). This ensures that after that read barrier, we see everything done here before the write barrier. */ assert(_cffi_call_python_org != NULL); _cffi_call_python = (_cffi_call_python_fnptr)_cffi_call_python_org; } else { /* initialization failed. Reset this to NULL, even if it was already set to some other value. Future calls to _cffi_start_python() are still forced to occur, and will always return NULL from now on. */ _cffi_call_python_org = NULL; } } _cffi_release_reentrant_mutex(); return (_cffi_call_python_fnptr)_cffi_call_python_org; } static void _cffi_start_and_call_python(struct _cffi_externpy_s *externpy, char *args) { _cffi_call_python_fnptr fnptr; int current_err = errno; #ifdef _MSC_VER int current_lasterr = GetLastError(); #endif fnptr = _cffi_start_python(); if (fnptr == NULL) { fprintf(stderr, "function %s() called, but initialization code " "failed. Returning 0.\n", externpy->name); memset(args, 0, externpy->size_of_result); } #ifdef _MSC_VER SetLastError(current_lasterr); #endif errno = current_err; if (fnptr != NULL) fnptr(externpy, args); } /* The cffi_start_python() function makes sure Python is initialized and our cffi module is set up. It can be called manually from the user C code. The same effect is obtained automatically from any dll-exported ``extern "Python"`` function. This function returns -1 if initialization failed, 0 if all is OK. */ _CFFI_UNUSED_FN static int cffi_start_python(void) { if (_cffi_call_python == &_cffi_start_and_call_python) { if (_cffi_start_python() == NULL) return -1; } cffi_read_barrier(); return 0; } #undef cffi_compare_and_swap #undef cffi_write_barrier #undef cffi_read_barrier #ifdef __cplusplus } #endif recompiler.py000064400000173664152344566040007311 0ustar00import os, sys, io from . import ffiplatform, model from .error import VerificationError from .cffi_opcode import * VERSION_BASE = 0x2601 VERSION_EMBEDDED = 0x2701 VERSION_CHAR16CHAR32 = 0x2801 class GlobalExpr: def __init__(self, name, address, type_op, size=0, check_value=0): self.name = name self.address = address self.type_op = type_op self.size = size self.check_value = check_value def as_c_expr(self): return ' { "%s", (void *)%s, %s, (void *)%s },' % ( self.name, self.address, self.type_op.as_c_expr(), self.size) def as_python_expr(self): return "b'%s%s',%d" % (self.type_op.as_python_bytes(), self.name, self.check_value) class FieldExpr: def __init__(self, name, field_offset, field_size, fbitsize, field_type_op): self.name = name self.field_offset = field_offset self.field_size = field_size self.fbitsize = fbitsize self.field_type_op = field_type_op def as_c_expr(self): spaces = " " * len(self.name) return (' { "%s", %s,\n' % (self.name, self.field_offset) + ' %s %s,\n' % (spaces, self.field_size) + ' %s %s },' % (spaces, self.field_type_op.as_c_expr())) def as_python_expr(self): raise NotImplementedError def as_field_python_expr(self): if self.field_type_op.op == OP_NOOP: size_expr = '' elif self.field_type_op.op == OP_BITFIELD: size_expr = format_four_bytes(self.fbitsize) else: raise NotImplementedError return "b'%s%s%s'" % (self.field_type_op.as_python_bytes(), size_expr, self.name) class StructUnionExpr: def __init__(self, name, type_index, flags, size, alignment, comment, first_field_index, c_fields): self.name = name self.type_index = type_index self.flags = flags self.size = size self.alignment = alignment self.comment = comment self.first_field_index = first_field_index self.c_fields = c_fields def as_c_expr(self): return (' { "%s", %d, %s,' % (self.name, self.type_index, self.flags) + '\n %s, %s, ' % (self.size, self.alignment) + '%d, %d ' % (self.first_field_index, len(self.c_fields)) + ('/* %s */ ' % self.comment if self.comment else '') + '},') def as_python_expr(self): flags = eval(self.flags, G_FLAGS) fields_expr = [c_field.as_field_python_expr() for c_field in self.c_fields] return "(b'%s%s%s',%s)" % ( format_four_bytes(self.type_index), format_four_bytes(flags), self.name, ','.join(fields_expr)) class EnumExpr: def __init__(self, name, type_index, size, signed, allenums): self.name = name self.type_index = type_index self.size = size self.signed = signed self.allenums = allenums def as_c_expr(self): return (' { "%s", %d, _cffi_prim_int(%s, %s),\n' ' "%s" },' % (self.name, self.type_index, self.size, self.signed, self.allenums)) def as_python_expr(self): prim_index = { (1, 0): PRIM_UINT8, (1, 1): PRIM_INT8, (2, 0): PRIM_UINT16, (2, 1): PRIM_INT16, (4, 0): PRIM_UINT32, (4, 1): PRIM_INT32, (8, 0): PRIM_UINT64, (8, 1): PRIM_INT64, }[self.size, self.signed] return "b'%s%s%s\\x00%s'" % (format_four_bytes(self.type_index), format_four_bytes(prim_index), self.name, self.allenums) class TypenameExpr: def __init__(self, name, type_index): self.name = name self.type_index = type_index def as_c_expr(self): return ' { "%s", %d },' % (self.name, self.type_index) def as_python_expr(self): return "b'%s%s'" % (format_four_bytes(self.type_index), self.name) # ____________________________________________________________ class Recompiler: _num_externpy = 0 def __init__(self, ffi, module_name, target_is_python=False): self.ffi = ffi self.module_name = module_name self.target_is_python = target_is_python self._version = VERSION_BASE def needs_version(self, ver): self._version = max(self._version, ver) def collect_type_table(self): self._typesdict = {} self._generate("collecttype") # all_decls = sorted(self._typesdict, key=str) # # prepare all FUNCTION bytecode sequences first self.cffi_types = [] for tp in all_decls: if tp.is_raw_function: assert self._typesdict[tp] is None self._typesdict[tp] = len(self.cffi_types) self.cffi_types.append(tp) # placeholder for tp1 in tp.args: assert isinstance(tp1, (model.VoidType, model.BasePrimitiveType, model.PointerType, model.StructOrUnionOrEnum, model.FunctionPtrType)) if self._typesdict[tp1] is None: self._typesdict[tp1] = len(self.cffi_types) self.cffi_types.append(tp1) # placeholder self.cffi_types.append('END') # placeholder # # prepare all OTHER bytecode sequences for tp in all_decls: if not tp.is_raw_function and self._typesdict[tp] is None: self._typesdict[tp] = len(self.cffi_types) self.cffi_types.append(tp) # placeholder if tp.is_array_type and tp.length is not None: self.cffi_types.append('LEN') # placeholder assert None not in self._typesdict.values() # # collect all structs and unions and enums self._struct_unions = {} self._enums = {} for tp in all_decls: if isinstance(tp, model.StructOrUnion): self._struct_unions[tp] = None elif isinstance(tp, model.EnumType): self._enums[tp] = None for i, tp in enumerate(sorted(self._struct_unions, key=lambda tp: tp.name)): self._struct_unions[tp] = i for i, tp in enumerate(sorted(self._enums, key=lambda tp: tp.name)): self._enums[tp] = i # # emit all bytecode sequences now for tp in all_decls: method = getattr(self, '_emit_bytecode_' + tp.__class__.__name__) method(tp, self._typesdict[tp]) # # consistency check for op in self.cffi_types: assert isinstance(op, CffiOp) self.cffi_types = tuple(self.cffi_types) # don't change any more def _do_collect_type(self, tp): if not isinstance(tp, model.BaseTypeByIdentity): if isinstance(tp, tuple): for x in tp: self._do_collect_type(x) return if tp not in self._typesdict: self._typesdict[tp] = None if isinstance(tp, model.FunctionPtrType): self._do_collect_type(tp.as_raw_function()) elif isinstance(tp, model.StructOrUnion): if tp.fldtypes is not None and ( tp not in self.ffi._parser._included_declarations): for name1, tp1, _, _ in tp.enumfields(): self._do_collect_type(self._field_type(tp, name1, tp1)) else: for _, x in tp._get_items(): self._do_collect_type(x) def _generate(self, step_name): lst = self.ffi._parser._declarations.items() for name, (tp, quals) in sorted(lst): kind, realname = name.split(' ', 1) try: method = getattr(self, '_generate_cpy_%s_%s' % (kind, step_name)) except AttributeError: raise VerificationError( "not implemented in recompile(): %r" % name) try: self._current_quals = quals method(tp, realname) except Exception as e: model.attach_exception_info(e, name) raise # ---------- ALL_STEPS = ["global", "field", "struct_union", "enum", "typename"] def collect_step_tables(self): # collect the declarations for '_cffi_globals', '_cffi_typenames', etc. self._lsts = {} for step_name in self.ALL_STEPS: self._lsts[step_name] = [] self._seen_struct_unions = set() self._generate("ctx") self._add_missing_struct_unions() # for step_name in self.ALL_STEPS: lst = self._lsts[step_name] if step_name != "field": lst.sort(key=lambda entry: entry.name) self._lsts[step_name] = tuple(lst) # don't change any more # # check for a possible internal inconsistency: _cffi_struct_unions # should have been generated with exactly self._struct_unions lst = self._lsts["struct_union"] for tp, i in self._struct_unions.items(): assert i < len(lst) assert lst[i].name == tp.name assert len(lst) == len(self._struct_unions) # same with enums lst = self._lsts["enum"] for tp, i in self._enums.items(): assert i < len(lst) assert lst[i].name == tp.name assert len(lst) == len(self._enums) # ---------- def _prnt(self, what=''): self._f.write(what + '\n') def write_source_to_f(self, f, preamble): if self.target_is_python: assert preamble is None self.write_py_source_to_f(f) else: assert preamble is not None self.write_c_source_to_f(f, preamble) def _rel_readlines(self, filename): g = open(os.path.join(os.path.dirname(__file__), filename), 'r') lines = g.readlines() g.close() return lines def write_c_source_to_f(self, f, preamble): self._f = f prnt = self._prnt if self.ffi._embedding is not None: prnt('#define _CFFI_USE_EMBEDDING') # # first the '#include' (actually done by inlining the file's content) lines = self._rel_readlines('_cffi_include.h') i = lines.index('#include "parse_c_type.h"\n') lines[i:i+1] = self._rel_readlines('parse_c_type.h') prnt(''.join(lines)) # # if we have ffi._embedding != None, we give it here as a macro # and include an extra file base_module_name = self.module_name.split('.')[-1] if self.ffi._embedding is not None: prnt('#define _CFFI_MODULE_NAME "%s"' % (self.module_name,)) prnt('static const char _CFFI_PYTHON_STARTUP_CODE[] = {') self._print_string_literal_in_array(self.ffi._embedding) prnt('0 };') prnt('#ifdef PYPY_VERSION') prnt('# define _CFFI_PYTHON_STARTUP_FUNC _cffi_pypyinit_%s' % ( base_module_name,)) prnt('#elif PY_MAJOR_VERSION >= 3') prnt('# define _CFFI_PYTHON_STARTUP_FUNC PyInit_%s' % ( base_module_name,)) prnt('#else') prnt('# define _CFFI_PYTHON_STARTUP_FUNC init%s' % ( base_module_name,)) prnt('#endif') lines = self._rel_readlines('_embedding.h') i = lines.index('#include "_cffi_errors.h"\n') lines[i:i+1] = self._rel_readlines('_cffi_errors.h') prnt(''.join(lines)) self.needs_version(VERSION_EMBEDDED) # # then paste the C source given by the user, verbatim. prnt('/************************************************************/') prnt() prnt(preamble) prnt() prnt('/************************************************************/') prnt() # # the declaration of '_cffi_types' prnt('static void *_cffi_types[] = {') typeindex2type = dict([(i, tp) for (tp, i) in self._typesdict.items()]) for i, op in enumerate(self.cffi_types): comment = '' if i in typeindex2type: comment = ' // ' + typeindex2type[i]._get_c_name() prnt('/* %2d */ %s,%s' % (i, op.as_c_expr(), comment)) if not self.cffi_types: prnt(' 0') prnt('};') prnt() # # call generate_cpy_xxx_decl(), for every xxx found from # ffi._parser._declarations. This generates all the functions. self._seen_constants = set() self._generate("decl") # # the declaration of '_cffi_globals' and '_cffi_typenames' nums = {} for step_name in self.ALL_STEPS: lst = self._lsts[step_name] nums[step_name] = len(lst) if nums[step_name] > 0: prnt('static const struct _cffi_%s_s _cffi_%ss[] = {' % ( step_name, step_name)) for entry in lst: prnt(entry.as_c_expr()) prnt('};') prnt() # # the declaration of '_cffi_includes' if self.ffi._included_ffis: prnt('static const char * const _cffi_includes[] = {') for ffi_to_include in self.ffi._included_ffis: try: included_module_name, included_source = ( ffi_to_include._assigned_source[:2]) except AttributeError: raise VerificationError( "ffi object %r includes %r, but the latter has not " "been prepared with set_source()" % ( self.ffi, ffi_to_include,)) if included_source is None: raise VerificationError( "not implemented yet: ffi.include() of a Python-based " "ffi inside a C-based ffi") prnt(' "%s",' % (included_module_name,)) prnt(' NULL') prnt('};') prnt() # # the declaration of '_cffi_type_context' prnt('static const struct _cffi_type_context_s _cffi_type_context = {') prnt(' _cffi_types,') for step_name in self.ALL_STEPS: if nums[step_name] > 0: prnt(' _cffi_%ss,' % step_name) else: prnt(' NULL, /* no %ss */' % step_name) for step_name in self.ALL_STEPS: if step_name != "field": prnt(' %d, /* num_%ss */' % (nums[step_name], step_name)) if self.ffi._included_ffis: prnt(' _cffi_includes,') else: prnt(' NULL, /* no includes */') prnt(' %d, /* num_types */' % (len(self.cffi_types),)) flags = 0 if self._num_externpy: flags |= 1 # set to mean that we use extern "Python" prnt(' %d, /* flags */' % flags) prnt('};') prnt() # # the init function prnt('#ifdef __GNUC__') prnt('# pragma GCC visibility push(default) /* for -fvisibility= */') prnt('#endif') prnt() prnt('#ifdef PYPY_VERSION') prnt('PyMODINIT_FUNC') prnt('_cffi_pypyinit_%s(const void *p[])' % (base_module_name,)) prnt('{') if self._num_externpy: prnt(' if (((intptr_t)p[0]) >= 0x0A03) {') prnt(' _cffi_call_python_org = ' '(void(*)(struct _cffi_externpy_s *, char *))p[1];') prnt(' }') prnt(' p[0] = (const void *)0x%x;' % self._version) prnt(' p[1] = &_cffi_type_context;') prnt('#if PY_MAJOR_VERSION >= 3') prnt(' return NULL;') prnt('#endif') prnt('}') # on Windows, distutils insists on putting init_cffi_xyz in # 'export_symbols', so instead of fighting it, just give up and # give it one prnt('# ifdef _MSC_VER') prnt(' PyMODINIT_FUNC') prnt('# if PY_MAJOR_VERSION >= 3') prnt(' PyInit_%s(void) { return NULL; }' % (base_module_name,)) prnt('# else') prnt(' init%s(void) { }' % (base_module_name,)) prnt('# endif') prnt('# endif') prnt('#elif PY_MAJOR_VERSION >= 3') prnt('PyMODINIT_FUNC') prnt('PyInit_%s(void)' % (base_module_name,)) prnt('{') prnt(' return _cffi_init("%s", 0x%x, &_cffi_type_context);' % ( self.module_name, self._version)) prnt('}') prnt('#else') prnt('PyMODINIT_FUNC') prnt('init%s(void)' % (base_module_name,)) prnt('{') prnt(' _cffi_init("%s", 0x%x, &_cffi_type_context);' % ( self.module_name, self._version)) prnt('}') prnt('#endif') prnt() prnt('#ifdef __GNUC__') prnt('# pragma GCC visibility pop') prnt('#endif') self._version = None def _to_py(self, x): if isinstance(x, str): return "b'%s'" % (x,) if isinstance(x, (list, tuple)): rep = [self._to_py(item) for item in x] if len(rep) == 1: rep.append('') return "(%s)" % (','.join(rep),) return x.as_python_expr() # Py2: unicode unexpected; Py3: bytes unexp. def write_py_source_to_f(self, f): self._f = f prnt = self._prnt # # header prnt("# auto-generated file") prnt("import _cffi_backend") # # the 'import' of the included ffis num_includes = len(self.ffi._included_ffis or ()) for i in range(num_includes): ffi_to_include = self.ffi._included_ffis[i] try: included_module_name, included_source = ( ffi_to_include._assigned_source[:2]) except AttributeError: raise VerificationError( "ffi object %r includes %r, but the latter has not " "been prepared with set_source()" % ( self.ffi, ffi_to_include,)) if included_source is not None: raise VerificationError( "not implemented yet: ffi.include() of a C-based " "ffi inside a Python-based ffi") prnt('from %s import ffi as _ffi%d' % (included_module_name, i)) prnt() prnt("ffi = _cffi_backend.FFI('%s'," % (self.module_name,)) prnt(" _version = 0x%x," % (self._version,)) self._version = None # # the '_types' keyword argument self.cffi_types = tuple(self.cffi_types) # don't change any more types_lst = [op.as_python_bytes() for op in self.cffi_types] prnt(' _types = %s,' % (self._to_py(''.join(types_lst)),)) typeindex2type = dict([(i, tp) for (tp, i) in self._typesdict.items()]) # # the keyword arguments from ALL_STEPS for step_name in self.ALL_STEPS: lst = self._lsts[step_name] if len(lst) > 0 and step_name != "field": prnt(' _%ss = %s,' % (step_name, self._to_py(lst))) # # the '_includes' keyword argument if num_includes > 0: prnt(' _includes = (%s,),' % ( ', '.join(['_ffi%d' % i for i in range(num_includes)]),)) # # the footer prnt(')') # ---------- def _gettypenum(self, type): # a KeyError here is a bug. please report it! :-) return self._typesdict[type] def _convert_funcarg_to_c(self, tp, fromvar, tovar, errcode): extraarg = '' if isinstance(tp, model.BasePrimitiveType) and not tp.is_complex_type(): if tp.is_integer_type() and tp.name != '_Bool': converter = '_cffi_to_c_int' extraarg = ', %s' % tp.name elif isinstance(tp, model.UnknownFloatType): # don't check with is_float_type(): it may be a 'long # double' here, and _cffi_to_c_double would loose precision converter = '(%s)_cffi_to_c_double' % (tp.get_c_name(''),) else: cname = tp.get_c_name('') converter = '(%s)_cffi_to_c_%s' % (cname, tp.name.replace(' ', '_')) if cname in ('char16_t', 'char32_t'): self.needs_version(VERSION_CHAR16CHAR32) errvalue = '-1' # elif isinstance(tp, model.PointerType): self._convert_funcarg_to_c_ptr_or_array(tp, fromvar, tovar, errcode) return # elif (isinstance(tp, model.StructOrUnionOrEnum) or isinstance(tp, model.BasePrimitiveType)): # a struct (not a struct pointer) as a function argument; # or, a complex (the same code works) self._prnt(' if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)' % (tovar, self._gettypenum(tp), fromvar)) self._prnt(' %s;' % errcode) return # elif isinstance(tp, model.FunctionPtrType): converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('') extraarg = ', _cffi_type(%d)' % self._gettypenum(tp) errvalue = 'NULL' # else: raise NotImplementedError(tp) # self._prnt(' %s = %s(%s%s);' % (tovar, converter, fromvar, extraarg)) self._prnt(' if (%s == (%s)%s && PyErr_Occurred())' % ( tovar, tp.get_c_name(''), errvalue)) self._prnt(' %s;' % errcode) def _extra_local_variables(self, tp, localvars): if isinstance(tp, model.PointerType): localvars.add('Py_ssize_t datasize') def _convert_funcarg_to_c_ptr_or_array(self, tp, fromvar, tovar, errcode): self._prnt(' datasize = _cffi_prepare_pointer_call_argument(') self._prnt(' _cffi_type(%d), %s, (char **)&%s);' % ( self._gettypenum(tp), fromvar, tovar)) self._prnt(' if (datasize != 0) {') self._prnt(' if (datasize < 0)') self._prnt(' %s;' % errcode) self._prnt(' %s = (%s)alloca((size_t)datasize);' % ( tovar, tp.get_c_name(''))) self._prnt(' memset((void *)%s, 0, (size_t)datasize);' % (tovar,)) self._prnt(' if (_cffi_convert_array_from_object(' '(char *)%s, _cffi_type(%d), %s) < 0)' % ( tovar, self._gettypenum(tp), fromvar)) self._prnt(' %s;' % errcode) self._prnt(' }') def _convert_expr_from_c(self, tp, var, context): if isinstance(tp, model.BasePrimitiveType): if tp.is_integer_type() and tp.name != '_Bool': return '_cffi_from_c_int(%s, %s)' % (var, tp.name) elif isinstance(tp, model.UnknownFloatType): return '_cffi_from_c_double(%s)' % (var,) elif tp.name != 'long double' and not tp.is_complex_type(): cname = tp.name.replace(' ', '_') if cname in ('char16_t', 'char32_t'): self.needs_version(VERSION_CHAR16CHAR32) return '_cffi_from_c_%s(%s)' % (cname, var) else: return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) elif isinstance(tp, (model.PointerType, model.FunctionPtrType)): return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) elif isinstance(tp, model.ArrayType): return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % ( var, self._gettypenum(model.PointerType(tp.item))) elif isinstance(tp, model.StructOrUnion): if tp.fldnames is None: raise TypeError("'%s' is used as %s, but is opaque" % ( tp._get_c_name(), context)) return '_cffi_from_c_struct((char *)&%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) elif isinstance(tp, model.EnumType): return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) else: raise NotImplementedError(tp) # ---------- # typedefs def _typedef_type(self, tp, name): return self._global_type(tp, "(*(%s *)0)" % (name,)) def _generate_cpy_typedef_collecttype(self, tp, name): self._do_collect_type(self._typedef_type(tp, name)) def _generate_cpy_typedef_decl(self, tp, name): pass def _typedef_ctx(self, tp, name): type_index = self._typesdict[tp] self._lsts["typename"].append(TypenameExpr(name, type_index)) def _generate_cpy_typedef_ctx(self, tp, name): tp = self._typedef_type(tp, name) self._typedef_ctx(tp, name) if getattr(tp, "origin", None) == "unknown_type": self._struct_ctx(tp, tp.name, approxname=None) elif isinstance(tp, model.NamedPointerType): self._struct_ctx(tp.totype, tp.totype.name, approxname=tp.name, named_ptr=tp) # ---------- # function declarations def _generate_cpy_function_collecttype(self, tp, name): self._do_collect_type(tp.as_raw_function()) if tp.ellipsis and not self.target_is_python: self._do_collect_type(tp) def _generate_cpy_function_decl(self, tp, name): assert not self.target_is_python assert isinstance(tp, model.FunctionPtrType) if tp.ellipsis: # cannot support vararg functions better than this: check for its # exact type (including the fixed arguments), and build it as a # constant function pointer (no CPython wrapper) self._generate_cpy_constant_decl(tp, name) return prnt = self._prnt numargs = len(tp.args) if numargs == 0: argname = 'noarg' elif numargs == 1: argname = 'arg0' else: argname = 'args' # # ------------------------------ # the 'd' version of the function, only for addressof(lib, 'func') arguments = [] call_arguments = [] context = 'argument of %s' % name for i, type in enumerate(tp.args): arguments.append(type.get_c_name(' x%d' % i, context)) call_arguments.append('x%d' % i) repr_arguments = ', '.join(arguments) repr_arguments = repr_arguments or 'void' if tp.abi: abi = tp.abi + ' ' else: abi = '' name_and_arguments = '%s_cffi_d_%s(%s)' % (abi, name, repr_arguments) prnt('static %s' % (tp.result.get_c_name(name_and_arguments),)) prnt('{') call_arguments = ', '.join(call_arguments) result_code = 'return ' if isinstance(tp.result, model.VoidType): result_code = '' prnt(' %s%s(%s);' % (result_code, name, call_arguments)) prnt('}') # prnt('#ifndef PYPY_VERSION') # ------------------------------ # prnt('static PyObject *') prnt('_cffi_f_%s(PyObject *self, PyObject *%s)' % (name, argname)) prnt('{') # context = 'argument of %s' % name for i, type in enumerate(tp.args): arg = type.get_c_name(' x%d' % i, context) prnt(' %s;' % arg) # localvars = set() for type in tp.args: self._extra_local_variables(type, localvars) for decl in localvars: prnt(' %s;' % (decl,)) # if not isinstance(tp.result, model.VoidType): result_code = 'result = ' context = 'result of %s' % name result_decl = ' %s;' % tp.result.get_c_name(' result', context) prnt(result_decl) else: result_decl = None result_code = '' # if len(tp.args) > 1: rng = range(len(tp.args)) for i in rng: prnt(' PyObject *arg%d;' % i) prnt() prnt(' if (!PyArg_UnpackTuple(args, "%s", %d, %d, %s))' % ( name, len(rng), len(rng), ', '.join(['&arg%d' % i for i in rng]))) prnt(' return NULL;') prnt() # for i, type in enumerate(tp.args): self._convert_funcarg_to_c(type, 'arg%d' % i, 'x%d' % i, 'return NULL') prnt() # prnt(' Py_BEGIN_ALLOW_THREADS') prnt(' _cffi_restore_errno();') call_arguments = ['x%d' % i for i in range(len(tp.args))] call_arguments = ', '.join(call_arguments) prnt(' { %s%s(%s); }' % (result_code, name, call_arguments)) prnt(' _cffi_save_errno();') prnt(' Py_END_ALLOW_THREADS') prnt() # prnt(' (void)self; /* unused */') if numargs == 0: prnt(' (void)noarg; /* unused */') if result_code: prnt(' return %s;' % self._convert_expr_from_c(tp.result, 'result', 'result type')) else: prnt(' Py_INCREF(Py_None);') prnt(' return Py_None;') prnt('}') # prnt('#else') # ------------------------------ # # the PyPy version: need to replace struct/union arguments with # pointers, and if the result is a struct/union, insert a first # arg that is a pointer to the result. We also do that for # complex args and return type. def need_indirection(type): return (isinstance(type, model.StructOrUnion) or (isinstance(type, model.PrimitiveType) and type.is_complex_type())) difference = False arguments = [] call_arguments = [] context = 'argument of %s' % name for i, type in enumerate(tp.args): indirection = '' if need_indirection(type): indirection = '*' difference = True arg = type.get_c_name(' %sx%d' % (indirection, i), context) arguments.append(arg) call_arguments.append('%sx%d' % (indirection, i)) tp_result = tp.result if need_indirection(tp_result): context = 'result of %s' % name arg = tp_result.get_c_name(' *result', context) arguments.insert(0, arg) tp_result = model.void_type result_decl = None result_code = '*result = ' difference = True if difference: repr_arguments = ', '.join(arguments) repr_arguments = repr_arguments or 'void' name_and_arguments = '%s_cffi_f_%s(%s)' % (abi, name, repr_arguments) prnt('static %s' % (tp_result.get_c_name(name_and_arguments),)) prnt('{') if result_decl: prnt(result_decl) call_arguments = ', '.join(call_arguments) prnt(' { %s%s(%s); }' % (result_code, name, call_arguments)) if result_decl: prnt(' return result;') prnt('}') else: prnt('# define _cffi_f_%s _cffi_d_%s' % (name, name)) # prnt('#endif') # ------------------------------ prnt() def _generate_cpy_function_ctx(self, tp, name): if tp.ellipsis and not self.target_is_python: self._generate_cpy_constant_ctx(tp, name) return type_index = self._typesdict[tp.as_raw_function()] numargs = len(tp.args) if self.target_is_python: meth_kind = OP_DLOPEN_FUNC elif numargs == 0: meth_kind = OP_CPYTHON_BLTN_N # 'METH_NOARGS' elif numargs == 1: meth_kind = OP_CPYTHON_BLTN_O # 'METH_O' else: meth_kind = OP_CPYTHON_BLTN_V # 'METH_VARARGS' self._lsts["global"].append( GlobalExpr(name, '_cffi_f_%s' % name, CffiOp(meth_kind, type_index), size='_cffi_d_%s' % name)) # ---------- # named structs or unions def _field_type(self, tp_struct, field_name, tp_field): if isinstance(tp_field, model.ArrayType): actual_length = tp_field.length if actual_length == '...': ptr_struct_name = tp_struct.get_c_name('*') actual_length = '_cffi_array_len(((%s)0)->%s)' % ( ptr_struct_name, field_name) tp_item = self._field_type(tp_struct, '%s[0]' % field_name, tp_field.item) tp_field = model.ArrayType(tp_item, actual_length) return tp_field def _struct_collecttype(self, tp): self._do_collect_type(tp) if self.target_is_python: # also requires nested anon struct/unions in ABI mode, recursively for fldtype in tp.anonymous_struct_fields(): self._struct_collecttype(fldtype) def _struct_decl(self, tp, cname, approxname): if tp.fldtypes is None: return prnt = self._prnt checkfuncname = '_cffi_checkfld_%s' % (approxname,) prnt('_CFFI_UNUSED_FN') prnt('static void %s(%s *p)' % (checkfuncname, cname)) prnt('{') prnt(' /* only to generate compile-time warnings or errors */') prnt(' (void)p;') for fname, ftype, fbitsize, fqual in tp.enumfields(): try: if ftype.is_integer_type() or fbitsize >= 0: # accept all integers, but complain on float or double prnt(" (void)((p->%s) | 0); /* check that '%s.%s' is " "an integer */" % (fname, cname, fname)) continue # only accept exactly the type declared, except that '[]' # is interpreted as a '*' and so will match any array length. # (It would also match '*', but that's harder to detect...) while (isinstance(ftype, model.ArrayType) and (ftype.length is None or ftype.length == '...')): ftype = ftype.item fname = fname + '[0]' prnt(' { %s = &p->%s; (void)tmp; }' % ( ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual), fname)) except VerificationError as e: prnt(' /* %s */' % str(e)) # cannot verify it, ignore prnt('}') prnt('struct _cffi_align_%s { char x; %s y; };' % (approxname, cname)) prnt() def _struct_ctx(self, tp, cname, approxname, named_ptr=None): type_index = self._typesdict[tp] reason_for_not_expanding = None flags = [] if isinstance(tp, model.UnionType): flags.append("_CFFI_F_UNION") if tp.fldtypes is None: flags.append("_CFFI_F_OPAQUE") reason_for_not_expanding = "opaque" if (tp not in self.ffi._parser._included_declarations and (named_ptr is None or named_ptr not in self.ffi._parser._included_declarations)): if tp.fldtypes is None: pass # opaque elif tp.partial or any(tp.anonymous_struct_fields()): pass # field layout obtained silently from the C compiler else: flags.append("_CFFI_F_CHECK_FIELDS") if tp.packed: flags.append("_CFFI_F_PACKED") else: flags.append("_CFFI_F_EXTERNAL") reason_for_not_expanding = "external" flags = '|'.join(flags) or '0' c_fields = [] if reason_for_not_expanding is None: expand_anonymous_struct_union = not self.target_is_python enumfields = list(tp.enumfields(expand_anonymous_struct_union)) for fldname, fldtype, fbitsize, fqual in enumfields: fldtype = self._field_type(tp, fldname, fldtype) self._check_not_opaque(fldtype, "field '%s.%s'" % (tp.name, fldname)) # cname is None for _add_missing_struct_unions() only op = OP_NOOP if fbitsize >= 0: op = OP_BITFIELD size = '%d /* bits */' % fbitsize elif cname is None or ( isinstance(fldtype, model.ArrayType) and fldtype.length is None): size = '(size_t)-1' else: size = 'sizeof(((%s)0)->%s)' % ( tp.get_c_name('*') if named_ptr is None else named_ptr.name, fldname) if cname is None or fbitsize >= 0: offset = '(size_t)-1' elif named_ptr is not None: offset = '((char *)&((%s)0)->%s) - (char *)0' % ( named_ptr.name, fldname) else: offset = 'offsetof(%s, %s)' % (tp.get_c_name(''), fldname) c_fields.append( FieldExpr(fldname, offset, size, fbitsize, CffiOp(op, self._typesdict[fldtype]))) first_field_index = len(self._lsts["field"]) self._lsts["field"].extend(c_fields) # if cname is None: # unknown name, for _add_missing_struct_unions size = '(size_t)-2' align = -2 comment = "unnamed" else: if named_ptr is not None: size = 'sizeof(*(%s)0)' % (named_ptr.name,) align = '-1 /* unknown alignment */' else: size = 'sizeof(%s)' % (cname,) align = 'offsetof(struct _cffi_align_%s, y)' % (approxname,) comment = None else: size = '(size_t)-1' align = -1 first_field_index = -1 comment = reason_for_not_expanding self._lsts["struct_union"].append( StructUnionExpr(tp.name, type_index, flags, size, align, comment, first_field_index, c_fields)) self._seen_struct_unions.add(tp) def _check_not_opaque(self, tp, location): while isinstance(tp, model.ArrayType): tp = tp.item if isinstance(tp, model.StructOrUnion) and tp.fldtypes is None: raise TypeError( "%s is of an opaque type (not declared in cdef())" % location) def _add_missing_struct_unions(self): # not very nice, but some struct declarations might be missing # because they don't have any known C name. Check that they are # not partial (we can't complete or verify them!) and emit them # anonymously. lst = list(self._struct_unions.items()) lst.sort(key=lambda tp_order: tp_order[1]) for tp, order in lst: if tp not in self._seen_struct_unions: if tp.partial: raise NotImplementedError("internal inconsistency: %r is " "partial but was not seen at " "this point" % (tp,)) if tp.name.startswith('$') and tp.name[1:].isdigit(): approxname = tp.name[1:] elif tp.name == '_IO_FILE' and tp.forcename == 'FILE': approxname = 'FILE' self._typedef_ctx(tp, 'FILE') else: raise NotImplementedError("internal inconsistency: %r" % (tp,)) self._struct_ctx(tp, None, approxname) def _generate_cpy_struct_collecttype(self, tp, name): self._struct_collecttype(tp) _generate_cpy_union_collecttype = _generate_cpy_struct_collecttype def _struct_names(self, tp): cname = tp.get_c_name('') if ' ' in cname: return cname, cname.replace(' ', '_') else: return cname, '_' + cname def _generate_cpy_struct_decl(self, tp, name): self._struct_decl(tp, *self._struct_names(tp)) _generate_cpy_union_decl = _generate_cpy_struct_decl def _generate_cpy_struct_ctx(self, tp, name): self._struct_ctx(tp, *self._struct_names(tp)) _generate_cpy_union_ctx = _generate_cpy_struct_ctx # ---------- # 'anonymous' declarations. These are produced for anonymous structs # or unions; the 'name' is obtained by a typedef. def _generate_cpy_anonymous_collecttype(self, tp, name): if isinstance(tp, model.EnumType): self._generate_cpy_enum_collecttype(tp, name) else: self._struct_collecttype(tp) def _generate_cpy_anonymous_decl(self, tp, name): if isinstance(tp, model.EnumType): self._generate_cpy_enum_decl(tp) else: self._struct_decl(tp, name, 'typedef_' + name) def _generate_cpy_anonymous_ctx(self, tp, name): if isinstance(tp, model.EnumType): self._enum_ctx(tp, name) else: self._struct_ctx(tp, name, 'typedef_' + name) # ---------- # constants, declared with "static const ..." def _generate_cpy_const(self, is_int, name, tp=None, category='const', check_value=None): if (category, name) in self._seen_constants: raise VerificationError( "duplicate declaration of %s '%s'" % (category, name)) self._seen_constants.add((category, name)) # prnt = self._prnt funcname = '_cffi_%s_%s' % (category, name) if is_int: prnt('static int %s(unsigned long long *o)' % funcname) prnt('{') prnt(' int n = (%s) <= 0;' % (name,)) prnt(' *o = (unsigned long long)((%s) | 0);' ' /* check that %s is an integer */' % (name, name)) if check_value is not None: if check_value > 0: check_value = '%dU' % (check_value,) prnt(' if (!_cffi_check_int(*o, n, %s))' % (check_value,)) prnt(' n |= 2;') prnt(' return n;') prnt('}') else: assert check_value is None prnt('static void %s(char *o)' % funcname) prnt('{') prnt(' *(%s)o = %s;' % (tp.get_c_name('*'), name)) prnt('}') prnt() def _generate_cpy_constant_collecttype(self, tp, name): is_int = tp.is_integer_type() if not is_int or self.target_is_python: self._do_collect_type(tp) def _generate_cpy_constant_decl(self, tp, name): is_int = tp.is_integer_type() self._generate_cpy_const(is_int, name, tp) def _generate_cpy_constant_ctx(self, tp, name): if not self.target_is_python and tp.is_integer_type(): type_op = CffiOp(OP_CONSTANT_INT, -1) else: if self.target_is_python: const_kind = OP_DLOPEN_CONST else: const_kind = OP_CONSTANT type_index = self._typesdict[tp] type_op = CffiOp(const_kind, type_index) self._lsts["global"].append( GlobalExpr(name, '_cffi_const_%s' % name, type_op)) # ---------- # enums def _generate_cpy_enum_collecttype(self, tp, name): self._do_collect_type(tp) def _generate_cpy_enum_decl(self, tp, name=None): for enumerator in tp.enumerators: self._generate_cpy_const(True, enumerator) def _enum_ctx(self, tp, cname): type_index = self._typesdict[tp] type_op = CffiOp(OP_ENUM, -1) if self.target_is_python: tp.check_not_partial() for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues): self._lsts["global"].append( GlobalExpr(enumerator, '_cffi_const_%s' % enumerator, type_op, check_value=enumvalue)) # if cname is not None and '$' not in cname and not self.target_is_python: size = "sizeof(%s)" % cname signed = "((%s)-1) <= 0" % cname else: basetp = tp.build_baseinttype(self.ffi, []) size = self.ffi.sizeof(basetp) signed = int(int(self.ffi.cast(basetp, -1)) < 0) allenums = ",".join(tp.enumerators) self._lsts["enum"].append( EnumExpr(tp.name, type_index, size, signed, allenums)) def _generate_cpy_enum_ctx(self, tp, name): self._enum_ctx(tp, tp._get_c_name()) # ---------- # macros: for now only for integers def _generate_cpy_macro_collecttype(self, tp, name): pass def _generate_cpy_macro_decl(self, tp, name): if tp == '...': check_value = None else: check_value = tp # an integer self._generate_cpy_const(True, name, check_value=check_value) def _generate_cpy_macro_ctx(self, tp, name): if tp == '...': if self.target_is_python: raise VerificationError( "cannot use the syntax '...' in '#define %s ...' when " "using the ABI mode" % (name,)) check_value = None else: check_value = tp # an integer type_op = CffiOp(OP_CONSTANT_INT, -1) self._lsts["global"].append( GlobalExpr(name, '_cffi_const_%s' % name, type_op, check_value=check_value)) # ---------- # global variables def _global_type(self, tp, global_name): if isinstance(tp, model.ArrayType): actual_length = tp.length if actual_length == '...': actual_length = '_cffi_array_len(%s)' % (global_name,) tp_item = self._global_type(tp.item, '%s[0]' % global_name) tp = model.ArrayType(tp_item, actual_length) return tp def _generate_cpy_variable_collecttype(self, tp, name): self._do_collect_type(self._global_type(tp, name)) def _generate_cpy_variable_decl(self, tp, name): prnt = self._prnt tp = self._global_type(tp, name) if isinstance(tp, model.ArrayType) and tp.length is None: tp = tp.item ampersand = '' else: ampersand = '&' # This code assumes that casts from "tp *" to "void *" is a # no-op, i.e. a function that returns a "tp *" can be called # as if it returned a "void *". This should be generally true # on any modern machine. The only exception to that rule (on # uncommon architectures, and as far as I can tell) might be # if 'tp' were a function type, but that is not possible here. # (If 'tp' is a function _pointer_ type, then casts from "fn_t # **" to "void *" are again no-ops, as far as I can tell.) decl = '*_cffi_var_%s(void)' % (name,) prnt('static ' + tp.get_c_name(decl, quals=self._current_quals)) prnt('{') prnt(' return %s(%s);' % (ampersand, name)) prnt('}') prnt() def _generate_cpy_variable_ctx(self, tp, name): tp = self._global_type(tp, name) type_index = self._typesdict[tp] if self.target_is_python: op = OP_GLOBAL_VAR else: op = OP_GLOBAL_VAR_F self._lsts["global"].append( GlobalExpr(name, '_cffi_var_%s' % name, CffiOp(op, type_index))) # ---------- # extern "Python" def _generate_cpy_extern_python_collecttype(self, tp, name): assert isinstance(tp, model.FunctionPtrType) self._do_collect_type(tp) _generate_cpy_dllexport_python_collecttype = \ _generate_cpy_extern_python_plus_c_collecttype = \ _generate_cpy_extern_python_collecttype def _extern_python_decl(self, tp, name, tag_and_space): prnt = self._prnt if isinstance(tp.result, model.VoidType): size_of_result = '0' else: context = 'result of %s' % name size_of_result = '(int)sizeof(%s)' % ( tp.result.get_c_name('', context),) prnt('static struct _cffi_externpy_s _cffi_externpy__%s =' % name) prnt(' { "%s.%s", %s };' % (self.module_name, name, size_of_result)) prnt() # arguments = [] context = 'argument of %s' % name for i, type in enumerate(tp.args): arg = type.get_c_name(' a%d' % i, context) arguments.append(arg) # repr_arguments = ', '.join(arguments) repr_arguments = repr_arguments or 'void' name_and_arguments = '%s(%s)' % (name, repr_arguments) if tp.abi == "__stdcall": name_and_arguments = '_cffi_stdcall ' + name_and_arguments # def may_need_128_bits(tp): return (isinstance(tp, model.PrimitiveType) and tp.name == 'long double') # size_of_a = max(len(tp.args)*8, 8) if may_need_128_bits(tp.result): size_of_a = max(size_of_a, 16) if isinstance(tp.result, model.StructOrUnion): size_of_a = 'sizeof(%s) > %d ? sizeof(%s) : %d' % ( tp.result.get_c_name(''), size_of_a, tp.result.get_c_name(''), size_of_a) prnt('%s%s' % (tag_and_space, tp.result.get_c_name(name_and_arguments))) prnt('{') prnt(' char a[%s];' % size_of_a) prnt(' char *p = a;') for i, type in enumerate(tp.args): arg = 'a%d' % i if (isinstance(type, model.StructOrUnion) or may_need_128_bits(type)): arg = '&' + arg type = model.PointerType(type) prnt(' *(%s)(p + %d) = %s;' % (type.get_c_name('*'), i*8, arg)) prnt(' _cffi_call_python(&_cffi_externpy__%s, p);' % name) if not isinstance(tp.result, model.VoidType): prnt(' return *(%s)p;' % (tp.result.get_c_name('*'),)) prnt('}') prnt() self._num_externpy += 1 def _generate_cpy_extern_python_decl(self, tp, name): self._extern_python_decl(tp, name, 'static ') def _generate_cpy_dllexport_python_decl(self, tp, name): self._extern_python_decl(tp, name, 'CFFI_DLLEXPORT ') def _generate_cpy_extern_python_plus_c_decl(self, tp, name): self._extern_python_decl(tp, name, '') def _generate_cpy_extern_python_ctx(self, tp, name): if self.target_is_python: raise VerificationError( "cannot use 'extern \"Python\"' in the ABI mode") if tp.ellipsis: raise NotImplementedError("a vararg function is extern \"Python\"") type_index = self._typesdict[tp] type_op = CffiOp(OP_EXTERN_PYTHON, type_index) self._lsts["global"].append( GlobalExpr(name, '&_cffi_externpy__%s' % name, type_op, name)) _generate_cpy_dllexport_python_ctx = \ _generate_cpy_extern_python_plus_c_ctx = \ _generate_cpy_extern_python_ctx def _print_string_literal_in_array(self, s): prnt = self._prnt prnt('// # NB. this is not a string because of a size limit in MSVC') for line in s.splitlines(True): prnt(('// ' + line).rstrip()) printed_line = '' for c in line: if len(printed_line) >= 76: prnt(printed_line) printed_line = '' printed_line += '%d,' % (ord(c),) prnt(printed_line) # ---------- # emitting the opcodes for individual types def _emit_bytecode_VoidType(self, tp, index): self.cffi_types[index] = CffiOp(OP_PRIMITIVE, PRIM_VOID) def _emit_bytecode_PrimitiveType(self, tp, index): prim_index = PRIMITIVE_TO_INDEX[tp.name] self.cffi_types[index] = CffiOp(OP_PRIMITIVE, prim_index) def _emit_bytecode_UnknownIntegerType(self, tp, index): s = ('_cffi_prim_int(sizeof(%s), (\n' ' ((%s)-1) | 0 /* check that %s is an integer type */\n' ' ) <= 0)' % (tp.name, tp.name, tp.name)) self.cffi_types[index] = CffiOp(OP_PRIMITIVE, s) def _emit_bytecode_UnknownFloatType(self, tp, index): s = ('_cffi_prim_float(sizeof(%s) *\n' ' (((%s)1) / 2) * 2 /* integer => 0, float => 1 */\n' ' )' % (tp.name, tp.name)) self.cffi_types[index] = CffiOp(OP_PRIMITIVE, s) def _emit_bytecode_RawFunctionType(self, tp, index): self.cffi_types[index] = CffiOp(OP_FUNCTION, self._typesdict[tp.result]) index += 1 for tp1 in tp.args: realindex = self._typesdict[tp1] if index != realindex: if isinstance(tp1, model.PrimitiveType): self._emit_bytecode_PrimitiveType(tp1, index) else: self.cffi_types[index] = CffiOp(OP_NOOP, realindex) index += 1 flags = int(tp.ellipsis) if tp.abi is not None: if tp.abi == '__stdcall': flags |= 2 else: raise NotImplementedError("abi=%r" % (tp.abi,)) self.cffi_types[index] = CffiOp(OP_FUNCTION_END, flags) def _emit_bytecode_PointerType(self, tp, index): self.cffi_types[index] = CffiOp(OP_POINTER, self._typesdict[tp.totype]) _emit_bytecode_ConstPointerType = _emit_bytecode_PointerType _emit_bytecode_NamedPointerType = _emit_bytecode_PointerType def _emit_bytecode_FunctionPtrType(self, tp, index): raw = tp.as_raw_function() self.cffi_types[index] = CffiOp(OP_POINTER, self._typesdict[raw]) def _emit_bytecode_ArrayType(self, tp, index): item_index = self._typesdict[tp.item] if tp.length is None: self.cffi_types[index] = CffiOp(OP_OPEN_ARRAY, item_index) elif tp.length == '...': raise VerificationError( "type %s badly placed: the '...' array length can only be " "used on global arrays or on fields of structures" % ( str(tp).replace('/*...*/', '...'),)) else: assert self.cffi_types[index + 1] == 'LEN' self.cffi_types[index] = CffiOp(OP_ARRAY, item_index) self.cffi_types[index + 1] = CffiOp(None, str(tp.length)) def _emit_bytecode_StructType(self, tp, index): struct_index = self._struct_unions[tp] self.cffi_types[index] = CffiOp(OP_STRUCT_UNION, struct_index) _emit_bytecode_UnionType = _emit_bytecode_StructType def _emit_bytecode_EnumType(self, tp, index): enum_index = self._enums[tp] self.cffi_types[index] = CffiOp(OP_ENUM, enum_index) if sys.version_info >= (3,): NativeIO = io.StringIO else: class NativeIO(io.BytesIO): def write(self, s): if isinstance(s, unicode): s = s.encode('ascii') super(NativeIO, self).write(s) def _make_c_or_py_source(ffi, module_name, preamble, target_file, verbose): if verbose: print("generating %s" % (target_file,)) recompiler = Recompiler(ffi, module_name, target_is_python=(preamble is None)) recompiler.collect_type_table() recompiler.collect_step_tables() f = NativeIO() recompiler.write_source_to_f(f, preamble) output = f.getvalue() try: with open(target_file, 'r') as f1: if f1.read(len(output) + 1) != output: raise IOError if verbose: print("(already up-to-date)") return False # already up-to-date except IOError: tmp_file = '%s.~%d' % (target_file, os.getpid()) with open(tmp_file, 'w') as f1: f1.write(output) try: os.rename(tmp_file, target_file) except OSError: os.unlink(target_file) os.rename(tmp_file, target_file) return True def make_c_source(ffi, module_name, preamble, target_c_file, verbose=False): assert preamble is not None return _make_c_or_py_source(ffi, module_name, preamble, target_c_file, verbose) def make_py_source(ffi, module_name, target_py_file, verbose=False): return _make_c_or_py_source(ffi, module_name, None, target_py_file, verbose) def _modname_to_file(outputdir, modname, extension): parts = modname.split('.') try: os.makedirs(os.path.join(outputdir, *parts[:-1])) except OSError: pass parts[-1] += extension return os.path.join(outputdir, *parts), parts # Aaargh. Distutils is not tested at all for the purpose of compiling # DLLs that are not extension modules. Here are some hacks to work # around that, in the _patch_for_*() functions... def _patch_meth(patchlist, cls, name, new_meth): old = getattr(cls, name) patchlist.append((cls, name, old)) setattr(cls, name, new_meth) return old def _unpatch_meths(patchlist): for cls, name, old_meth in reversed(patchlist): setattr(cls, name, old_meth) def _patch_for_embedding(patchlist): if sys.platform == 'win32': # we must not remove the manifest when building for embedding! from distutils.msvc9compiler import MSVCCompiler _patch_meth(patchlist, MSVCCompiler, '_remove_visual_c_ref', lambda self, manifest_file: manifest_file) if sys.platform == 'darwin': # we must not make a '-bundle', but a '-dynamiclib' instead from distutils.ccompiler import CCompiler def my_link_shared_object(self, *args, **kwds): if '-bundle' in self.linker_so: self.linker_so = list(self.linker_so) i = self.linker_so.index('-bundle') self.linker_so[i] = '-dynamiclib' return old_link_shared_object(self, *args, **kwds) old_link_shared_object = _patch_meth(patchlist, CCompiler, 'link_shared_object', my_link_shared_object) def _patch_for_target(patchlist, target): from distutils.command.build_ext import build_ext # if 'target' is different from '*', we need to patch some internal # method to just return this 'target' value, instead of having it # built from module_name if target.endswith('.*'): target = target[:-2] if sys.platform == 'win32': target += '.dll' elif sys.platform == 'darwin': target += '.dylib' else: target += '.so' _patch_meth(patchlist, build_ext, 'get_ext_filename', lambda self, ext_name: target) def recompile(ffi, module_name, preamble, tmpdir='.', call_c_compiler=True, c_file=None, source_extension='.c', extradir=None, compiler_verbose=1, target=None, debug=None, **kwds): if not isinstance(module_name, str): module_name = module_name.encode('ascii') if ffi._windows_unicode: ffi._apply_windows_unicode(kwds) if preamble is not None: embedding = (ffi._embedding is not None) if embedding: ffi._apply_embedding_fix(kwds) if c_file is None: c_file, parts = _modname_to_file(tmpdir, module_name, source_extension) if extradir: parts = [extradir] + parts ext_c_file = os.path.join(*parts) else: ext_c_file = c_file # if target is None: if embedding: target = '%s.*' % module_name else: target = '*' # ext = ffiplatform.get_extension(ext_c_file, module_name, **kwds) updated = make_c_source(ffi, module_name, preamble, c_file, verbose=compiler_verbose) if call_c_compiler: patchlist = [] cwd = os.getcwd() try: if embedding: _patch_for_embedding(patchlist) if target != '*': _patch_for_target(patchlist, target) if compiler_verbose: if tmpdir == '.': msg = 'the current directory is' else: msg = 'setting the current directory to' print('%s %r' % (msg, os.path.abspath(tmpdir))) os.chdir(tmpdir) outputfilename = ffiplatform.compile('.', ext, compiler_verbose, debug) finally: os.chdir(cwd) _unpatch_meths(patchlist) return outputfilename else: return ext, updated else: if c_file is None: c_file, _ = _modname_to_file(tmpdir, module_name, '.py') updated = make_py_source(ffi, module_name, c_file, verbose=compiler_verbose) if call_c_compiler: return c_file else: return None, updated def _verify(ffi, module_name, preamble, *args, **kwds): # FOR TESTS ONLY from testing.udir import udir import imp assert module_name not in sys.modules, "module name conflict: %r" % ( module_name,) kwds.setdefault('tmpdir', str(udir)) outputfilename = recompile(ffi, module_name, preamble, *args, **kwds) module = imp.load_dynamic(module_name, outputfilename) # # hack hack hack: copy all *bound methods* from module.ffi back to the # ffi instance. Then calls like ffi.new() will invoke module.ffi.new(). for name in dir(module.ffi): if not name.startswith('_'): attr = getattr(module.ffi, name) if attr is not getattr(ffi, name, object()): setattr(ffi, name, attr) def typeof_disabled(*args, **kwds): raise NotImplementedError ffi._typeof = typeof_disabled for name in dir(ffi): if not name.startswith('_') and not hasattr(module.ffi, name): setattr(ffi, name, NotImplemented) return module.lib backend_ctypes.py000064400000122146152344566040010113 0ustar00import ctypes, ctypes.util, operator, sys from . import model if sys.version_info < (3,): bytechr = chr else: unicode = str long = int xrange = range bytechr = lambda num: bytes([num]) class CTypesType(type): pass class CTypesData(object): __metaclass__ = CTypesType __slots__ = ['__weakref__'] __name__ = '' def __init__(self, *args): raise TypeError("cannot instantiate %r" % (self.__class__,)) @classmethod def _newp(cls, init): raise TypeError("expected a pointer or array ctype, got '%s'" % (cls._get_c_name(),)) @staticmethod def _to_ctypes(value): raise TypeError @classmethod def _arg_to_ctypes(cls, *value): try: ctype = cls._ctype except AttributeError: raise TypeError("cannot create an instance of %r" % (cls,)) if value: res = cls._to_ctypes(*value) if not isinstance(res, ctype): res = cls._ctype(res) else: res = cls._ctype() return res @classmethod def _create_ctype_obj(cls, init): if init is None: return cls._arg_to_ctypes() else: return cls._arg_to_ctypes(init) @staticmethod def _from_ctypes(ctypes_value): raise TypeError @classmethod def _get_c_name(cls, replace_with=''): return cls._reftypename.replace(' &', replace_with) @classmethod def _fix_class(cls): cls.__name__ = 'CData<%s>' % (cls._get_c_name(),) cls.__qualname__ = 'CData<%s>' % (cls._get_c_name(),) cls.__module__ = 'ffi' def _get_own_repr(self): raise NotImplementedError def _addr_repr(self, address): if address == 0: return 'NULL' else: if address < 0: address += 1 << (8*ctypes.sizeof(ctypes.c_void_p)) return '0x%x' % address def __repr__(self, c_name=None): own = self._get_own_repr() return '' % (c_name or self._get_c_name(), own) def _convert_to_address(self, BClass): if BClass is None: raise TypeError("cannot convert %r to an address" % ( self._get_c_name(),)) else: raise TypeError("cannot convert %r to %r" % ( self._get_c_name(), BClass._get_c_name())) @classmethod def _get_size(cls): return ctypes.sizeof(cls._ctype) def _get_size_of_instance(self): return ctypes.sizeof(self._ctype) @classmethod def _cast_from(cls, source): raise TypeError("cannot cast to %r" % (cls._get_c_name(),)) def _cast_to_integer(self): return self._convert_to_address(None) @classmethod def _alignment(cls): return ctypes.alignment(cls._ctype) def __iter__(self): raise TypeError("cdata %r does not support iteration" % ( self._get_c_name()),) def _make_cmp(name): cmpfunc = getattr(operator, name) def cmp(self, other): v_is_ptr = not isinstance(self, CTypesGenericPrimitive) w_is_ptr = (isinstance(other, CTypesData) and not isinstance(other, CTypesGenericPrimitive)) if v_is_ptr and w_is_ptr: return cmpfunc(self._convert_to_address(None), other._convert_to_address(None)) elif v_is_ptr or w_is_ptr: return NotImplemented else: if isinstance(self, CTypesGenericPrimitive): self = self._value if isinstance(other, CTypesGenericPrimitive): other = other._value return cmpfunc(self, other) cmp.func_name = name return cmp __eq__ = _make_cmp('__eq__') __ne__ = _make_cmp('__ne__') __lt__ = _make_cmp('__lt__') __le__ = _make_cmp('__le__') __gt__ = _make_cmp('__gt__') __ge__ = _make_cmp('__ge__') def __hash__(self): return hash(self._convert_to_address(None)) def _to_string(self, maxlen): raise TypeError("string(): %r" % (self,)) class CTypesGenericPrimitive(CTypesData): __slots__ = [] def __hash__(self): return hash(self._value) def _get_own_repr(self): return repr(self._from_ctypes(self._value)) class CTypesGenericArray(CTypesData): __slots__ = [] @classmethod def _newp(cls, init): return cls(init) def __iter__(self): for i in xrange(len(self)): yield self[i] def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob)) class CTypesGenericPtr(CTypesData): __slots__ = ['_address', '_as_ctype_ptr'] _automatic_casts = False kind = "pointer" @classmethod def _newp(cls, init): return cls(init) @classmethod def _cast_from(cls, source): if source is None: address = 0 elif isinstance(source, CTypesData): address = source._cast_to_integer() elif isinstance(source, (int, long)): address = source else: raise TypeError("bad type for cast to %r: %r" % (cls, type(source).__name__)) return cls._new_pointer_at(address) @classmethod def _new_pointer_at(cls, address): self = cls.__new__(cls) self._address = address self._as_ctype_ptr = ctypes.cast(address, cls._ctype) return self def _get_own_repr(self): try: return self._addr_repr(self._address) except AttributeError: return '???' def _cast_to_integer(self): return self._address def __nonzero__(self): return bool(self._address) __bool__ = __nonzero__ @classmethod def _to_ctypes(cls, value): if not isinstance(value, CTypesData): raise TypeError("unexpected %s object" % type(value).__name__) address = value._convert_to_address(cls) return ctypes.cast(address, cls._ctype) @classmethod def _from_ctypes(cls, ctypes_ptr): address = ctypes.cast(ctypes_ptr, ctypes.c_void_p).value or 0 return cls._new_pointer_at(address) @classmethod def _initialize(cls, ctypes_ptr, value): if value: ctypes_ptr.contents = cls._to_ctypes(value).contents def _convert_to_address(self, BClass): if (BClass in (self.__class__, None) or BClass._automatic_casts or self._automatic_casts): return self._address else: return CTypesData._convert_to_address(self, BClass) class CTypesBaseStructOrUnion(CTypesData): __slots__ = ['_blob'] @classmethod def _create_ctype_obj(cls, init): # may be overridden raise TypeError("cannot instantiate opaque type %s" % (cls,)) def _get_own_repr(self): return self._addr_repr(ctypes.addressof(self._blob)) @classmethod def _offsetof(cls, fieldname): return getattr(cls._ctype, fieldname).offset def _convert_to_address(self, BClass): if getattr(BClass, '_BItem', None) is self.__class__: return ctypes.addressof(self._blob) else: return CTypesData._convert_to_address(self, BClass) @classmethod def _from_ctypes(cls, ctypes_struct_or_union): self = cls.__new__(cls) self._blob = ctypes_struct_or_union return self @classmethod def _to_ctypes(cls, value): return value._blob def __repr__(self, c_name=None): return CTypesData.__repr__(self, c_name or self._get_c_name(' &')) class CTypesBackend(object): PRIMITIVE_TYPES = { 'char': ctypes.c_char, 'short': ctypes.c_short, 'int': ctypes.c_int, 'long': ctypes.c_long, 'long long': ctypes.c_longlong, 'signed char': ctypes.c_byte, 'unsigned char': ctypes.c_ubyte, 'unsigned short': ctypes.c_ushort, 'unsigned int': ctypes.c_uint, 'unsigned long': ctypes.c_ulong, 'unsigned long long': ctypes.c_ulonglong, 'float': ctypes.c_float, 'double': ctypes.c_double, '_Bool': ctypes.c_bool, } for _name in ['unsigned long long', 'unsigned long', 'unsigned int', 'unsigned short', 'unsigned char']: _size = ctypes.sizeof(PRIMITIVE_TYPES[_name]) PRIMITIVE_TYPES['uint%d_t' % (8*_size)] = PRIMITIVE_TYPES[_name] if _size == ctypes.sizeof(ctypes.c_void_p): PRIMITIVE_TYPES['uintptr_t'] = PRIMITIVE_TYPES[_name] if _size == ctypes.sizeof(ctypes.c_size_t): PRIMITIVE_TYPES['size_t'] = PRIMITIVE_TYPES[_name] for _name in ['long long', 'long', 'int', 'short', 'signed char']: _size = ctypes.sizeof(PRIMITIVE_TYPES[_name]) PRIMITIVE_TYPES['int%d_t' % (8*_size)] = PRIMITIVE_TYPES[_name] if _size == ctypes.sizeof(ctypes.c_void_p): PRIMITIVE_TYPES['intptr_t'] = PRIMITIVE_TYPES[_name] PRIMITIVE_TYPES['ptrdiff_t'] = PRIMITIVE_TYPES[_name] if _size == ctypes.sizeof(ctypes.c_size_t): PRIMITIVE_TYPES['ssize_t'] = PRIMITIVE_TYPES[_name] def __init__(self): self.RTLD_LAZY = 0 # not supported anyway by ctypes self.RTLD_NOW = 0 self.RTLD_GLOBAL = ctypes.RTLD_GLOBAL self.RTLD_LOCAL = ctypes.RTLD_LOCAL def set_ffi(self, ffi): self.ffi = ffi def _get_types(self): return CTypesData, CTypesType def load_library(self, path, flags=0): cdll = ctypes.CDLL(path, flags) return CTypesLibrary(self, cdll) def new_void_type(self): class CTypesVoid(CTypesData): __slots__ = [] _reftypename = 'void &' @staticmethod def _from_ctypes(novalue): return None @staticmethod def _to_ctypes(novalue): if novalue is not None: raise TypeError("None expected, got %s object" % (type(novalue).__name__,)) return None CTypesVoid._fix_class() return CTypesVoid def new_primitive_type(self, name): if name == 'wchar_t': raise NotImplementedError(name) ctype = self.PRIMITIVE_TYPES[name] if name == 'char': kind = 'char' elif name in ('float', 'double'): kind = 'float' else: if name in ('signed char', 'unsigned char'): kind = 'byte' elif name == '_Bool': kind = 'bool' else: kind = 'int' is_signed = (ctype(-1).value == -1) # def _cast_source_to_int(source): if isinstance(source, (int, long, float)): source = int(source) elif isinstance(source, CTypesData): source = source._cast_to_integer() elif isinstance(source, bytes): source = ord(source) elif source is None: source = 0 else: raise TypeError("bad type for cast to %r: %r" % (CTypesPrimitive, type(source).__name__)) return source # kind1 = kind class CTypesPrimitive(CTypesGenericPrimitive): __slots__ = ['_value'] _ctype = ctype _reftypename = '%s &' % name kind = kind1 def __init__(self, value): self._value = value @staticmethod def _create_ctype_obj(init): if init is None: return ctype() return ctype(CTypesPrimitive._to_ctypes(init)) if kind == 'int' or kind == 'byte': @classmethod def _cast_from(cls, source): source = _cast_source_to_int(source) source = ctype(source).value # cast within range return cls(source) def __int__(self): return self._value if kind == 'bool': @classmethod def _cast_from(cls, source): if not isinstance(source, (int, long, float)): source = _cast_source_to_int(source) return cls(bool(source)) def __int__(self): return self._value if kind == 'char': @classmethod def _cast_from(cls, source): source = _cast_source_to_int(source) source = bytechr(source & 0xFF) return cls(source) def __int__(self): return ord(self._value) if kind == 'float': @classmethod def _cast_from(cls, source): if isinstance(source, float): pass elif isinstance(source, CTypesGenericPrimitive): if hasattr(source, '__float__'): source = float(source) else: source = int(source) else: source = _cast_source_to_int(source) source = ctype(source).value # fix precision return cls(source) def __int__(self): return int(self._value) def __float__(self): return self._value _cast_to_integer = __int__ if kind == 'int' or kind == 'byte' or kind == 'bool': @staticmethod def _to_ctypes(x): if not isinstance(x, (int, long)): if isinstance(x, CTypesData): x = int(x) else: raise TypeError("integer expected, got %s" % type(x).__name__) if ctype(x).value != x: if not is_signed and x < 0: raise OverflowError("%s: negative integer" % name) else: raise OverflowError("%s: integer out of bounds" % name) return x if kind == 'char': @staticmethod def _to_ctypes(x): if isinstance(x, bytes) and len(x) == 1: return x if isinstance(x, CTypesPrimitive): # > return x._value raise TypeError("character expected, got %s" % type(x).__name__) def __nonzero__(self): return ord(self._value) != 0 else: def __nonzero__(self): return self._value != 0 __bool__ = __nonzero__ if kind == 'float': @staticmethod def _to_ctypes(x): if not isinstance(x, (int, long, float, CTypesData)): raise TypeError("float expected, got %s" % type(x).__name__) return ctype(x).value @staticmethod def _from_ctypes(value): return getattr(value, 'value', value) @staticmethod def _initialize(blob, init): blob.value = CTypesPrimitive._to_ctypes(init) if kind == 'char': def _to_string(self, maxlen): return self._value if kind == 'byte': def _to_string(self, maxlen): return chr(self._value & 0xff) # CTypesPrimitive._fix_class() return CTypesPrimitive def new_pointer_type(self, BItem): getbtype = self.ffi._get_cached_btype if BItem is getbtype(model.PrimitiveType('char')): kind = 'charp' elif BItem in (getbtype(model.PrimitiveType('signed char')), getbtype(model.PrimitiveType('unsigned char'))): kind = 'bytep' elif BItem is getbtype(model.void_type): kind = 'voidp' else: kind = 'generic' # class CTypesPtr(CTypesGenericPtr): __slots__ = ['_own'] if kind == 'charp': __slots__ += ['__as_strbuf'] _BItem = BItem if hasattr(BItem, '_ctype'): _ctype = ctypes.POINTER(BItem._ctype) _bitem_size = ctypes.sizeof(BItem._ctype) else: _ctype = ctypes.c_void_p if issubclass(BItem, CTypesGenericArray): _reftypename = BItem._get_c_name('(* &)') else: _reftypename = BItem._get_c_name(' * &') def __init__(self, init): ctypeobj = BItem._create_ctype_obj(init) if kind == 'charp': self.__as_strbuf = ctypes.create_string_buffer( ctypeobj.value + b'\x00') self._as_ctype_ptr = ctypes.cast( self.__as_strbuf, self._ctype) else: self._as_ctype_ptr = ctypes.pointer(ctypeobj) self._address = ctypes.cast(self._as_ctype_ptr, ctypes.c_void_p).value self._own = True def __add__(self, other): if isinstance(other, (int, long)): return self._new_pointer_at(self._address + other * self._bitem_size) else: return NotImplemented def __sub__(self, other): if isinstance(other, (int, long)): return self._new_pointer_at(self._address - other * self._bitem_size) elif type(self) is type(other): return (self._address - other._address) // self._bitem_size else: return NotImplemented def __getitem__(self, index): if getattr(self, '_own', False) and index != 0: raise IndexError return BItem._from_ctypes(self._as_ctype_ptr[index]) def __setitem__(self, index, value): self._as_ctype_ptr[index] = BItem._to_ctypes(value) if kind == 'charp' or kind == 'voidp': @classmethod def _arg_to_ctypes(cls, *value): if value and isinstance(value[0], bytes): return ctypes.c_char_p(value[0]) else: return super(CTypesPtr, cls)._arg_to_ctypes(*value) if kind == 'charp' or kind == 'bytep': def _to_string(self, maxlen): if maxlen < 0: maxlen = sys.maxsize p = ctypes.cast(self._as_ctype_ptr, ctypes.POINTER(ctypes.c_char)) n = 0 while n < maxlen and p[n] != b'\x00': n += 1 return b''.join([p[i] for i in range(n)]) def _get_own_repr(self): if getattr(self, '_own', False): return 'owning %d bytes' % ( ctypes.sizeof(self._as_ctype_ptr.contents),) return super(CTypesPtr, self)._get_own_repr() # if (BItem is self.ffi._get_cached_btype(model.void_type) or BItem is self.ffi._get_cached_btype(model.PrimitiveType('char'))): CTypesPtr._automatic_casts = True # CTypesPtr._fix_class() return CTypesPtr def new_array_type(self, CTypesPtr, length): if length is None: brackets = ' &[]' else: brackets = ' &[%d]' % length BItem = CTypesPtr._BItem getbtype = self.ffi._get_cached_btype if BItem is getbtype(model.PrimitiveType('char')): kind = 'char' elif BItem in (getbtype(model.PrimitiveType('signed char')), getbtype(model.PrimitiveType('unsigned char'))): kind = 'byte' else: kind = 'generic' # class CTypesArray(CTypesGenericArray): __slots__ = ['_blob', '_own'] if length is not None: _ctype = BItem._ctype * length else: __slots__.append('_ctype') _reftypename = BItem._get_c_name(brackets) _declared_length = length _CTPtr = CTypesPtr def __init__(self, init): if length is None: if isinstance(init, (int, long)): len1 = init init = None elif kind == 'char' and isinstance(init, bytes): len1 = len(init) + 1 # extra null else: init = tuple(init) len1 = len(init) self._ctype = BItem._ctype * len1 self._blob = self._ctype() self._own = True if init is not None: self._initialize(self._blob, init) @staticmethod def _initialize(blob, init): if isinstance(init, bytes): init = [init[i:i+1] for i in range(len(init))] else: init = tuple(init) if len(init) > len(blob): raise IndexError("too many initializers") addr = ctypes.cast(blob, ctypes.c_void_p).value PTR = ctypes.POINTER(BItem._ctype) itemsize = ctypes.sizeof(BItem._ctype) for i, value in enumerate(init): p = ctypes.cast(addr + i * itemsize, PTR) BItem._initialize(p.contents, value) def __len__(self): return len(self._blob) def __getitem__(self, index): if not (0 <= index < len(self._blob)): raise IndexError return BItem._from_ctypes(self._blob[index]) def __setitem__(self, index, value): if not (0 <= index < len(self._blob)): raise IndexError self._blob[index] = BItem._to_ctypes(value) if kind == 'char' or kind == 'byte': def _to_string(self, maxlen): if maxlen < 0: maxlen = len(self._blob) p = ctypes.cast(self._blob, ctypes.POINTER(ctypes.c_char)) n = 0 while n < maxlen and p[n] != b'\x00': n += 1 return b''.join([p[i] for i in range(n)]) def _get_own_repr(self): if getattr(self, '_own', False): return 'owning %d bytes' % (ctypes.sizeof(self._blob),) return super(CTypesArray, self)._get_own_repr() def _convert_to_address(self, BClass): if BClass in (CTypesPtr, None) or BClass._automatic_casts: return ctypes.addressof(self._blob) else: return CTypesData._convert_to_address(self, BClass) @staticmethod def _from_ctypes(ctypes_array): self = CTypesArray.__new__(CTypesArray) self._blob = ctypes_array return self @staticmethod def _arg_to_ctypes(value): return CTypesPtr._arg_to_ctypes(value) def __add__(self, other): if isinstance(other, (int, long)): return CTypesPtr._new_pointer_at( ctypes.addressof(self._blob) + other * ctypes.sizeof(BItem._ctype)) else: return NotImplemented @classmethod def _cast_from(cls, source): raise NotImplementedError("casting to %r" % ( cls._get_c_name(),)) # CTypesArray._fix_class() return CTypesArray def _new_struct_or_union(self, kind, name, base_ctypes_class): # class struct_or_union(base_ctypes_class): pass struct_or_union.__name__ = '%s_%s' % (kind, name) kind1 = kind # class CTypesStructOrUnion(CTypesBaseStructOrUnion): __slots__ = ['_blob'] _ctype = struct_or_union _reftypename = '%s &' % (name,) _kind = kind = kind1 # CTypesStructOrUnion._fix_class() return CTypesStructOrUnion def new_struct_type(self, name): return self._new_struct_or_union('struct', name, ctypes.Structure) def new_union_type(self, name): return self._new_struct_or_union('union', name, ctypes.Union) def complete_struct_or_union(self, CTypesStructOrUnion, fields, tp, totalsize=-1, totalalignment=-1, sflags=0): if totalsize >= 0 or totalalignment >= 0: raise NotImplementedError("the ctypes backend of CFFI does not support " "structures completed by verify(); please " "compile and install the _cffi_backend module.") struct_or_union = CTypesStructOrUnion._ctype fnames = [fname for (fname, BField, bitsize) in fields] btypes = [BField for (fname, BField, bitsize) in fields] bitfields = [bitsize for (fname, BField, bitsize) in fields] # bfield_types = {} cfields = [] for (fname, BField, bitsize) in fields: if bitsize < 0: cfields.append((fname, BField._ctype)) bfield_types[fname] = BField else: cfields.append((fname, BField._ctype, bitsize)) bfield_types[fname] = Ellipsis if sflags & 8: struct_or_union._pack_ = 1 struct_or_union._fields_ = cfields CTypesStructOrUnion._bfield_types = bfield_types # @staticmethod def _create_ctype_obj(init): result = struct_or_union() if init is not None: initialize(result, init) return result CTypesStructOrUnion._create_ctype_obj = _create_ctype_obj # def initialize(blob, init): if is_union: if len(init) > 1: raise ValueError("union initializer: %d items given, but " "only one supported (use a dict if needed)" % (len(init),)) if not isinstance(init, dict): if isinstance(init, (bytes, unicode)): raise TypeError("union initializer: got a str") init = tuple(init) if len(init) > len(fnames): raise ValueError("too many values for %s initializer" % CTypesStructOrUnion._get_c_name()) init = dict(zip(fnames, init)) addr = ctypes.addressof(blob) for fname, value in init.items(): BField, bitsize = name2fieldtype[fname] assert bitsize < 0, \ "not implemented: initializer with bit fields" offset = CTypesStructOrUnion._offsetof(fname) PTR = ctypes.POINTER(BField._ctype) p = ctypes.cast(addr + offset, PTR) BField._initialize(p.contents, value) is_union = CTypesStructOrUnion._kind == 'union' name2fieldtype = dict(zip(fnames, zip(btypes, bitfields))) # for fname, BField, bitsize in fields: if fname == '': raise NotImplementedError("nested anonymous structs/unions") if hasattr(CTypesStructOrUnion, fname): raise ValueError("the field name %r conflicts in " "the ctypes backend" % fname) if bitsize < 0: def getter(self, fname=fname, BField=BField, offset=CTypesStructOrUnion._offsetof(fname), PTR=ctypes.POINTER(BField._ctype)): addr = ctypes.addressof(self._blob) p = ctypes.cast(addr + offset, PTR) return BField._from_ctypes(p.contents) def setter(self, value, fname=fname, BField=BField): setattr(self._blob, fname, BField._to_ctypes(value)) # if issubclass(BField, CTypesGenericArray): setter = None if BField._declared_length == 0: def getter(self, fname=fname, BFieldPtr=BField._CTPtr, offset=CTypesStructOrUnion._offsetof(fname), PTR=ctypes.POINTER(BField._ctype)): addr = ctypes.addressof(self._blob) p = ctypes.cast(addr + offset, PTR) return BFieldPtr._from_ctypes(p) # else: def getter(self, fname=fname, BField=BField): return BField._from_ctypes(getattr(self._blob, fname)) def setter(self, value, fname=fname, BField=BField): # xxx obscure workaround value = BField._to_ctypes(value) oldvalue = getattr(self._blob, fname) setattr(self._blob, fname, value) if value != getattr(self._blob, fname): setattr(self._blob, fname, oldvalue) raise OverflowError("value too large for bitfield") setattr(CTypesStructOrUnion, fname, property(getter, setter)) # CTypesPtr = self.ffi._get_cached_btype(model.PointerType(tp)) for fname in fnames: if hasattr(CTypesPtr, fname): raise ValueError("the field name %r conflicts in " "the ctypes backend" % fname) def getter(self, fname=fname): return getattr(self[0], fname) def setter(self, value, fname=fname): setattr(self[0], fname, value) setattr(CTypesPtr, fname, property(getter, setter)) def new_function_type(self, BArgs, BResult, has_varargs): nameargs = [BArg._get_c_name() for BArg in BArgs] if has_varargs: nameargs.append('...') nameargs = ', '.join(nameargs) # class CTypesFunctionPtr(CTypesGenericPtr): __slots__ = ['_own_callback', '_name'] _ctype = ctypes.CFUNCTYPE(getattr(BResult, '_ctype', None), *[BArg._ctype for BArg in BArgs], use_errno=True) _reftypename = BResult._get_c_name('(* &)(%s)' % (nameargs,)) def __init__(self, init, error=None): # create a callback to the Python callable init() import traceback assert not has_varargs, "varargs not supported for callbacks" if getattr(BResult, '_ctype', None) is not None: error = BResult._from_ctypes( BResult._create_ctype_obj(error)) else: error = None def callback(*args): args2 = [] for arg, BArg in zip(args, BArgs): args2.append(BArg._from_ctypes(arg)) try: res2 = init(*args2) res2 = BResult._to_ctypes(res2) except: traceback.print_exc() res2 = error if issubclass(BResult, CTypesGenericPtr): if res2: res2 = ctypes.cast(res2, ctypes.c_void_p).value # .value: http://bugs.python.org/issue1574593 else: res2 = None #print repr(res2) return res2 if issubclass(BResult, CTypesGenericPtr): # The only pointers callbacks can return are void*s: # http://bugs.python.org/issue5710 callback_ctype = ctypes.CFUNCTYPE( ctypes.c_void_p, *[BArg._ctype for BArg in BArgs], use_errno=True) else: callback_ctype = CTypesFunctionPtr._ctype self._as_ctype_ptr = callback_ctype(callback) self._address = ctypes.cast(self._as_ctype_ptr, ctypes.c_void_p).value self._own_callback = init @staticmethod def _initialize(ctypes_ptr, value): if value: raise NotImplementedError("ctypes backend: not supported: " "initializers for function pointers") def __repr__(self): c_name = getattr(self, '_name', None) if c_name: i = self._reftypename.index('(* &)') if self._reftypename[i-1] not in ' )*': c_name = ' ' + c_name c_name = self._reftypename.replace('(* &)', c_name) return CTypesData.__repr__(self, c_name) def _get_own_repr(self): if getattr(self, '_own_callback', None) is not None: return 'calling %r' % (self._own_callback,) return super(CTypesFunctionPtr, self)._get_own_repr() def __call__(self, *args): if has_varargs: assert len(args) >= len(BArgs) extraargs = args[len(BArgs):] args = args[:len(BArgs)] else: assert len(args) == len(BArgs) ctypes_args = [] for arg, BArg in zip(args, BArgs): ctypes_args.append(BArg._arg_to_ctypes(arg)) if has_varargs: for i, arg in enumerate(extraargs): if arg is None: ctypes_args.append(ctypes.c_void_p(0)) # NULL continue if not isinstance(arg, CTypesData): raise TypeError( "argument %d passed in the variadic part " "needs to be a cdata object (got %s)" % (1 + len(BArgs) + i, type(arg).__name__)) ctypes_args.append(arg._arg_to_ctypes(arg)) result = self._as_ctype_ptr(*ctypes_args) return BResult._from_ctypes(result) # CTypesFunctionPtr._fix_class() return CTypesFunctionPtr def new_enum_type(self, name, enumerators, enumvalues, CTypesInt): assert isinstance(name, str) reverse_mapping = dict(zip(reversed(enumvalues), reversed(enumerators))) # class CTypesEnum(CTypesInt): __slots__ = [] _reftypename = '%s &' % name def _get_own_repr(self): value = self._value try: return '%d: %s' % (value, reverse_mapping[value]) except KeyError: return str(value) def _to_string(self, maxlen): value = self._value try: return reverse_mapping[value] except KeyError: return str(value) # CTypesEnum._fix_class() return CTypesEnum def get_errno(self): return ctypes.get_errno() def set_errno(self, value): ctypes.set_errno(value) def string(self, b, maxlen=-1): return b._to_string(maxlen) def buffer(self, bptr, size=-1): raise NotImplementedError("buffer() with ctypes backend") def sizeof(self, cdata_or_BType): if isinstance(cdata_or_BType, CTypesData): return cdata_or_BType._get_size_of_instance() else: assert issubclass(cdata_or_BType, CTypesData) return cdata_or_BType._get_size() def alignof(self, BType): assert issubclass(BType, CTypesData) return BType._alignment() def newp(self, BType, source): if not issubclass(BType, CTypesData): raise TypeError return BType._newp(source) def cast(self, BType, source): return BType._cast_from(source) def callback(self, BType, source, error, onerror): assert onerror is None # XXX not implemented return BType(source, error) _weakref_cache_ref = None def gcp(self, cdata, destructor, size=0): if self._weakref_cache_ref is None: import weakref class MyRef(weakref.ref): def __eq__(self, other): myref = self() return self is other or ( myref is not None and myref is other()) def __ne__(self, other): return not (self == other) def __hash__(self): try: return self._hash except AttributeError: self._hash = hash(self()) return self._hash self._weakref_cache_ref = {}, MyRef weak_cache, MyRef = self._weakref_cache_ref if destructor is None: try: del weak_cache[MyRef(cdata)] except KeyError: raise TypeError("Can remove destructor only on a object " "previously returned by ffi.gc()") return None def remove(k): cdata, destructor = weak_cache.pop(k, (None, None)) if destructor is not None: destructor(cdata) new_cdata = self.cast(self.typeof(cdata), cdata) assert new_cdata is not cdata weak_cache[MyRef(new_cdata, remove)] = (cdata, destructor) return new_cdata typeof = type def getcname(self, BType, replace_with): return BType._get_c_name(replace_with) def typeoffsetof(self, BType, fieldname, num=0): if isinstance(fieldname, str): if num == 0 and issubclass(BType, CTypesGenericPtr): BType = BType._BItem if not issubclass(BType, CTypesBaseStructOrUnion): raise TypeError("expected a struct or union ctype") BField = BType._bfield_types[fieldname] if BField is Ellipsis: raise TypeError("not supported for bitfields") return (BField, BType._offsetof(fieldname)) elif isinstance(fieldname, (int, long)): if issubclass(BType, CTypesGenericArray): BType = BType._CTPtr if not issubclass(BType, CTypesGenericPtr): raise TypeError("expected an array or ptr ctype") BItem = BType._BItem offset = BItem._get_size() * fieldname if offset > sys.maxsize: raise OverflowError return (BItem, offset) else: raise TypeError(type(fieldname)) def rawaddressof(self, BTypePtr, cdata, offset=None): if isinstance(cdata, CTypesBaseStructOrUnion): ptr = ctypes.pointer(type(cdata)._to_ctypes(cdata)) elif isinstance(cdata, CTypesGenericPtr): if offset is None or not issubclass(type(cdata)._BItem, CTypesBaseStructOrUnion): raise TypeError("unexpected cdata type") ptr = type(cdata)._to_ctypes(cdata) elif isinstance(cdata, CTypesGenericArray): ptr = type(cdata)._to_ctypes(cdata) else: raise TypeError("expected a ") if offset: ptr = ctypes.cast( ctypes.c_void_p( ctypes.cast(ptr, ctypes.c_void_p).value + offset), type(ptr)) return BTypePtr._from_ctypes(ptr) class CTypesLibrary(object): def __init__(self, backend, cdll): self.backend = backend self.cdll = cdll def load_function(self, BType, name): c_func = getattr(self.cdll, name) funcobj = BType._from_ctypes(c_func) funcobj._name = name return funcobj def read_variable(self, BType, name): try: ctypes_obj = BType._ctype.in_dll(self.cdll, name) except AttributeError as e: raise NotImplementedError(e) return BType._from_ctypes(ctypes_obj) def write_variable(self, BType, name, value): new_ctypes_obj = BType._to_ctypes(value) ctypes_obj = BType._ctype.in_dll(self.cdll, name) ctypes.memmove(ctypes.addressof(ctypes_obj), ctypes.addressof(new_ctypes_obj), ctypes.sizeof(BType._ctype)) verifier.py000064400000025707152344566040006755 0ustar00# # DEPRECATED: implementation for ffi.verify() # import sys, os, binascii, shutil, io from . import __version_verifier_modules__ from . import ffiplatform from .error import VerificationError if sys.version_info >= (3, 3): import importlib.machinery def _extension_suffixes(): return importlib.machinery.EXTENSION_SUFFIXES[:] else: import imp def _extension_suffixes(): return [suffix for suffix, _, type in imp.get_suffixes() if type == imp.C_EXTENSION] if sys.version_info >= (3,): NativeIO = io.StringIO else: class NativeIO(io.BytesIO): def write(self, s): if isinstance(s, unicode): s = s.encode('ascii') super(NativeIO, self).write(s) class Verifier(object): def __init__(self, ffi, preamble, tmpdir=None, modulename=None, ext_package=None, tag='', force_generic_engine=False, source_extension='.c', flags=None, relative_to=None, **kwds): if ffi._parser._uses_new_feature: raise VerificationError( "feature not supported with ffi.verify(), but only " "with ffi.set_source(): %s" % (ffi._parser._uses_new_feature,)) self.ffi = ffi self.preamble = preamble if not modulename: flattened_kwds = ffiplatform.flatten(kwds) vengine_class = _locate_engine_class(ffi, force_generic_engine) self._vengine = vengine_class(self) self._vengine.patch_extension_kwds(kwds) self.flags = flags self.kwds = self.make_relative_to(kwds, relative_to) # if modulename: if tag: raise TypeError("can't specify both 'modulename' and 'tag'") else: key = '\x00'.join([sys.version[:3], __version_verifier_modules__, preamble, flattened_kwds] + ffi._cdefsources) if sys.version_info >= (3,): key = key.encode('utf-8') k1 = hex(binascii.crc32(key[0::2]) & 0xffffffff) k1 = k1.lstrip('0x').rstrip('L') k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff) k2 = k2.lstrip('0').rstrip('L') modulename = '_cffi_%s_%s%s%s' % (tag, self._vengine._class_key, k1, k2) suffix = _get_so_suffixes()[0] self.tmpdir = tmpdir or _caller_dir_pycache() self.sourcefilename = os.path.join(self.tmpdir, modulename + source_extension) self.modulefilename = os.path.join(self.tmpdir, modulename + suffix) self.ext_package = ext_package self._has_source = False self._has_module = False def write_source(self, file=None): """Write the C source code. It is produced in 'self.sourcefilename', which can be tweaked beforehand.""" with self.ffi._lock: if self._has_source and file is None: raise VerificationError( "source code already written") self._write_source(file) def compile_module(self): """Write the C source code (if not done already) and compile it. This produces a dynamic link library in 'self.modulefilename'.""" with self.ffi._lock: if self._has_module: raise VerificationError("module already compiled") if not self._has_source: self._write_source() self._compile_module() def load_library(self): """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. """ with self.ffi._lock: if not self._has_module: self._locate_module() if not self._has_module: if not self._has_source: self._write_source() self._compile_module() return self._load_library() def get_module_name(self): basename = os.path.basename(self.modulefilename) # kill both the .so extension and the other .'s, as introduced # by Python 3: 'basename.cpython-33m.so' basename = basename.split('.', 1)[0] # and the _d added in Python 2 debug builds --- but try to be # conservative and not kill a legitimate _d if basename.endswith('_d') and hasattr(sys, 'gettotalrefcount'): basename = basename[:-2] return basename def get_extension(self): ffiplatform._hack_at_distutils() # backward compatibility hack if not self._has_source: with self.ffi._lock: if not self._has_source: self._write_source() sourcename = ffiplatform.maybe_relative_path(self.sourcefilename) modname = self.get_module_name() return ffiplatform.get_extension(sourcename, modname, **self.kwds) def generates_python_module(self): return self._vengine._gen_python_module def make_relative_to(self, kwds, relative_to): if relative_to and os.path.dirname(relative_to): dirname = os.path.dirname(relative_to) kwds = kwds.copy() for key in ffiplatform.LIST_OF_FILE_NAMES: if key in kwds: lst = kwds[key] if not isinstance(lst, (list, tuple)): raise TypeError("keyword '%s' should be a list or tuple" % (key,)) lst = [os.path.join(dirname, fn) for fn in lst] kwds[key] = lst return kwds # ---------- def _locate_module(self): if not os.path.isfile(self.modulefilename): if self.ext_package: try: pkg = __import__(self.ext_package, None, None, ['__doc__']) except ImportError: return # cannot import the package itself, give up # (e.g. it might be called differently before installation) path = pkg.__path__ else: path = None filename = self._vengine.find_module(self.get_module_name(), path, _get_so_suffixes()) if filename is None: return self.modulefilename = filename self._vengine.collect_types() self._has_module = True def _write_source_to(self, file): self._vengine._f = file try: self._vengine.write_source_to_f() finally: del self._vengine._f def _write_source(self, file=None): if file is not None: self._write_source_to(file) else: # Write our source file to an in memory file. f = NativeIO() self._write_source_to(f) source_data = f.getvalue() # Determine if this matches the current file if os.path.exists(self.sourcefilename): with open(self.sourcefilename, "r") as fp: needs_written = not (fp.read() == source_data) else: needs_written = True # Actually write the file out if it doesn't match if needs_written: _ensure_dir(self.sourcefilename) with open(self.sourcefilename, "w") as fp: fp.write(source_data) # Set this flag self._has_source = True def _compile_module(self): # compile this C source tmpdir = os.path.dirname(self.sourcefilename) outputfilename = ffiplatform.compile(tmpdir, self.get_extension()) try: same = ffiplatform.samefile(outputfilename, self.modulefilename) except OSError: same = False if not same: _ensure_dir(self.modulefilename) shutil.move(outputfilename, self.modulefilename) self._has_module = True def _load_library(self): assert self._has_module if self.flags is not None: return self._vengine.load_library(self.flags) else: return self._vengine.load_library() # ____________________________________________________________ _FORCE_GENERIC_ENGINE = False # for tests def _locate_engine_class(ffi, force_generic_engine): if _FORCE_GENERIC_ENGINE: force_generic_engine = True if not force_generic_engine: if '__pypy__' in sys.builtin_module_names: force_generic_engine = True else: try: import _cffi_backend except ImportError: _cffi_backend = '?' if ffi._backend is not _cffi_backend: force_generic_engine = True if force_generic_engine: from . import vengine_gen return vengine_gen.VGenericEngine else: from . import vengine_cpy return vengine_cpy.VCPythonEngine # ____________________________________________________________ _TMPDIR = None def _caller_dir_pycache(): if _TMPDIR: return _TMPDIR result = os.environ.get('CFFI_TMPDIR') if result: return result filename = sys._getframe(2).f_code.co_filename return os.path.abspath(os.path.join(os.path.dirname(filename), '__pycache__')) def set_tmpdir(dirname): """Set the temporary directory to use instead of __pycache__.""" global _TMPDIR _TMPDIR = dirname def cleanup_tmpdir(tmpdir=None, keep_so=False): """Clean up the temporary directory by removing all files in it called `_cffi_*.{c,so}` as well as the `build` subdirectory.""" tmpdir = tmpdir or _caller_dir_pycache() try: filelist = os.listdir(tmpdir) except OSError: return if keep_so: suffix = '.c' # only remove .c files else: suffix = _get_so_suffixes()[0].lower() for fn in filelist: if fn.lower().startswith('_cffi_') and ( fn.lower().endswith(suffix) or fn.lower().endswith('.c')): try: os.unlink(os.path.join(tmpdir, fn)) except OSError: pass clean_dir = [os.path.join(tmpdir, 'build')] for dir in clean_dir: try: for fn in os.listdir(dir): fn = os.path.join(dir, fn) if os.path.isdir(fn): clean_dir.append(fn) else: os.unlink(fn) except OSError: pass def _get_so_suffixes(): suffixes = _extension_suffixes() if not suffixes: # bah, no C_EXTENSION available. Occurs on pypy without cpyext if sys.platform == 'win32': suffixes = [".pyd"] else: suffixes = [".so"] return suffixes def _ensure_dir(filename): dirname = os.path.dirname(filename) if dirname and not os.path.isdir(dirname): os.makedirs(dirname) error.py000064400000001232152344566040006256 0ustar00 class FFIError(Exception): pass class CDefError(Exception): def __str__(self): try: current_decl = self.args[1] filename = current_decl.coord.file linenum = current_decl.coord.line prefix = '%s:%d: ' % (filename, linenum) except (AttributeError, TypeError, IndexError): prefix = '' return '%s%s' % (prefix, self.args[0]) class VerificationError(Exception): """ An error raised when verification fails """ class VerificationMissing(Exception): """ An error raised when incomplete structures are passed into cdef, but no verification has been done """ _cffi_errors.h000064400000007334152344566040007377 0ustar00#ifndef CFFI_MESSAGEBOX # ifdef _MSC_VER # define CFFI_MESSAGEBOX 1 # else # define CFFI_MESSAGEBOX 0 # endif #endif #if CFFI_MESSAGEBOX /* Windows only: logic to take the Python-CFFI embedding logic initialization errors and display them in a background thread with MessageBox. The idea is that if the whole program closes as a result of this problem, then likely it is already a console program and you can read the stderr output in the console too. If it is not a console program, then it will likely show its own dialog to complain, or generally not abruptly close, and for this case the background thread should stay alive. */ static void *volatile _cffi_bootstrap_text; static PyObject *_cffi_start_error_capture(void) { PyObject *result = NULL; PyObject *x, *m, *bi; if (InterlockedCompareExchangePointer(&_cffi_bootstrap_text, (void *)1, NULL) != NULL) return (PyObject *)1; m = PyImport_AddModule("_cffi_error_capture"); if (m == NULL) goto error; result = PyModule_GetDict(m); if (result == NULL) goto error; #if PY_MAJOR_VERSION >= 3 bi = PyImport_ImportModule("builtins"); #else bi = PyImport_ImportModule("__builtin__"); #endif if (bi == NULL) goto error; PyDict_SetItemString(result, "__builtins__", bi); Py_DECREF(bi); x = PyRun_String( "import sys\n" "class FileLike:\n" " def write(self, x):\n" " of.write(x)\n" " self.buf += x\n" "fl = FileLike()\n" "fl.buf = ''\n" "of = sys.stderr\n" "sys.stderr = fl\n" "def done():\n" " sys.stderr = of\n" " return fl.buf\n", /* make sure the returned value stays alive */ Py_file_input, result, result); Py_XDECREF(x); error: if (PyErr_Occurred()) { PyErr_WriteUnraisable(Py_None); PyErr_Clear(); } return result; } #pragma comment(lib, "user32.lib") static DWORD WINAPI _cffi_bootstrap_dialog(LPVOID ignored) { Sleep(666); /* may be interrupted if the whole process is closing */ #if PY_MAJOR_VERSION >= 3 MessageBoxW(NULL, (wchar_t *)_cffi_bootstrap_text, L"Python-CFFI error", MB_OK | MB_ICONERROR); #else MessageBoxA(NULL, (char *)_cffi_bootstrap_text, "Python-CFFI error", MB_OK | MB_ICONERROR); #endif _cffi_bootstrap_text = NULL; return 0; } static void _cffi_stop_error_capture(PyObject *ecap) { PyObject *s; void *text; if (ecap == (PyObject *)1) return; if (ecap == NULL) goto error; s = PyRun_String("done()", Py_eval_input, ecap, ecap); if (s == NULL) goto error; /* Show a dialog box, but in a background thread, and never show multiple dialog boxes at once. */ #if PY_MAJOR_VERSION >= 3 text = PyUnicode_AsWideCharString(s, NULL); #else text = PyString_AsString(s); #endif _cffi_bootstrap_text = text; if (text != NULL) { HANDLE h; h = CreateThread(NULL, 0, _cffi_bootstrap_dialog, NULL, 0, NULL); if (h != NULL) CloseHandle(h); } /* decref the string, but it should stay alive as 'fl.buf' in the small module above. It will really be freed only if we later get another similar error. So it's a leak of at most one copy of the small module. That's fine for this situation which is usually a "fatal error" anyway. */ Py_DECREF(s); PyErr_Clear(); return; error: _cffi_bootstrap_text = NULL; PyErr_Clear(); } #else static PyObject *_cffi_start_error_capture(void) { return NULL; } static void _cffi_stop_error_capture(PyObject *ecap) { } #endif commontypes.py000064400000005201152344566040007502 0ustar00import sys from . import model from .error import FFIError COMMON_TYPES = {} try: # fetch "bool" and all simple Windows types from _cffi_backend import _get_common_types _get_common_types(COMMON_TYPES) except ImportError: pass COMMON_TYPES['FILE'] = model.unknown_type('FILE', '_IO_FILE') COMMON_TYPES['bool'] = '_Bool' # in case we got ImportError above for _type in model.PrimitiveType.ALL_PRIMITIVE_TYPES: if _type.endswith('_t'): COMMON_TYPES[_type] = _type del _type _CACHE = {} def resolve_common_type(parser, commontype): try: return _CACHE[commontype] except KeyError: cdecl = COMMON_TYPES.get(commontype, commontype) if not isinstance(cdecl, str): result, quals = cdecl, 0 # cdecl is already a BaseType elif cdecl in model.PrimitiveType.ALL_PRIMITIVE_TYPES: result, quals = model.PrimitiveType(cdecl), 0 elif cdecl == 'set-unicode-needed': raise FFIError("The Windows type %r is only available after " "you call ffi.set_unicode()" % (commontype,)) else: if commontype == cdecl: raise FFIError( "Unsupported type: %r. Please look at " "http://cffi.readthedocs.io/en/latest/cdef.html#ffi-cdef-limitations " "and file an issue if you think this type should really " "be supported." % (commontype,)) result, quals = parser.parse_type_and_quals(cdecl) # recursive assert isinstance(result, model.BaseTypeByIdentity) _CACHE[commontype] = result, quals return result, quals # ____________________________________________________________ # extra types for Windows (most of them are in commontypes.c) def win_common_types(): return { "UNICODE_STRING": model.StructType( "_UNICODE_STRING", ["Length", "MaximumLength", "Buffer"], [model.PrimitiveType("unsigned short"), model.PrimitiveType("unsigned short"), model.PointerType(model.PrimitiveType("wchar_t"))], [-1, -1, -1]), "PUNICODE_STRING": "UNICODE_STRING *", "PCUNICODE_STRING": "const UNICODE_STRING *", "TBYTE": "set-unicode-needed", "TCHAR": "set-unicode-needed", "LPCTSTR": "set-unicode-needed", "PCTSTR": "set-unicode-needed", "LPTSTR": "set-unicode-needed", "PTSTR": "set-unicode-needed", "PTBYTE": "set-unicode-needed", "PTCHAR": "set-unicode-needed", } if sys.platform == 'win32': COMMON_TYPES.update(win_common_types()) api.py000064400000117341152344566040005707 0ustar00import sys, types from .lock import allocate_lock from .error import CDefError from . import model try: callable except NameError: # Python 3.1 from collections import Callable callable = lambda x: isinstance(x, Callable) try: basestring except NameError: # Python 3.x basestring = str class FFI(object): r''' The main top-level class that you instantiate once, or once per module. Example usage: ffi = FFI() ffi.cdef(""" int printf(const char *, ...); """) C = ffi.dlopen(None) # standard library -or- C = ffi.verify() # use a C compiler: verify the decl above is right C.printf("hello, %s!\n", ffi.new("char[]", "world")) ''' def __init__(self, backend=None): """Create an FFI instance. The 'backend' argument is used to select a non-default backend, mostly for tests. """ if backend is None: # You need PyPy (>= 2.0 beta), or a CPython (>= 2.6) with # _cffi_backend.so compiled. import _cffi_backend as backend from . import __version__ if backend.__version__ != __version__: # bad version! Try to be as explicit as possible. if hasattr(backend, '__file__'): # CPython raise Exception("Version mismatch: this is the 'cffi' package version %s, located in %r. When we import the top-level '_cffi_backend' extension module, we get version %s, located in %r. The two versions should be equal; check your installation." % ( __version__, __file__, backend.__version__, backend.__file__)) else: # PyPy raise Exception("Version mismatch: this is the 'cffi' package version %s, located in %r. This interpreter comes with a built-in '_cffi_backend' module, which is version %s. The two versions should be equal; check your installation." % ( __version__, __file__, backend.__version__)) # (If you insist you can also try to pass the option # 'backend=backend_ctypes.CTypesBackend()', but don't # rely on it! It's probably not going to work well.) from . import cparser self._backend = backend self._lock = allocate_lock() self._parser = cparser.Parser() self._cached_btypes = {} self._parsed_types = types.ModuleType('parsed_types').__dict__ self._new_types = types.ModuleType('new_types').__dict__ self._function_caches = [] self._libraries = [] self._cdefsources = [] self._included_ffis = [] self._windows_unicode = None self._init_once_cache = {} self._cdef_version = None self._embedding = None self._typecache = model.get_typecache(backend) if hasattr(backend, 'set_ffi'): backend.set_ffi(self) for name in list(backend.__dict__): if name.startswith('RTLD_'): setattr(self, name, getattr(backend, name)) # with self._lock: self.BVoidP = self._get_cached_btype(model.voidp_type) self.BCharA = self._get_cached_btype(model.char_array_type) if isinstance(backend, types.ModuleType): # _cffi_backend: attach these constants to the class if not hasattr(FFI, 'NULL'): FFI.NULL = self.cast(self.BVoidP, 0) FFI.CData, FFI.CType = backend._get_types() else: # ctypes backend: attach these constants to the instance self.NULL = self.cast(self.BVoidP, 0) self.CData, self.CType = backend._get_types() self.buffer = backend.buffer def cdef(self, csource, override=False, packed=False): """Parse the given C source. This registers all declared functions, types, and global variables. The functions and global variables can then be accessed via either 'ffi.dlopen()' or 'ffi.verify()'. The types can be used in 'ffi.new()' and other functions. If 'packed' is specified as True, all structs declared inside this cdef are packed, i.e. laid out without any field alignment at all. """ self._cdef(csource, override=override, packed=packed) def embedding_api(self, csource, packed=False): self._cdef(csource, packed=packed, dllexport=True) if self._embedding is None: self._embedding = '' def _cdef(self, csource, override=False, **options): if not isinstance(csource, str): # unicode, on Python 2 if not isinstance(csource, basestring): raise TypeError("cdef() argument must be a string") csource = csource.encode('ascii') with self._lock: self._cdef_version = object() self._parser.parse(csource, override=override, **options) self._cdefsources.append(csource) if override: for cache in self._function_caches: cache.clear() finishlist = self._parser._recomplete if finishlist: self._parser._recomplete = [] for tp in finishlist: tp.finish_backend_type(self, finishlist) def dlopen(self, name, flags=0): """Load and return a dynamic library identified by 'name'. The standard C library can be loaded by passing None. Note that functions and types declared by 'ffi.cdef()' are not linked to a particular library, just like C headers; in the library we only look for the actual (untyped) symbols. """ assert isinstance(name, basestring) or name is None with self._lock: lib, function_cache = _make_ffi_library(self, name, flags) self._function_caches.append(function_cache) self._libraries.append(lib) return lib def dlclose(self, lib): """Close a library obtained with ffi.dlopen(). After this call, access to functions or variables from the library will fail (possibly with a segmentation fault). """ type(lib).__cffi_close__(lib) def _typeof_locked(self, cdecl): # call me with the lock! key = cdecl if key in self._parsed_types: return self._parsed_types[key] # if not isinstance(cdecl, str): # unicode, on Python 2 cdecl = cdecl.encode('ascii') # type = self._parser.parse_type(cdecl) really_a_function_type = type.is_raw_function if really_a_function_type: type = type.as_function_pointer() btype = self._get_cached_btype(type) result = btype, really_a_function_type self._parsed_types[key] = result return result def _typeof(self, cdecl, consider_function_as_funcptr=False): # string -> ctype object try: result = self._parsed_types[cdecl] except KeyError: with self._lock: result = self._typeof_locked(cdecl) # btype, really_a_function_type = result if really_a_function_type and not consider_function_as_funcptr: raise CDefError("the type %r is a function type, not a " "pointer-to-function type" % (cdecl,)) return btype def typeof(self, cdecl): """Parse the C type given as a string and return the corresponding object. It can also be used on 'cdata' instance to get its C type. """ if isinstance(cdecl, basestring): return self._typeof(cdecl) if isinstance(cdecl, self.CData): return self._backend.typeof(cdecl) if isinstance(cdecl, types.BuiltinFunctionType): res = _builtin_function_type(cdecl) if res is not None: return res if (isinstance(cdecl, types.FunctionType) and hasattr(cdecl, '_cffi_base_type')): with self._lock: return self._get_cached_btype(cdecl._cffi_base_type) raise TypeError(type(cdecl)) def sizeof(self, cdecl): """Return the size in bytes of the argument. It can be a string naming a C type, or a 'cdata' instance. """ if isinstance(cdecl, basestring): BType = self._typeof(cdecl) return self._backend.sizeof(BType) else: return self._backend.sizeof(cdecl) def alignof(self, cdecl): """Return the natural alignment size in bytes of the C type given as a string. """ if isinstance(cdecl, basestring): cdecl = self._typeof(cdecl) return self._backend.alignof(cdecl) def offsetof(self, cdecl, *fields_or_indexes): """Return the offset of the named field inside the given structure or array, which must be given as a C type name. You can give several field names in case of nested structures. You can also give numeric values which correspond to array items, in case of an array type. """ if isinstance(cdecl, basestring): cdecl = self._typeof(cdecl) return self._typeoffsetof(cdecl, *fields_or_indexes)[1] def new(self, cdecl, init=None): """Allocate an instance according to the specified C type and return a pointer to it. The specified C type must be either a pointer or an array: ``new('X *')`` allocates an X and returns a pointer to it, whereas ``new('X[n]')`` allocates an array of n X'es and returns an array referencing it (which works mostly like a pointer, like in C). You can also use ``new('X[]', n)`` to allocate an array of a non-constant length n. The memory is initialized following the rules of declaring a global variable in C: by default it is zero-initialized, but an explicit initializer can be given which can be used to fill all or part of the memory. When the returned object goes out of scope, the memory is freed. In other words the returned object has ownership of the value of type 'cdecl' that it points to. This means that the raw data can be used as long as this object is kept alive, but must not be used for a longer time. Be careful about that when copying the pointer to the memory somewhere else, e.g. into another structure. """ if isinstance(cdecl, basestring): cdecl = self._typeof(cdecl) return self._backend.newp(cdecl, init) def new_allocator(self, alloc=None, free=None, should_clear_after_alloc=True): """Return a new allocator, i.e. a function that behaves like ffi.new() but uses the provided low-level 'alloc' and 'free' functions. 'alloc' is called with the size as argument. If it returns NULL, a MemoryError is raised. 'free' is called with the result of 'alloc' as argument. Both can be either Python function or directly C functions. If 'free' is None, then no free function is called. If both 'alloc' and 'free' are None, the default is used. If 'should_clear_after_alloc' is set to False, then the memory returned by 'alloc' is assumed to be already cleared (or you are fine with garbage); otherwise CFFI will clear it. """ compiled_ffi = self._backend.FFI() allocator = compiled_ffi.new_allocator(alloc, free, should_clear_after_alloc) def allocate(cdecl, init=None): if isinstance(cdecl, basestring): cdecl = self._typeof(cdecl) return allocator(cdecl, init) return allocate def cast(self, cdecl, source): """Similar to a C cast: returns an instance of the named C type initialized with the given 'source'. The source is casted between integers or pointers of any type. """ if isinstance(cdecl, basestring): cdecl = self._typeof(cdecl) return self._backend.cast(cdecl, source) def string(self, cdata, maxlen=-1): """Return a Python string (or unicode string) from the 'cdata'. If 'cdata' is a pointer or array of characters or bytes, returns the null-terminated string. The returned string extends until the first null character, or at most 'maxlen' characters. If 'cdata' is an array then 'maxlen' defaults to its length. If 'cdata' is a pointer or array of wchar_t, returns a unicode string following the same rules. If 'cdata' is a single character or byte or a wchar_t, returns it as a string or unicode string. If 'cdata' is an enum, returns the value of the enumerator as a string, or 'NUMBER' if the value is out of range. """ return self._backend.string(cdata, maxlen) def unpack(self, cdata, length): """Unpack an array of C data of the given length, returning a Python string/unicode/list. If 'cdata' is a pointer to 'char', returns a byte string. It does not stop at the first null. This is equivalent to: ffi.buffer(cdata, length)[:] If 'cdata' is a pointer to 'wchar_t', returns a unicode string. 'length' is measured in wchar_t's; it is not the size in bytes. If 'cdata' is a pointer to anything else, returns a list of 'length' items. This is a faster equivalent to: [cdata[i] for i in range(length)] """ return self._backend.unpack(cdata, length) #def buffer(self, cdata, size=-1): # """Return a read-write buffer object that references the raw C data # pointed to by the given 'cdata'. The 'cdata' must be a pointer or # an array. Can be passed to functions expecting a buffer, or directly # manipulated with: # # buf[:] get a copy of it in a regular string, or # buf[idx] as a single character # buf[:] = ... # buf[idx] = ... change the content # """ # note that 'buffer' is a type, set on this instance by __init__ def from_buffer(self, python_buffer, require_writable=False): """Return a that points to the data of the given Python object, which must support the buffer interface. Note that this is not meant to be used on the built-in types str or unicode (you can build 'char[]' arrays explicitly) but only on objects containing large quantities of raw data in some other format, like 'array.array' or numpy arrays. """ return self._backend.from_buffer(self.BCharA, python_buffer, require_writable) def memmove(self, dest, src, n): """ffi.memmove(dest, src, n) copies n bytes of memory from src to dest. Like the C function memmove(), the memory areas may overlap; apart from that it behaves like the C function memcpy(). 'src' can be any cdata ptr or array, or any Python buffer object. 'dest' can be any cdata ptr or array, or a writable Python buffer object. The size to copy, 'n', is always measured in bytes. Unlike other methods, this one supports all Python buffer including byte strings and bytearrays---but it still does not support non-contiguous buffers. """ return self._backend.memmove(dest, src, n) def callback(self, cdecl, python_callable=None, error=None, onerror=None): """Return a callback object or a decorator making such a callback object. 'cdecl' must name a C function pointer type. The callback invokes the specified 'python_callable' (which may be provided either directly or via a decorator). Important: the callback object must be manually kept alive for as long as the callback may be invoked from the C level. """ def callback_decorator_wrap(python_callable): if not callable(python_callable): raise TypeError("the 'python_callable' argument " "is not callable") return self._backend.callback(cdecl, python_callable, error, onerror) if isinstance(cdecl, basestring): cdecl = self._typeof(cdecl, consider_function_as_funcptr=True) if python_callable is None: return callback_decorator_wrap # decorator mode else: return callback_decorator_wrap(python_callable) # direct mode def getctype(self, cdecl, replace_with=''): """Return a string giving the C type 'cdecl', which may be itself a string or a object. If 'replace_with' is given, it gives extra text to append (or insert for more complicated C types), like a variable name, or '*' to get actually the C type 'pointer-to-cdecl'. """ if isinstance(cdecl, basestring): cdecl = self._typeof(cdecl) replace_with = replace_with.strip() if (replace_with.startswith('*') and '&[' in self._backend.getcname(cdecl, '&')): replace_with = '(%s)' % replace_with elif replace_with and not replace_with[0] in '[(': replace_with = ' ' + replace_with return self._backend.getcname(cdecl, replace_with) def gc(self, cdata, destructor, size=0): """Return a new cdata object that points to the same data. Later, when this new cdata object is garbage-collected, 'destructor(old_cdata_object)' will be called. The optional 'size' gives an estimate of the size, used to trigger the garbage collection more eagerly. So far only used on PyPy. It tells the GC that the returned object keeps alive roughly 'size' bytes of external memory. """ return self._backend.gcp(cdata, destructor, size) def _get_cached_btype(self, type): assert self._lock.acquire(False) is False # call me with the lock! try: BType = self._cached_btypes[type] except KeyError: finishlist = [] BType = type.get_cached_btype(self, finishlist) for type in finishlist: type.finish_backend_type(self, finishlist) return BType def verify(self, source='', tmpdir=None, **kwargs): """Verify that the current ffi signatures compile on this machine, and return a dynamic library object. The dynamic library can be used to call functions and access global variables declared in this 'ffi'. The library is compiled by the C compiler: it gives you C-level API compatibility (including calling macros). This is unlike 'ffi.dlopen()', which requires binary compatibility in the signatures. """ from .verifier import Verifier, _caller_dir_pycache # # If set_unicode(True) was called, insert the UNICODE and # _UNICODE macro declarations if self._windows_unicode: self._apply_windows_unicode(kwargs) # # Set the tmpdir here, and not in Verifier.__init__: it picks # up the caller's directory, which we want to be the caller of # ffi.verify(), as opposed to the caller of Veritier(). tmpdir = tmpdir or _caller_dir_pycache() # # Make a Verifier() and use it to load the library. self.verifier = Verifier(self, source, tmpdir, **kwargs) lib = self.verifier.load_library() # # Save the loaded library for keep-alive purposes, even # if the caller doesn't keep it alive itself (it should). self._libraries.append(lib) return lib def _get_errno(self): return self._backend.get_errno() def _set_errno(self, errno): self._backend.set_errno(errno) errno = property(_get_errno, _set_errno, None, "the value of 'errno' from/to the C calls") def getwinerror(self, code=-1): return self._backend.getwinerror(code) def _pointer_to(self, ctype): with self._lock: return model.pointer_cache(self, ctype) def addressof(self, cdata, *fields_or_indexes): """Return the address of a . If 'fields_or_indexes' are given, returns the address of that field or array item in the structure or array, recursively in case of nested structures. """ try: ctype = self._backend.typeof(cdata) except TypeError: if '__addressof__' in type(cdata).__dict__: return type(cdata).__addressof__(cdata, *fields_or_indexes) raise if fields_or_indexes: ctype, offset = self._typeoffsetof(ctype, *fields_or_indexes) else: if ctype.kind == "pointer": raise TypeError("addressof(pointer)") offset = 0 ctypeptr = self._pointer_to(ctype) return self._backend.rawaddressof(ctypeptr, cdata, offset) def _typeoffsetof(self, ctype, field_or_index, *fields_or_indexes): ctype, offset = self._backend.typeoffsetof(ctype, field_or_index) for field1 in fields_or_indexes: ctype, offset1 = self._backend.typeoffsetof(ctype, field1, 1) offset += offset1 return ctype, offset def include(self, ffi_to_include): """Includes the typedefs, structs, unions and enums defined in another FFI instance. Usage is similar to a #include in C, where a part of the program might include types defined in another part for its own usage. Note that the include() method has no effect on functions, constants and global variables, which must anyway be accessed directly from the lib object returned by the original FFI instance. """ if not isinstance(ffi_to_include, FFI): raise TypeError("ffi.include() expects an argument that is also of" " type cffi.FFI, not %r" % ( type(ffi_to_include).__name__,)) if ffi_to_include is self: raise ValueError("self.include(self)") with ffi_to_include._lock: with self._lock: self._parser.include(ffi_to_include._parser) self._cdefsources.append('[') self._cdefsources.extend(ffi_to_include._cdefsources) self._cdefsources.append(']') self._included_ffis.append(ffi_to_include) def new_handle(self, x): return self._backend.newp_handle(self.BVoidP, x) def from_handle(self, x): return self._backend.from_handle(x) def set_unicode(self, enabled_flag): """Windows: if 'enabled_flag' is True, enable the UNICODE and _UNICODE defines in C, and declare the types like TCHAR and LPTCSTR to be (pointers to) wchar_t. If 'enabled_flag' is False, declare these types to be (pointers to) plain 8-bit characters. This is mostly for backward compatibility; you usually want True. """ if self._windows_unicode is not None: raise ValueError("set_unicode() can only be called once") enabled_flag = bool(enabled_flag) if enabled_flag: self.cdef("typedef wchar_t TBYTE;" "typedef wchar_t TCHAR;" "typedef const wchar_t *LPCTSTR;" "typedef const wchar_t *PCTSTR;" "typedef wchar_t *LPTSTR;" "typedef wchar_t *PTSTR;" "typedef TBYTE *PTBYTE;" "typedef TCHAR *PTCHAR;") else: self.cdef("typedef char TBYTE;" "typedef char TCHAR;" "typedef const char *LPCTSTR;" "typedef const char *PCTSTR;" "typedef char *LPTSTR;" "typedef char *PTSTR;" "typedef TBYTE *PTBYTE;" "typedef TCHAR *PTCHAR;") self._windows_unicode = enabled_flag def _apply_windows_unicode(self, kwds): defmacros = kwds.get('define_macros', ()) if not isinstance(defmacros, (list, tuple)): raise TypeError("'define_macros' must be a list or tuple") defmacros = list(defmacros) + [('UNICODE', '1'), ('_UNICODE', '1')] kwds['define_macros'] = defmacros def _apply_embedding_fix(self, kwds): # must include an argument like "-lpython2.7" for the compiler def ensure(key, value): lst = kwds.setdefault(key, []) if value not in lst: lst.append(value) # if '__pypy__' in sys.builtin_module_names: import os if sys.platform == "win32": # we need 'libpypy-c.lib'. Current distributions of # pypy (>= 4.1) contain it as 'libs/python27.lib'. pythonlib = "python27" if hasattr(sys, 'prefix'): ensure('library_dirs', os.path.join(sys.prefix, 'libs')) else: # we need 'libpypy-c.{so,dylib}', which should be by # default located in 'sys.prefix/bin' for installed # systems. if sys.version_info < (3,): pythonlib = "pypy-c" else: pythonlib = "pypy3-c" if hasattr(sys, 'prefix'): ensure('library_dirs', os.path.join(sys.prefix, 'bin')) # On uninstalled pypy's, the libpypy-c is typically found in # .../pypy/goal/. if hasattr(sys, 'prefix'): ensure('library_dirs', os.path.join(sys.prefix, 'pypy', 'goal')) else: if sys.platform == "win32": template = "python%d%d" if hasattr(sys, 'gettotalrefcount'): template += '_d' else: try: import sysconfig except ImportError: # 2.6 from distutils import sysconfig template = "python%d.%d" if sysconfig.get_config_var('DEBUG_EXT'): template += sysconfig.get_config_var('DEBUG_EXT') pythonlib = (template % (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff)) if hasattr(sys, 'abiflags'): pythonlib += sys.abiflags ensure('libraries', pythonlib) if sys.platform == "win32": ensure('extra_link_args', '/MANIFEST') def set_source(self, module_name, source, source_extension='.c', **kwds): import os if hasattr(self, '_assigned_source'): raise ValueError("set_source() cannot be called several times " "per ffi object") if not isinstance(module_name, basestring): raise TypeError("'module_name' must be a string") if os.sep in module_name or (os.altsep and os.altsep in module_name): raise ValueError("'module_name' must not contain '/': use a dotted " "name to make a 'package.module' location") self._assigned_source = (str(module_name), source, source_extension, kwds) def distutils_extension(self, tmpdir='build', verbose=True): from distutils.dir_util import mkpath from .recompiler import recompile # if not hasattr(self, '_assigned_source'): if hasattr(self, 'verifier'): # fallback, 'tmpdir' ignored return self.verifier.get_extension() raise ValueError("set_source() must be called before" " distutils_extension()") module_name, source, source_extension, kwds = self._assigned_source if source is None: raise TypeError("distutils_extension() is only for C extension " "modules, not for dlopen()-style pure Python " "modules") mkpath(tmpdir) ext, updated = recompile(self, module_name, source, tmpdir=tmpdir, extradir=tmpdir, source_extension=source_extension, call_c_compiler=False, **kwds) if verbose: if updated: sys.stderr.write("regenerated: %r\n" % (ext.sources[0],)) else: sys.stderr.write("not modified: %r\n" % (ext.sources[0],)) return ext def emit_c_code(self, filename): from .recompiler import recompile # if not hasattr(self, '_assigned_source'): raise ValueError("set_source() must be called before emit_c_code()") module_name, source, source_extension, kwds = self._assigned_source if source is None: raise TypeError("emit_c_code() is only for C extension modules, " "not for dlopen()-style pure Python modules") recompile(self, module_name, source, c_file=filename, call_c_compiler=False, **kwds) def emit_python_code(self, filename): from .recompiler import recompile # if not hasattr(self, '_assigned_source'): raise ValueError("set_source() must be called before emit_c_code()") module_name, source, source_extension, kwds = self._assigned_source if source is not None: raise TypeError("emit_python_code() is only for dlopen()-style " "pure Python modules, not for C extension modules") recompile(self, module_name, source, c_file=filename, call_c_compiler=False, **kwds) def compile(self, tmpdir='.', verbose=0, target=None, debug=None): """The 'target' argument gives the final file name of the compiled DLL. Use '*' to force distutils' choice, suitable for regular CPython C API modules. Use a file name ending in '.*' to ask for the system's default extension for dynamic libraries (.so/.dll/.dylib). The default is '*' when building a non-embedded C API extension, and (module_name + '.*') when building an embedded library. """ from .recompiler import recompile # if not hasattr(self, '_assigned_source'): raise ValueError("set_source() must be called before compile()") module_name, source, source_extension, kwds = self._assigned_source return recompile(self, module_name, source, tmpdir=tmpdir, target=target, source_extension=source_extension, compiler_verbose=verbose, debug=debug, **kwds) def init_once(self, func, tag): # Read _init_once_cache[tag], which is either (False, lock) if # we're calling the function now in some thread, or (True, result). # Don't call setdefault() in most cases, to avoid allocating and # immediately freeing a lock; but still use setdefaut() to avoid # races. try: x = self._init_once_cache[tag] except KeyError: x = self._init_once_cache.setdefault(tag, (False, allocate_lock())) # Common case: we got (True, result), so we return the result. if x[0]: return x[1] # Else, it's a lock. Acquire it to serialize the following tests. with x[1]: # Read again from _init_once_cache the current status. x = self._init_once_cache[tag] if x[0]: return x[1] # Call the function and store the result back. result = func() self._init_once_cache[tag] = (True, result) return result def embedding_init_code(self, pysource): if self._embedding: raise ValueError("embedding_init_code() can only be called once") # fix 'pysource' before it gets dumped into the C file: # - remove empty lines at the beginning, so it starts at "line 1" # - dedent, if all non-empty lines are indented # - check for SyntaxErrors import re match = re.match(r'\s*\n', pysource) if match: pysource = pysource[match.end():] lines = pysource.splitlines() or [''] prefix = re.match(r'\s*', lines[0]).group() for i in range(1, len(lines)): line = lines[i] if line.rstrip(): while not line.startswith(prefix): prefix = prefix[:-1] i = len(prefix) lines = [line[i:]+'\n' for line in lines] pysource = ''.join(lines) # compile(pysource, "cffi_init", "exec") # self._embedding = pysource def def_extern(self, *args, **kwds): raise ValueError("ffi.def_extern() is only available on API-mode FFI " "objects") def list_types(self): """Returns the user type names known to this FFI instance. This returns a tuple containing three lists of names: (typedef_names, names_of_structs, names_of_unions) """ typedefs = [] structs = [] unions = [] for key in self._parser._declarations: if key.startswith('typedef '): typedefs.append(key[8:]) elif key.startswith('struct '): structs.append(key[7:]) elif key.startswith('union '): unions.append(key[6:]) typedefs.sort() structs.sort() unions.sort() return (typedefs, structs, unions) def _load_backend_lib(backend, name, flags): import os if name is None: if sys.platform != "win32": return backend.load_library(None, flags) name = "c" # Windows: load_library(None) fails, but this works # on Python 2 (backward compatibility hack only) first_error = None if '.' in name or '/' in name or os.sep in name: try: return backend.load_library(name, flags) except OSError as e: first_error = e import ctypes.util path = ctypes.util.find_library(name) if path is None: if name == "c" and sys.platform == "win32" and sys.version_info >= (3,): raise OSError("dlopen(None) cannot work on Windows for Python 3 " "(see http://bugs.python.org/issue23606)") msg = ("ctypes.util.find_library() did not manage " "to locate a library called %r" % (name,)) if first_error is not None: msg = "%s. Additionally, %s" % (first_error, msg) raise OSError(msg) return backend.load_library(path, flags) def _make_ffi_library(ffi, libname, flags): backend = ffi._backend backendlib = _load_backend_lib(backend, libname, flags) # def accessor_function(name): key = 'function ' + name tp, _ = ffi._parser._declarations[key] BType = ffi._get_cached_btype(tp) value = backendlib.load_function(BType, name) library.__dict__[name] = value # def accessor_variable(name): key = 'variable ' + name tp, _ = ffi._parser._declarations[key] BType = ffi._get_cached_btype(tp) read_variable = backendlib.read_variable write_variable = backendlib.write_variable setattr(FFILibrary, name, property( lambda self: read_variable(BType, name), lambda self, value: write_variable(BType, name, value))) # def addressof_var(name): try: return addr_variables[name] except KeyError: with ffi._lock: if name not in addr_variables: key = 'variable ' + name tp, _ = ffi._parser._declarations[key] BType = ffi._get_cached_btype(tp) if BType.kind != 'array': BType = model.pointer_cache(ffi, BType) p = backendlib.load_function(BType, name) addr_variables[name] = p return addr_variables[name] # def accessor_constant(name): raise NotImplementedError("non-integer constant '%s' cannot be " "accessed from a dlopen() library" % (name,)) # def accessor_int_constant(name): library.__dict__[name] = ffi._parser._int_constants[name] # accessors = {} accessors_version = [False] addr_variables = {} # def update_accessors(): if accessors_version[0] is ffi._cdef_version: return # for key, (tp, _) in ffi._parser._declarations.items(): if not isinstance(tp, model.EnumType): tag, name = key.split(' ', 1) if tag == 'function': accessors[name] = accessor_function elif tag == 'variable': accessors[name] = accessor_variable elif tag == 'constant': accessors[name] = accessor_constant else: for i, enumname in enumerate(tp.enumerators): def accessor_enum(name, tp=tp, i=i): tp.check_not_partial() library.__dict__[name] = tp.enumvalues[i] accessors[enumname] = accessor_enum for name in ffi._parser._int_constants: accessors.setdefault(name, accessor_int_constant) accessors_version[0] = ffi._cdef_version # def make_accessor(name): with ffi._lock: if name in library.__dict__ or name in FFILibrary.__dict__: return # added by another thread while waiting for the lock if name not in accessors: update_accessors() if name not in accessors: raise AttributeError(name) accessors[name](name) # class FFILibrary(object): def __getattr__(self, name): make_accessor(name) return getattr(self, name) def __setattr__(self, name, value): try: property = getattr(self.__class__, name) except AttributeError: make_accessor(name) setattr(self, name, value) else: property.__set__(self, value) def __dir__(self): with ffi._lock: update_accessors() return accessors.keys() def __addressof__(self, name): if name in library.__dict__: return library.__dict__[name] if name in FFILibrary.__dict__: return addressof_var(name) make_accessor(name) if name in library.__dict__: return library.__dict__[name] if name in FFILibrary.__dict__: return addressof_var(name) raise AttributeError("cffi library has no function or " "global variable named '%s'" % (name,)) def __cffi_close__(self): backendlib.close_lib() self.__dict__.clear() # if libname is not None: try: if not isinstance(libname, str): # unicode, on Python 2 libname = libname.encode('utf-8') FFILibrary.__name__ = 'FFILibrary_%s' % libname except UnicodeError: pass library = FFILibrary() return library, library.__dict__ def _builtin_function_type(func): # a hack to make at least ffi.typeof(builtin_function) work, # if the builtin function was obtained by 'vengine_cpy'. import sys try: module = sys.modules[func.__module__] ffi = module._cffi_original_ffi types_of_builtin_funcs = module._cffi_types_of_builtin_funcs tp = types_of_builtin_funcs[func] except (KeyError, AttributeError, TypeError): return None else: with ffi._lock: return ffi._get_cached_btype(tp) lock.py000064400000001353152344566040006061 0ustar00import sys if sys.version_info < (3,): try: from thread import allocate_lock except ImportError: from dummy_thread import allocate_lock else: try: from _thread import allocate_lock except ImportError: from _dummy_thread import allocate_lock ##import sys ##l1 = allocate_lock ##class allocate_lock(object): ## def __init__(self): ## self._real = l1() ## def __enter__(self): ## for i in range(4, 0, -1): ## print sys._getframe(i).f_code ## print ## return self._real.__enter__() ## def __exit__(self, *args): ## return self._real.__exit__(*args) ## def acquire(self, f): ## assert f is False ## return self._real.acquire(f) setuptools_ext.py000064400000020006152344566040010226 0ustar00import os import sys try: basestring except NameError: # Python 3.x basestring = str def error(msg): from distutils.errors import DistutilsSetupError raise DistutilsSetupError(msg) def execfile(filename, glob): # We use execfile() (here rewritten for Python 3) instead of # __import__() to load the build script. The problem with # a normal import is that in some packages, the intermediate # __init__.py files may already try to import the file that # we are generating. with open(filename) as f: src = f.read() src += '\n' # Python 2.6 compatibility code = compile(src, filename, 'exec') exec(code, glob, glob) def add_cffi_module(dist, mod_spec): from cffi.api import FFI if not isinstance(mod_spec, basestring): error("argument to 'cffi_modules=...' must be a str or a list of str," " not %r" % (type(mod_spec).__name__,)) mod_spec = str(mod_spec) try: build_file_name, ffi_var_name = mod_spec.split(':') except ValueError: error("%r must be of the form 'path/build.py:ffi_variable'" % (mod_spec,)) if not os.path.exists(build_file_name): ext = '' rewritten = build_file_name.replace('.', '/') + '.py' if os.path.exists(rewritten): ext = ' (rewrite cffi_modules to [%r])' % ( rewritten + ':' + ffi_var_name,) error("%r does not name an existing file%s" % (build_file_name, ext)) mod_vars = {'__name__': '__cffi__', '__file__': build_file_name} execfile(build_file_name, mod_vars) try: ffi = mod_vars[ffi_var_name] except KeyError: error("%r: object %r not found in module" % (mod_spec, ffi_var_name)) if not isinstance(ffi, FFI): ffi = ffi() # maybe it's a function instead of directly an ffi if not isinstance(ffi, FFI): error("%r is not an FFI instance (got %r)" % (mod_spec, type(ffi).__name__)) if not hasattr(ffi, '_assigned_source'): error("%r: the set_source() method was not called" % (mod_spec,)) module_name, source, source_extension, kwds = ffi._assigned_source if ffi._windows_unicode: kwds = kwds.copy() ffi._apply_windows_unicode(kwds) if source is None: _add_py_module(dist, ffi, module_name) else: _add_c_module(dist, ffi, module_name, source, source_extension, kwds) def _set_py_limited_api(Extension, kwds): """ Add py_limited_api to kwds if setuptools >= 26 is in use. Do not alter the setting if it already exists. Setuptools takes care of ignoring the flag on Python 2 and PyPy. CPython itself should ignore the flag in a debugging version (by not listing .abi3.so in the extensions it supports), but it doesn't so far, creating troubles. That's why we check for "not hasattr(sys, 'gettotalrefcount')" (the 2.7 compatible equivalent of 'd' not in sys.abiflags). (http://bugs.python.org/issue28401) On Windows, it's better not to use py_limited_api until issue #355 can be resolved (by having virtualenv copy PYTHON3.DLL). See also the start of _cffi_include.h. """ if ('py_limited_api' not in kwds and not hasattr(sys, 'gettotalrefcount') and sys.platform != 'win32'): import setuptools try: setuptools_major_version = int(setuptools.__version__.partition('.')[0]) if setuptools_major_version >= 26: kwds['py_limited_api'] = True except ValueError: # certain development versions of setuptools # If we don't know the version number of setuptools, we # try to set 'py_limited_api' anyway. At worst, we get a # warning. kwds['py_limited_api'] = True return kwds def _add_c_module(dist, ffi, module_name, source, source_extension, kwds): from distutils.core import Extension # We are a setuptools extension. Need this build_ext for py_limited_api. from setuptools.command.build_ext import build_ext from distutils.dir_util import mkpath from distutils import log from cffi import recompiler allsources = ['$PLACEHOLDER'] allsources.extend(kwds.pop('sources', [])) kwds = _set_py_limited_api(Extension, kwds) ext = Extension(name=module_name, sources=allsources, **kwds) def make_mod(tmpdir, pre_run=None): c_file = os.path.join(tmpdir, module_name + source_extension) log.info("generating cffi module %r" % c_file) mkpath(tmpdir) # a setuptools-only, API-only hook: called with the "ext" and "ffi" # arguments just before we turn the ffi into C code. To use it, # subclass the 'distutils.command.build_ext.build_ext' class and # add a method 'def pre_run(self, ext, ffi)'. if pre_run is not None: pre_run(ext, ffi) updated = recompiler.make_c_source(ffi, module_name, source, c_file) if not updated: log.info("already up-to-date") return c_file if dist.ext_modules is None: dist.ext_modules = [] dist.ext_modules.append(ext) base_class = dist.cmdclass.get('build_ext', build_ext) class build_ext_make_mod(base_class): def run(self): if ext.sources[0] == '$PLACEHOLDER': pre_run = getattr(self, 'pre_run', None) ext.sources[0] = make_mod(self.build_temp, pre_run) base_class.run(self) dist.cmdclass['build_ext'] = build_ext_make_mod # NB. multiple runs here will create multiple 'build_ext_make_mod' # classes. Even in this case the 'build_ext' command should be # run once; but just in case, the logic above does nothing if # called again. def _add_py_module(dist, ffi, module_name): from distutils.dir_util import mkpath from setuptools.command.build_py import build_py from setuptools.command.build_ext import build_ext from distutils import log from cffi import recompiler def generate_mod(py_file): log.info("generating cffi module %r" % py_file) mkpath(os.path.dirname(py_file)) updated = recompiler.make_py_source(ffi, module_name, py_file) if not updated: log.info("already up-to-date") base_class = dist.cmdclass.get('build_py', build_py) class build_py_make_mod(base_class): def run(self): base_class.run(self) module_path = module_name.split('.') module_path[-1] += '.py' generate_mod(os.path.join(self.build_lib, *module_path)) dist.cmdclass['build_py'] = build_py_make_mod # distutils and setuptools have no notion I could find of a # generated python module. If we don't add module_name to # dist.py_modules, then things mostly work but there are some # combination of options (--root and --record) that will miss # the module. So we add it here, which gives a few apparently # harmless warnings about not finding the file outside the # build directory. if dist.py_modules is None: dist.py_modules = [] dist.py_modules.append(module_name) # the following is only for "build_ext -i" base_class_2 = dist.cmdclass.get('build_ext', build_ext) class build_ext_make_mod(base_class_2): def run(self): base_class_2.run(self) if self.inplace: # from get_ext_fullpath() in distutils/command/build_ext.py module_path = module_name.split('.') package = '.'.join(module_path[:-1]) build_py = self.get_finalized_command('build_py') package_dir = build_py.get_package_dir(package) file_name = module_path[-1] + '.py' generate_mod(os.path.join(package_dir, file_name)) dist.cmdclass['build_ext'] = build_ext_make_mod def cffi_modules(dist, attr, value): assert attr == 'cffi_modules' if isinstance(value, basestring): value = [value] for cffi_module in value: add_cffi_module(dist, cffi_module) model.py000064400000052060152344566040006232 0ustar00import types import weakref from .lock import allocate_lock from .error import CDefError, VerificationError, VerificationMissing # type qualifiers Q_CONST = 0x01 Q_RESTRICT = 0x02 Q_VOLATILE = 0x04 def qualify(quals, replace_with): if quals & Q_CONST: replace_with = ' const ' + replace_with.lstrip() if quals & Q_VOLATILE: replace_with = ' volatile ' + replace_with.lstrip() if quals & Q_RESTRICT: # It seems that __restrict is supported by gcc and msvc. # If you hit some different compiler, add a #define in # _cffi_include.h for it (and in its copies, documented there) replace_with = ' __restrict ' + replace_with.lstrip() return replace_with class BaseTypeByIdentity(object): is_array_type = False is_raw_function = False def get_c_name(self, replace_with='', context='a C file', quals=0): result = self.c_name_with_marker assert result.count('&') == 1 # some logic duplication with ffi.getctype()... :-( replace_with = replace_with.strip() if replace_with: if replace_with.startswith('*') and '&[' in result: replace_with = '(%s)' % replace_with elif not replace_with[0] in '[(': replace_with = ' ' + replace_with replace_with = qualify(quals, replace_with) result = result.replace('&', replace_with) if '$' in result: raise VerificationError( "cannot generate '%s' in %s: unknown type name" % (self._get_c_name(), context)) return result def _get_c_name(self): return self.c_name_with_marker.replace('&', '') def has_c_name(self): return '$' not in self._get_c_name() def is_integer_type(self): return False def get_cached_btype(self, ffi, finishlist, can_delay=False): try: BType = ffi._cached_btypes[self] except KeyError: BType = self.build_backend_type(ffi, finishlist) BType2 = ffi._cached_btypes.setdefault(self, BType) assert BType2 is BType return BType def __repr__(self): return '<%s>' % (self._get_c_name(),) def _get_items(self): return [(name, getattr(self, name)) for name in self._attrs_] class BaseType(BaseTypeByIdentity): def __eq__(self, other): return (self.__class__ == other.__class__ and self._get_items() == other._get_items()) def __ne__(self, other): return not self == other def __hash__(self): return hash((self.__class__, tuple(self._get_items()))) class VoidType(BaseType): _attrs_ = () def __init__(self): self.c_name_with_marker = 'void&' def build_backend_type(self, ffi, finishlist): return global_cache(self, ffi, 'new_void_type') void_type = VoidType() class BasePrimitiveType(BaseType): def is_complex_type(self): return False class PrimitiveType(BasePrimitiveType): _attrs_ = ('name',) ALL_PRIMITIVE_TYPES = { 'char': 'c', 'short': 'i', 'int': 'i', 'long': 'i', 'long long': 'i', 'signed char': 'i', 'unsigned char': 'i', 'unsigned short': 'i', 'unsigned int': 'i', 'unsigned long': 'i', 'unsigned long long': 'i', 'float': 'f', 'double': 'f', 'long double': 'f', 'float _Complex': 'j', 'double _Complex': 'j', '_Bool': 'i', # the following types are not primitive in the C sense 'wchar_t': 'c', 'char16_t': 'c', 'char32_t': 'c', 'int8_t': 'i', 'uint8_t': 'i', 'int16_t': 'i', 'uint16_t': 'i', 'int32_t': 'i', 'uint32_t': 'i', 'int64_t': 'i', 'uint64_t': 'i', 'int_least8_t': 'i', 'uint_least8_t': 'i', 'int_least16_t': 'i', 'uint_least16_t': 'i', 'int_least32_t': 'i', 'uint_least32_t': 'i', 'int_least64_t': 'i', 'uint_least64_t': 'i', 'int_fast8_t': 'i', 'uint_fast8_t': 'i', 'int_fast16_t': 'i', 'uint_fast16_t': 'i', 'int_fast32_t': 'i', 'uint_fast32_t': 'i', 'int_fast64_t': 'i', 'uint_fast64_t': 'i', 'intptr_t': 'i', 'uintptr_t': 'i', 'intmax_t': 'i', 'uintmax_t': 'i', 'ptrdiff_t': 'i', 'size_t': 'i', 'ssize_t': 'i', } def __init__(self, name): assert name in self.ALL_PRIMITIVE_TYPES self.name = name self.c_name_with_marker = name + '&' def is_char_type(self): return self.ALL_PRIMITIVE_TYPES[self.name] == 'c' def is_integer_type(self): return self.ALL_PRIMITIVE_TYPES[self.name] == 'i' def is_float_type(self): return self.ALL_PRIMITIVE_TYPES[self.name] == 'f' def is_complex_type(self): return self.ALL_PRIMITIVE_TYPES[self.name] == 'j' def build_backend_type(self, ffi, finishlist): return global_cache(self, ffi, 'new_primitive_type', self.name) class UnknownIntegerType(BasePrimitiveType): _attrs_ = ('name',) def __init__(self, name): self.name = name self.c_name_with_marker = name + '&' def is_integer_type(self): return True def build_backend_type(self, ffi, finishlist): raise NotImplementedError("integer type '%s' can only be used after " "compilation" % self.name) class UnknownFloatType(BasePrimitiveType): _attrs_ = ('name', ) def __init__(self, name): self.name = name self.c_name_with_marker = name + '&' def build_backend_type(self, ffi, finishlist): raise NotImplementedError("float type '%s' can only be used after " "compilation" % self.name) class BaseFunctionType(BaseType): _attrs_ = ('args', 'result', 'ellipsis', 'abi') def __init__(self, args, result, ellipsis, abi=None): self.args = args self.result = result self.ellipsis = ellipsis self.abi = abi # reprargs = [arg._get_c_name() for arg in self.args] if self.ellipsis: reprargs.append('...') reprargs = reprargs or ['void'] replace_with = self._base_pattern % (', '.join(reprargs),) if abi is not None: replace_with = replace_with[:1] + abi + ' ' + replace_with[1:] self.c_name_with_marker = ( self.result.c_name_with_marker.replace('&', replace_with)) class RawFunctionType(BaseFunctionType): # Corresponds to a C type like 'int(int)', which is the C type of # a function, but not a pointer-to-function. The backend has no # notion of such a type; it's used temporarily by parsing. _base_pattern = '(&)(%s)' is_raw_function = True def build_backend_type(self, ffi, finishlist): raise CDefError("cannot render the type %r: it is a function " "type, not a pointer-to-function type" % (self,)) def as_function_pointer(self): return FunctionPtrType(self.args, self.result, self.ellipsis, self.abi) class FunctionPtrType(BaseFunctionType): _base_pattern = '(*&)(%s)' def build_backend_type(self, ffi, finishlist): result = self.result.get_cached_btype(ffi, finishlist) args = [] for tp in self.args: args.append(tp.get_cached_btype(ffi, finishlist)) abi_args = () if self.abi == "__stdcall": if not self.ellipsis: # __stdcall ignored for variadic funcs try: abi_args = (ffi._backend.FFI_STDCALL,) except AttributeError: pass return global_cache(self, ffi, 'new_function_type', tuple(args), result, self.ellipsis, *abi_args) def as_raw_function(self): return RawFunctionType(self.args, self.result, self.ellipsis, self.abi) class PointerType(BaseType): _attrs_ = ('totype', 'quals') def __init__(self, totype, quals=0): self.totype = totype self.quals = quals extra = qualify(quals, " *&") if totype.is_array_type: extra = "(%s)" % (extra.lstrip(),) self.c_name_with_marker = totype.c_name_with_marker.replace('&', extra) def build_backend_type(self, ffi, finishlist): BItem = self.totype.get_cached_btype(ffi, finishlist, can_delay=True) return global_cache(self, ffi, 'new_pointer_type', BItem) voidp_type = PointerType(void_type) def ConstPointerType(totype): return PointerType(totype, Q_CONST) const_voidp_type = ConstPointerType(void_type) class NamedPointerType(PointerType): _attrs_ = ('totype', 'name') def __init__(self, totype, name, quals=0): PointerType.__init__(self, totype, quals) self.name = name self.c_name_with_marker = name + '&' class ArrayType(BaseType): _attrs_ = ('item', 'length') is_array_type = True def __init__(self, item, length): self.item = item self.length = length # if length is None: brackets = '&[]' elif length == '...': brackets = '&[/*...*/]' else: brackets = '&[%s]' % length self.c_name_with_marker = ( self.item.c_name_with_marker.replace('&', brackets)) def resolve_length(self, newlength): return ArrayType(self.item, newlength) def build_backend_type(self, ffi, finishlist): if self.length == '...': raise CDefError("cannot render the type %r: unknown length" % (self,)) self.item.get_cached_btype(ffi, finishlist) # force the item BType BPtrItem = PointerType(self.item).get_cached_btype(ffi, finishlist) return global_cache(self, ffi, 'new_array_type', BPtrItem, self.length) char_array_type = ArrayType(PrimitiveType('char'), None) class StructOrUnionOrEnum(BaseTypeByIdentity): _attrs_ = ('name',) forcename = None def build_c_name_with_marker(self): name = self.forcename or '%s %s' % (self.kind, self.name) self.c_name_with_marker = name + '&' def force_the_name(self, forcename): self.forcename = forcename self.build_c_name_with_marker() def get_official_name(self): assert self.c_name_with_marker.endswith('&') return self.c_name_with_marker[:-1] class StructOrUnion(StructOrUnionOrEnum): fixedlayout = None completed = 0 partial = False packed = False def __init__(self, name, fldnames, fldtypes, fldbitsize, fldquals=None): self.name = name self.fldnames = fldnames self.fldtypes = fldtypes self.fldbitsize = fldbitsize self.fldquals = fldquals self.build_c_name_with_marker() def anonymous_struct_fields(self): if self.fldtypes is not None: for name, type in zip(self.fldnames, self.fldtypes): if name == '' and isinstance(type, StructOrUnion): yield type def enumfields(self, expand_anonymous_struct_union=True): fldquals = self.fldquals if fldquals is None: fldquals = (0,) * len(self.fldnames) for name, type, bitsize, quals in zip(self.fldnames, self.fldtypes, self.fldbitsize, fldquals): if (name == '' and isinstance(type, StructOrUnion) and expand_anonymous_struct_union): # nested anonymous struct/union for result in type.enumfields(): yield result else: yield (name, type, bitsize, quals) def force_flatten(self): # force the struct or union to have a declaration that lists # directly all fields returned by enumfields(), flattening # nested anonymous structs/unions. names = [] types = [] bitsizes = [] fldquals = [] for name, type, bitsize, quals in self.enumfields(): names.append(name) types.append(type) bitsizes.append(bitsize) fldquals.append(quals) self.fldnames = tuple(names) self.fldtypes = tuple(types) self.fldbitsize = tuple(bitsizes) self.fldquals = tuple(fldquals) def get_cached_btype(self, ffi, finishlist, can_delay=False): BType = StructOrUnionOrEnum.get_cached_btype(self, ffi, finishlist, can_delay) if not can_delay: self.finish_backend_type(ffi, finishlist) return BType def finish_backend_type(self, ffi, finishlist): if self.completed: if self.completed != 2: raise NotImplementedError("recursive structure declaration " "for '%s'" % (self.name,)) return BType = ffi._cached_btypes[self] # self.completed = 1 # if self.fldtypes is None: pass # not completing it: it's an opaque struct # elif self.fixedlayout is None: fldtypes = [tp.get_cached_btype(ffi, finishlist) for tp in self.fldtypes] lst = list(zip(self.fldnames, fldtypes, self.fldbitsize)) sflags = 0 if self.packed: sflags = 8 # SF_PACKED ffi._backend.complete_struct_or_union(BType, lst, self, -1, -1, sflags) # else: fldtypes = [] fieldofs, fieldsize, totalsize, totalalignment = self.fixedlayout for i in range(len(self.fldnames)): fsize = fieldsize[i] ftype = self.fldtypes[i] # if isinstance(ftype, ArrayType) and ftype.length == '...': # fix the length to match the total size BItemType = ftype.item.get_cached_btype(ffi, finishlist) nlen, nrest = divmod(fsize, ffi.sizeof(BItemType)) if nrest != 0: self._verification_error( "field '%s.%s' has a bogus size?" % ( self.name, self.fldnames[i] or '{}')) ftype = ftype.resolve_length(nlen) self.fldtypes = (self.fldtypes[:i] + (ftype,) + self.fldtypes[i+1:]) # BFieldType = ftype.get_cached_btype(ffi, finishlist) if isinstance(ftype, ArrayType) and ftype.length is None: assert fsize == 0 else: bitemsize = ffi.sizeof(BFieldType) if bitemsize != fsize: self._verification_error( "field '%s.%s' is declared as %d bytes, but is " "really %d bytes" % (self.name, self.fldnames[i] or '{}', bitemsize, fsize)) fldtypes.append(BFieldType) # lst = list(zip(self.fldnames, fldtypes, self.fldbitsize, fieldofs)) ffi._backend.complete_struct_or_union(BType, lst, self, totalsize, totalalignment) self.completed = 2 def _verification_error(self, msg): raise VerificationError(msg) def check_not_partial(self): if self.partial and self.fixedlayout is None: raise VerificationMissing(self._get_c_name()) def build_backend_type(self, ffi, finishlist): self.check_not_partial() finishlist.append(self) # return global_cache(self, ffi, 'new_%s_type' % self.kind, self.get_official_name(), key=self) class StructType(StructOrUnion): kind = 'struct' class UnionType(StructOrUnion): kind = 'union' class EnumType(StructOrUnionOrEnum): kind = 'enum' partial = False partial_resolved = False def __init__(self, name, enumerators, enumvalues, baseinttype=None): self.name = name self.enumerators = enumerators self.enumvalues = enumvalues self.baseinttype = baseinttype self.build_c_name_with_marker() def force_the_name(self, forcename): StructOrUnionOrEnum.force_the_name(self, forcename) if self.forcename is None: name = self.get_official_name() self.forcename = '$' + name.replace(' ', '_') def check_not_partial(self): if self.partial and not self.partial_resolved: raise VerificationMissing(self._get_c_name()) def build_backend_type(self, ffi, finishlist): self.check_not_partial() base_btype = self.build_baseinttype(ffi, finishlist) return global_cache(self, ffi, 'new_enum_type', self.get_official_name(), self.enumerators, self.enumvalues, base_btype, key=self) def build_baseinttype(self, ffi, finishlist): if self.baseinttype is not None: return self.baseinttype.get_cached_btype(ffi, finishlist) # if self.enumvalues: smallest_value = min(self.enumvalues) largest_value = max(self.enumvalues) else: import warnings try: # XXX! The goal is to ensure that the warnings.warn() # will not suppress the warning. We want to get it # several times if we reach this point several times. __warningregistry__.clear() except NameError: pass warnings.warn("%r has no values explicitly defined; " "guessing that it is equivalent to 'unsigned int'" % self._get_c_name()) smallest_value = largest_value = 0 if smallest_value < 0: # needs a signed type sign = 1 candidate1 = PrimitiveType("int") candidate2 = PrimitiveType("long") else: sign = 0 candidate1 = PrimitiveType("unsigned int") candidate2 = PrimitiveType("unsigned long") btype1 = candidate1.get_cached_btype(ffi, finishlist) btype2 = candidate2.get_cached_btype(ffi, finishlist) size1 = ffi.sizeof(btype1) size2 = ffi.sizeof(btype2) if (smallest_value >= ((-1) << (8*size1-1)) and largest_value < (1 << (8*size1-sign))): return btype1 if (smallest_value >= ((-1) << (8*size2-1)) and largest_value < (1 << (8*size2-sign))): return btype2 raise CDefError("%s values don't all fit into either 'long' " "or 'unsigned long'" % self._get_c_name()) def unknown_type(name, structname=None): if structname is None: structname = '$%s' % name tp = StructType(structname, None, None, None) tp.force_the_name(name) tp.origin = "unknown_type" return tp def unknown_ptr_type(name, structname=None): if structname is None: structname = '$$%s' % name tp = StructType(structname, None, None, None) return NamedPointerType(tp, name) global_lock = allocate_lock() _typecache_cffi_backend = weakref.WeakValueDictionary() def get_typecache(backend): # returns _typecache_cffi_backend if backend is the _cffi_backend # module, or type(backend).__typecache if backend is an instance of # CTypesBackend (or some FakeBackend class during tests) if isinstance(backend, types.ModuleType): return _typecache_cffi_backend with global_lock: if not hasattr(type(backend), '__typecache'): type(backend).__typecache = weakref.WeakValueDictionary() return type(backend).__typecache def global_cache(srctype, ffi, funcname, *args, **kwds): key = kwds.pop('key', (funcname, args)) assert not kwds try: return ffi._typecache[key] except KeyError: pass try: res = getattr(ffi._backend, funcname)(*args) except NotImplementedError as e: raise NotImplementedError("%s: %r: %s" % (funcname, srctype, e)) # note that setdefault() on WeakValueDictionary is not atomic # and contains a rare bug (http://bugs.python.org/issue19542); # we have to use a lock and do it ourselves cache = ffi._typecache with global_lock: res1 = cache.get(key) if res1 is None: cache[key] = res return res else: return res1 def pointer_cache(ffi, BType): return global_cache('?', ffi, 'new_pointer_type', BType) def attach_exception_info(e, name): if e.args and type(e.args[0]) is str: e.args = ('%s: %s' % (name, e.args[0]),) + e.args[1:] ffiplatform.py000064400000007716152344566040007453 0ustar00import sys, os from .error import VerificationError LIST_OF_FILE_NAMES = ['sources', 'include_dirs', 'library_dirs', 'extra_objects', 'depends'] def get_extension(srcfilename, modname, sources=(), **kwds): _hack_at_distutils() from distutils.core import Extension allsources = [srcfilename] for src in sources: allsources.append(os.path.normpath(src)) return Extension(name=modname, sources=allsources, **kwds) def compile(tmpdir, ext, compiler_verbose=0, debug=None): """Compile a C extension module using distutils.""" _hack_at_distutils() saved_environ = os.environ.copy() try: outputfilename = _build(tmpdir, ext, compiler_verbose, debug) outputfilename = os.path.abspath(outputfilename) finally: # workaround for a distutils bugs where some env vars can # become longer and longer every time it is used for key, value in saved_environ.items(): if os.environ.get(key) != value: os.environ[key] = value return outputfilename def _build(tmpdir, ext, compiler_verbose=0, debug=None): # XXX compact but horrible :-( from distutils.core import Distribution import distutils.errors, distutils.log # dist = Distribution({'ext_modules': [ext]}) dist.parse_config_files() options = dist.get_option_dict('build_ext') if debug is None: debug = sys.flags.debug options['debug'] = ('ffiplatform', debug) options['force'] = ('ffiplatform', True) options['build_lib'] = ('ffiplatform', tmpdir) options['build_temp'] = ('ffiplatform', tmpdir) # try: old_level = distutils.log.set_threshold(0) or 0 try: distutils.log.set_verbosity(compiler_verbose) dist.run_command('build_ext') cmd_obj = dist.get_command_obj('build_ext') [soname] = cmd_obj.get_outputs() finally: distutils.log.set_threshold(old_level) except (distutils.errors.CompileError, distutils.errors.LinkError) as e: raise VerificationError('%s: %s' % (e.__class__.__name__, e)) # return soname try: from os.path import samefile except ImportError: def samefile(f1, f2): return os.path.abspath(f1) == os.path.abspath(f2) def maybe_relative_path(path): if not os.path.isabs(path): return path # already relative dir = path names = [] while True: prevdir = dir dir, name = os.path.split(prevdir) if dir == prevdir or not dir: return path # failed to make it relative names.append(name) try: if samefile(dir, os.curdir): names.reverse() return os.path.join(*names) except OSError: pass # ____________________________________________________________ try: int_or_long = (int, long) import cStringIO except NameError: int_or_long = int # Python 3 import io as cStringIO def _flatten(x, f): if isinstance(x, str): f.write('%ds%s' % (len(x), x)) elif isinstance(x, dict): keys = sorted(x.keys()) f.write('%dd' % len(keys)) for key in keys: _flatten(key, f) _flatten(x[key], f) elif isinstance(x, (list, tuple)): f.write('%dl' % len(x)) for value in x: _flatten(value, f) elif isinstance(x, int_or_long): f.write('%di' % (x,)) else: raise TypeError( "the keywords to verify() contains unsupported object %r" % (x,)) def flatten(x): f = cStringIO.StringIO() _flatten(x, f) return f.getvalue() def _hack_at_distutils(): # Windows-only workaround for some configurations: see # https://bugs.python.org/issue23246 (Python 2.7 with # a specific MS compiler suite download) if sys.platform == "win32": try: import setuptools # for side-effects, patches distutils except ImportError: pass parse_c_type.h000064400000013530152344566040007405 0ustar00 /* This part is from file 'cffi/parse_c_type.h'. It is copied at the beginning of C sources generated by CFFI's ffi.set_source(). */ typedef void *_cffi_opcode_t; #define _CFFI_OP(opcode, arg) (_cffi_opcode_t)(opcode | (((uintptr_t)(arg)) << 8)) #define _CFFI_GETOP(cffi_opcode) ((unsigned char)(uintptr_t)cffi_opcode) #define _CFFI_GETARG(cffi_opcode) (((intptr_t)cffi_opcode) >> 8) #define _CFFI_OP_PRIMITIVE 1 #define _CFFI_OP_POINTER 3 #define _CFFI_OP_ARRAY 5 #define _CFFI_OP_OPEN_ARRAY 7 #define _CFFI_OP_STRUCT_UNION 9 #define _CFFI_OP_ENUM 11 #define _CFFI_OP_FUNCTION 13 #define _CFFI_OP_FUNCTION_END 15 #define _CFFI_OP_NOOP 17 #define _CFFI_OP_BITFIELD 19 #define _CFFI_OP_TYPENAME 21 #define _CFFI_OP_CPYTHON_BLTN_V 23 // varargs #define _CFFI_OP_CPYTHON_BLTN_N 25 // noargs #define _CFFI_OP_CPYTHON_BLTN_O 27 // O (i.e. a single arg) #define _CFFI_OP_CONSTANT 29 #define _CFFI_OP_CONSTANT_INT 31 #define _CFFI_OP_GLOBAL_VAR 33 #define _CFFI_OP_DLOPEN_FUNC 35 #define _CFFI_OP_DLOPEN_CONST 37 #define _CFFI_OP_GLOBAL_VAR_F 39 #define _CFFI_OP_EXTERN_PYTHON 41 #define _CFFI_PRIM_VOID 0 #define _CFFI_PRIM_BOOL 1 #define _CFFI_PRIM_CHAR 2 #define _CFFI_PRIM_SCHAR 3 #define _CFFI_PRIM_UCHAR 4 #define _CFFI_PRIM_SHORT 5 #define _CFFI_PRIM_USHORT 6 #define _CFFI_PRIM_INT 7 #define _CFFI_PRIM_UINT 8 #define _CFFI_PRIM_LONG 9 #define _CFFI_PRIM_ULONG 10 #define _CFFI_PRIM_LONGLONG 11 #define _CFFI_PRIM_ULONGLONG 12 #define _CFFI_PRIM_FLOAT 13 #define _CFFI_PRIM_DOUBLE 14 #define _CFFI_PRIM_LONGDOUBLE 15 #define _CFFI_PRIM_WCHAR 16 #define _CFFI_PRIM_INT8 17 #define _CFFI_PRIM_UINT8 18 #define _CFFI_PRIM_INT16 19 #define _CFFI_PRIM_UINT16 20 #define _CFFI_PRIM_INT32 21 #define _CFFI_PRIM_UINT32 22 #define _CFFI_PRIM_INT64 23 #define _CFFI_PRIM_UINT64 24 #define _CFFI_PRIM_INTPTR 25 #define _CFFI_PRIM_UINTPTR 26 #define _CFFI_PRIM_PTRDIFF 27 #define _CFFI_PRIM_SIZE 28 #define _CFFI_PRIM_SSIZE 29 #define _CFFI_PRIM_INT_LEAST8 30 #define _CFFI_PRIM_UINT_LEAST8 31 #define _CFFI_PRIM_INT_LEAST16 32 #define _CFFI_PRIM_UINT_LEAST16 33 #define _CFFI_PRIM_INT_LEAST32 34 #define _CFFI_PRIM_UINT_LEAST32 35 #define _CFFI_PRIM_INT_LEAST64 36 #define _CFFI_PRIM_UINT_LEAST64 37 #define _CFFI_PRIM_INT_FAST8 38 #define _CFFI_PRIM_UINT_FAST8 39 #define _CFFI_PRIM_INT_FAST16 40 #define _CFFI_PRIM_UINT_FAST16 41 #define _CFFI_PRIM_INT_FAST32 42 #define _CFFI_PRIM_UINT_FAST32 43 #define _CFFI_PRIM_INT_FAST64 44 #define _CFFI_PRIM_UINT_FAST64 45 #define _CFFI_PRIM_INTMAX 46 #define _CFFI_PRIM_UINTMAX 47 #define _CFFI_PRIM_FLOATCOMPLEX 48 #define _CFFI_PRIM_DOUBLECOMPLEX 49 #define _CFFI_PRIM_CHAR16 50 #define _CFFI_PRIM_CHAR32 51 #define _CFFI__NUM_PRIM 52 #define _CFFI__UNKNOWN_PRIM (-1) #define _CFFI__UNKNOWN_FLOAT_PRIM (-2) #define _CFFI__UNKNOWN_LONG_DOUBLE (-3) #define _CFFI__IO_FILE_STRUCT (-1) struct _cffi_global_s { const char *name; void *address; _cffi_opcode_t type_op; void *size_or_direct_fn; // OP_GLOBAL_VAR: size, or 0 if unknown // OP_CPYTHON_BLTN_*: addr of direct function }; struct _cffi_getconst_s { unsigned long long value; const struct _cffi_type_context_s *ctx; int gindex; }; struct _cffi_struct_union_s { const char *name; int type_index; // -> _cffi_types, on a OP_STRUCT_UNION int flags; // _CFFI_F_* flags below size_t size; int alignment; int first_field_index; // -> _cffi_fields array int num_fields; }; #define _CFFI_F_UNION 0x01 // is a union, not a struct #define _CFFI_F_CHECK_FIELDS 0x02 // complain if fields are not in the // "standard layout" or if some are missing #define _CFFI_F_PACKED 0x04 // for CHECK_FIELDS, assume a packed struct #define _CFFI_F_EXTERNAL 0x08 // in some other ffi.include() #define _CFFI_F_OPAQUE 0x10 // opaque struct _cffi_field_s { const char *name; size_t field_offset; size_t field_size; _cffi_opcode_t field_type_op; }; struct _cffi_enum_s { const char *name; int type_index; // -> _cffi_types, on a OP_ENUM int type_prim; // _CFFI_PRIM_xxx const char *enumerators; // comma-delimited string }; struct _cffi_typename_s { const char *name; int type_index; /* if opaque, points to a possibly artificial OP_STRUCT which is itself opaque */ }; struct _cffi_type_context_s { _cffi_opcode_t *types; const struct _cffi_global_s *globals; const struct _cffi_field_s *fields; const struct _cffi_struct_union_s *struct_unions; const struct _cffi_enum_s *enums; const struct _cffi_typename_s *typenames; int num_globals; int num_struct_unions; int num_enums; int num_typenames; const char *const *includes; int num_types; int flags; /* future extension */ }; struct _cffi_parse_info_s { const struct _cffi_type_context_s *ctx; _cffi_opcode_t *output; unsigned int output_size; size_t error_location; const char *error_message; }; struct _cffi_externpy_s { const char *name; size_t size_of_result; void *reserved1, *reserved2; }; #ifdef _CFFI_INTERNAL static int parse_c_type(struct _cffi_parse_info_s *info, const char *input); static int search_in_globals(const struct _cffi_type_context_s *ctx, const char *search, size_t search_len); static int search_in_struct_unions(const struct _cffi_type_context_s *ctx, const char *search, size_t search_len); #endif vengine_cpy.py000064400000120776152344566040007452 0ustar00# # DEPRECATED: implementation for ffi.verify() # import sys, imp from . import model from .error import VerificationError class VCPythonEngine(object): _class_key = 'x' _gen_python_module = True def __init__(self, verifier): self.verifier = verifier self.ffi = verifier.ffi self._struct_pending_verification = {} self._types_of_builtin_functions = {} def patch_extension_kwds(self, kwds): pass def find_module(self, module_name, path, so_suffixes): try: f, filename, descr = imp.find_module(module_name, path) except ImportError: return None if f is not None: f.close() # Note that after a setuptools installation, there are both .py # and .so files with the same basename. The code here relies on # imp.find_module() locating the .so in priority. if descr[0] not in so_suffixes: return None return filename def collect_types(self): self._typesdict = {} self._generate("collecttype") def _prnt(self, what=''): self._f.write(what + '\n') def _gettypenum(self, type): # a KeyError here is a bug. please report it! :-) return self._typesdict[type] def _do_collect_type(self, tp): if ((not isinstance(tp, model.PrimitiveType) or tp.name == 'long double') and tp not in self._typesdict): num = len(self._typesdict) self._typesdict[tp] = num def write_source_to_f(self): self.collect_types() # # The new module will have a _cffi_setup() function that receives # objects from the ffi world, and that calls some setup code in # the module. This setup code is split in several independent # functions, e.g. one per constant. The functions are "chained" # by ending in a tail call to each other. # # This is further split in two chained lists, depending on if we # can do it at import-time or if we must wait for _cffi_setup() to # provide us with the objects. This is needed because we # need the values of the enum constants in order to build the # that we may have to pass to _cffi_setup(). # # The following two 'chained_list_constants' items contains # the head of these two chained lists, as a string that gives the # call to do, if any. self._chained_list_constants = ['((void)lib,0)', '((void)lib,0)'] # prnt = self._prnt # first paste some standard set of lines that are mostly '#define' prnt(cffimod_header) prnt() # then paste the C source given by the user, verbatim. prnt(self.verifier.preamble) prnt() # # call generate_cpy_xxx_decl(), for every xxx found from # ffi._parser._declarations. This generates all the functions. self._generate("decl") # # implement the function _cffi_setup_custom() as calling the # head of the chained list. self._generate_setup_custom() prnt() # # produce the method table, including the entries for the # generated Python->C function wrappers, which are done # by generate_cpy_function_method(). prnt('static PyMethodDef _cffi_methods[] = {') self._generate("method") prnt(' {"_cffi_setup", _cffi_setup, METH_VARARGS, NULL},') prnt(' {NULL, NULL, 0, NULL} /* Sentinel */') prnt('};') prnt() # # standard init. modname = self.verifier.get_module_name() constants = self._chained_list_constants[False] prnt('#if PY_MAJOR_VERSION >= 3') prnt() prnt('static struct PyModuleDef _cffi_module_def = {') prnt(' PyModuleDef_HEAD_INIT,') prnt(' "%s",' % modname) prnt(' NULL,') prnt(' -1,') prnt(' _cffi_methods,') prnt(' NULL, NULL, NULL, NULL') prnt('};') prnt() prnt('PyMODINIT_FUNC') prnt('PyInit_%s(void)' % modname) prnt('{') prnt(' PyObject *lib;') prnt(' lib = PyModule_Create(&_cffi_module_def);') prnt(' if (lib == NULL)') prnt(' return NULL;') prnt(' if (%s < 0 || _cffi_init() < 0) {' % (constants,)) prnt(' Py_DECREF(lib);') prnt(' return NULL;') prnt(' }') prnt(' return lib;') prnt('}') prnt() prnt('#else') prnt() prnt('PyMODINIT_FUNC') prnt('init%s(void)' % modname) prnt('{') prnt(' PyObject *lib;') prnt(' lib = Py_InitModule("%s", _cffi_methods);' % modname) prnt(' if (lib == NULL)') prnt(' return;') prnt(' if (%s < 0 || _cffi_init() < 0)' % (constants,)) prnt(' return;') prnt(' return;') prnt('}') prnt() prnt('#endif') def load_library(self, flags=None): # XXX review all usages of 'self' here! # import it as a new extension module imp.acquire_lock() try: if hasattr(sys, "getdlopenflags"): previous_flags = sys.getdlopenflags() try: if hasattr(sys, "setdlopenflags") and flags is not None: sys.setdlopenflags(flags) module = imp.load_dynamic(self.verifier.get_module_name(), self.verifier.modulefilename) except ImportError as e: error = "importing %r: %s" % (self.verifier.modulefilename, e) raise VerificationError(error) finally: if hasattr(sys, "setdlopenflags"): sys.setdlopenflags(previous_flags) finally: imp.release_lock() # # call loading_cpy_struct() to get the struct layout inferred by # the C compiler self._load(module, 'loading') # # the C code will need the objects. Collect them in # order in a list. revmapping = dict([(value, key) for (key, value) in self._typesdict.items()]) lst = [revmapping[i] for i in range(len(revmapping))] lst = list(map(self.ffi._get_cached_btype, lst)) # # build the FFILibrary class and instance and call _cffi_setup(). # this will set up some fields like '_cffi_types', and only then # it will invoke the chained list of functions that will really # build (notably) the constant objects, as if they are # pointers, and store them as attributes on the 'library' object. class FFILibrary(object): _cffi_python_module = module _cffi_ffi = self.ffi _cffi_dir = [] def __dir__(self): return FFILibrary._cffi_dir + list(self.__dict__) library = FFILibrary() if module._cffi_setup(lst, VerificationError, library): import warnings warnings.warn("reimporting %r might overwrite older definitions" % (self.verifier.get_module_name())) # # finally, call the loaded_cpy_xxx() functions. This will perform # the final adjustments, like copying the Python->C wrapper # functions from the module to the 'library' object, and setting # up the FFILibrary class with properties for the global C variables. self._load(module, 'loaded', library=library) module._cffi_original_ffi = self.ffi module._cffi_types_of_builtin_funcs = self._types_of_builtin_functions return library def _get_declarations(self): lst = [(key, tp) for (key, (tp, qual)) in self.ffi._parser._declarations.items()] lst.sort() return lst def _generate(self, step_name): for name, tp in self._get_declarations(): kind, realname = name.split(' ', 1) try: method = getattr(self, '_generate_cpy_%s_%s' % (kind, step_name)) except AttributeError: raise VerificationError( "not implemented in verify(): %r" % name) try: method(tp, realname) except Exception as e: model.attach_exception_info(e, name) raise def _load(self, module, step_name, **kwds): for name, tp in self._get_declarations(): kind, realname = name.split(' ', 1) method = getattr(self, '_%s_cpy_%s' % (step_name, kind)) try: method(tp, realname, module, **kwds) except Exception as e: model.attach_exception_info(e, name) raise def _generate_nothing(self, tp, name): pass def _loaded_noop(self, tp, name, module, **kwds): pass # ---------- def _convert_funcarg_to_c(self, tp, fromvar, tovar, errcode): extraarg = '' if isinstance(tp, model.PrimitiveType): if tp.is_integer_type() and tp.name != '_Bool': converter = '_cffi_to_c_int' extraarg = ', %s' % tp.name else: converter = '(%s)_cffi_to_c_%s' % (tp.get_c_name(''), tp.name.replace(' ', '_')) errvalue = '-1' # elif isinstance(tp, model.PointerType): self._convert_funcarg_to_c_ptr_or_array(tp, fromvar, tovar, errcode) return # elif isinstance(tp, (model.StructOrUnion, model.EnumType)): # a struct (not a struct pointer) as a function argument self._prnt(' if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)' % (tovar, self._gettypenum(tp), fromvar)) self._prnt(' %s;' % errcode) return # elif isinstance(tp, model.FunctionPtrType): converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('') extraarg = ', _cffi_type(%d)' % self._gettypenum(tp) errvalue = 'NULL' # else: raise NotImplementedError(tp) # self._prnt(' %s = %s(%s%s);' % (tovar, converter, fromvar, extraarg)) self._prnt(' if (%s == (%s)%s && PyErr_Occurred())' % ( tovar, tp.get_c_name(''), errvalue)) self._prnt(' %s;' % errcode) def _extra_local_variables(self, tp, localvars): if isinstance(tp, model.PointerType): localvars.add('Py_ssize_t datasize') def _convert_funcarg_to_c_ptr_or_array(self, tp, fromvar, tovar, errcode): self._prnt(' datasize = _cffi_prepare_pointer_call_argument(') self._prnt(' _cffi_type(%d), %s, (char **)&%s);' % ( self._gettypenum(tp), fromvar, tovar)) self._prnt(' if (datasize != 0) {') self._prnt(' if (datasize < 0)') self._prnt(' %s;' % errcode) self._prnt(' %s = alloca((size_t)datasize);' % (tovar,)) self._prnt(' memset((void *)%s, 0, (size_t)datasize);' % (tovar,)) self._prnt(' if (_cffi_convert_array_from_object(' '(char *)%s, _cffi_type(%d), %s) < 0)' % ( tovar, self._gettypenum(tp), fromvar)) self._prnt(' %s;' % errcode) self._prnt(' }') def _convert_expr_from_c(self, tp, var, context): if isinstance(tp, model.PrimitiveType): if tp.is_integer_type() and tp.name != '_Bool': return '_cffi_from_c_int(%s, %s)' % (var, tp.name) elif tp.name != 'long double': return '_cffi_from_c_%s(%s)' % (tp.name.replace(' ', '_'), var) else: return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) elif isinstance(tp, (model.PointerType, model.FunctionPtrType)): return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) elif isinstance(tp, model.ArrayType): return '_cffi_from_c_pointer((char *)%s, _cffi_type(%d))' % ( var, self._gettypenum(model.PointerType(tp.item))) elif isinstance(tp, model.StructOrUnion): if tp.fldnames is None: raise TypeError("'%s' is used as %s, but is opaque" % ( tp._get_c_name(), context)) return '_cffi_from_c_struct((char *)&%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) elif isinstance(tp, model.EnumType): return '_cffi_from_c_deref((char *)&%s, _cffi_type(%d))' % ( var, self._gettypenum(tp)) else: raise NotImplementedError(tp) # ---------- # typedefs: generates no code so far _generate_cpy_typedef_collecttype = _generate_nothing _generate_cpy_typedef_decl = _generate_nothing _generate_cpy_typedef_method = _generate_nothing _loading_cpy_typedef = _loaded_noop _loaded_cpy_typedef = _loaded_noop # ---------- # function declarations def _generate_cpy_function_collecttype(self, tp, name): assert isinstance(tp, model.FunctionPtrType) if tp.ellipsis: self._do_collect_type(tp) else: # don't call _do_collect_type(tp) in this common case, # otherwise test_autofilled_struct_as_argument fails for type in tp.args: self._do_collect_type(type) self._do_collect_type(tp.result) def _generate_cpy_function_decl(self, tp, name): assert isinstance(tp, model.FunctionPtrType) if tp.ellipsis: # cannot support vararg functions better than this: check for its # exact type (including the fixed arguments), and build it as a # constant function pointer (no CPython wrapper) self._generate_cpy_const(False, name, tp) return prnt = self._prnt numargs = len(tp.args) if numargs == 0: argname = 'noarg' elif numargs == 1: argname = 'arg0' else: argname = 'args' prnt('static PyObject *') prnt('_cffi_f_%s(PyObject *self, PyObject *%s)' % (name, argname)) prnt('{') # context = 'argument of %s' % name for i, type in enumerate(tp.args): prnt(' %s;' % type.get_c_name(' x%d' % i, context)) # localvars = set() for type in tp.args: self._extra_local_variables(type, localvars) for decl in localvars: prnt(' %s;' % (decl,)) # if not isinstance(tp.result, model.VoidType): result_code = 'result = ' context = 'result of %s' % name prnt(' %s;' % tp.result.get_c_name(' result', context)) else: result_code = '' # if len(tp.args) > 1: rng = range(len(tp.args)) for i in rng: prnt(' PyObject *arg%d;' % i) prnt() prnt(' if (!PyArg_ParseTuple(args, "%s:%s", %s))' % ( 'O' * numargs, name, ', '.join(['&arg%d' % i for i in rng]))) prnt(' return NULL;') prnt() # for i, type in enumerate(tp.args): self._convert_funcarg_to_c(type, 'arg%d' % i, 'x%d' % i, 'return NULL') prnt() # prnt(' Py_BEGIN_ALLOW_THREADS') prnt(' _cffi_restore_errno();') prnt(' { %s%s(%s); }' % ( result_code, name, ', '.join(['x%d' % i for i in range(len(tp.args))]))) prnt(' _cffi_save_errno();') prnt(' Py_END_ALLOW_THREADS') prnt() # prnt(' (void)self; /* unused */') if numargs == 0: prnt(' (void)noarg; /* unused */') if result_code: prnt(' return %s;' % self._convert_expr_from_c(tp.result, 'result', 'result type')) else: prnt(' Py_INCREF(Py_None);') prnt(' return Py_None;') prnt('}') prnt() def _generate_cpy_function_method(self, tp, name): if tp.ellipsis: return numargs = len(tp.args) if numargs == 0: meth = 'METH_NOARGS' elif numargs == 1: meth = 'METH_O' else: meth = 'METH_VARARGS' self._prnt(' {"%s", _cffi_f_%s, %s, NULL},' % (name, name, meth)) _loading_cpy_function = _loaded_noop def _loaded_cpy_function(self, tp, name, module, library): if tp.ellipsis: return func = getattr(module, name) setattr(library, name, func) self._types_of_builtin_functions[func] = tp # ---------- # named structs _generate_cpy_struct_collecttype = _generate_nothing def _generate_cpy_struct_decl(self, tp, name): assert name == tp.name self._generate_struct_or_union_decl(tp, 'struct', name) def _generate_cpy_struct_method(self, tp, name): self._generate_struct_or_union_method(tp, 'struct', name) def _loading_cpy_struct(self, tp, name, module): self._loading_struct_or_union(tp, 'struct', name, module) def _loaded_cpy_struct(self, tp, name, module, **kwds): self._loaded_struct_or_union(tp) _generate_cpy_union_collecttype = _generate_nothing def _generate_cpy_union_decl(self, tp, name): assert name == tp.name self._generate_struct_or_union_decl(tp, 'union', name) def _generate_cpy_union_method(self, tp, name): self._generate_struct_or_union_method(tp, 'union', name) def _loading_cpy_union(self, tp, name, module): self._loading_struct_or_union(tp, 'union', name, module) def _loaded_cpy_union(self, tp, name, module, **kwds): self._loaded_struct_or_union(tp) def _generate_struct_or_union_decl(self, tp, prefix, name): if tp.fldnames is None: return # nothing to do with opaque structs checkfuncname = '_cffi_check_%s_%s' % (prefix, name) layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) cname = ('%s %s' % (prefix, name)).strip() # prnt = self._prnt prnt('static void %s(%s *p)' % (checkfuncname, cname)) prnt('{') prnt(' /* only to generate compile-time warnings or errors */') prnt(' (void)p;') for fname, ftype, fbitsize, fqual in tp.enumfields(): if (isinstance(ftype, model.PrimitiveType) and ftype.is_integer_type()) or fbitsize >= 0: # accept all integers, but complain on float or double prnt(' (void)((p->%s) << 1);' % fname) else: # only accept exactly the type declared. try: prnt(' { %s = &p->%s; (void)tmp; }' % ( ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual), fname)) except VerificationError as e: prnt(' /* %s */' % str(e)) # cannot verify it, ignore prnt('}') prnt('static PyObject *') prnt('%s(PyObject *self, PyObject *noarg)' % (layoutfuncname,)) prnt('{') prnt(' struct _cffi_aligncheck { char x; %s y; };' % cname) prnt(' static Py_ssize_t nums[] = {') prnt(' sizeof(%s),' % cname) prnt(' offsetof(struct _cffi_aligncheck, y),') for fname, ftype, fbitsize, fqual in tp.enumfields(): if fbitsize >= 0: continue # xxx ignore fbitsize for now prnt(' offsetof(%s, %s),' % (cname, fname)) if isinstance(ftype, model.ArrayType) and ftype.length is None: prnt(' 0, /* %s */' % ftype._get_c_name()) else: prnt(' sizeof(((%s *)0)->%s),' % (cname, fname)) prnt(' -1') prnt(' };') prnt(' (void)self; /* unused */') prnt(' (void)noarg; /* unused */') prnt(' return _cffi_get_struct_layout(nums);') prnt(' /* the next line is not executed, but compiled */') prnt(' %s(0);' % (checkfuncname,)) prnt('}') prnt() def _generate_struct_or_union_method(self, tp, prefix, name): if tp.fldnames is None: return # nothing to do with opaque structs layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) self._prnt(' {"%s", %s, METH_NOARGS, NULL},' % (layoutfuncname, layoutfuncname)) def _loading_struct_or_union(self, tp, prefix, name, module): if tp.fldnames is None: return # nothing to do with opaque structs layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) # function = getattr(module, layoutfuncname) layout = function() if isinstance(tp, model.StructOrUnion) and tp.partial: # use the function()'s sizes and offsets to guide the # layout of the struct totalsize = layout[0] totalalignment = layout[1] fieldofs = layout[2::2] fieldsize = layout[3::2] tp.force_flatten() assert len(fieldofs) == len(fieldsize) == len(tp.fldnames) tp.fixedlayout = fieldofs, fieldsize, totalsize, totalalignment else: cname = ('%s %s' % (prefix, name)).strip() self._struct_pending_verification[tp] = layout, cname def _loaded_struct_or_union(self, tp): if tp.fldnames is None: return # nothing to do with opaque structs self.ffi._get_cached_btype(tp) # force 'fixedlayout' to be considered if tp in self._struct_pending_verification: # check that the layout sizes and offsets match the real ones def check(realvalue, expectedvalue, msg): if realvalue != expectedvalue: raise VerificationError( "%s (we have %d, but C compiler says %d)" % (msg, expectedvalue, realvalue)) ffi = self.ffi BStruct = ffi._get_cached_btype(tp) layout, cname = self._struct_pending_verification.pop(tp) check(layout[0], ffi.sizeof(BStruct), "wrong total size") check(layout[1], ffi.alignof(BStruct), "wrong total alignment") i = 2 for fname, ftype, fbitsize, fqual in tp.enumfields(): if fbitsize >= 0: continue # xxx ignore fbitsize for now check(layout[i], ffi.offsetof(BStruct, fname), "wrong offset for field %r" % (fname,)) if layout[i+1] != 0: BField = ffi._get_cached_btype(ftype) check(layout[i+1], ffi.sizeof(BField), "wrong size for field %r" % (fname,)) i += 2 assert i == len(layout) # ---------- # 'anonymous' declarations. These are produced for anonymous structs # or unions; the 'name' is obtained by a typedef. _generate_cpy_anonymous_collecttype = _generate_nothing def _generate_cpy_anonymous_decl(self, tp, name): if isinstance(tp, model.EnumType): self._generate_cpy_enum_decl(tp, name, '') else: self._generate_struct_or_union_decl(tp, '', name) def _generate_cpy_anonymous_method(self, tp, name): if not isinstance(tp, model.EnumType): self._generate_struct_or_union_method(tp, '', name) def _loading_cpy_anonymous(self, tp, name, module): if isinstance(tp, model.EnumType): self._loading_cpy_enum(tp, name, module) else: self._loading_struct_or_union(tp, '', name, module) def _loaded_cpy_anonymous(self, tp, name, module, **kwds): if isinstance(tp, model.EnumType): self._loaded_cpy_enum(tp, name, module, **kwds) else: self._loaded_struct_or_union(tp) # ---------- # constants, likely declared with '#define' def _generate_cpy_const(self, is_int, name, tp=None, category='const', vartp=None, delayed=True, size_too=False, check_value=None): prnt = self._prnt funcname = '_cffi_%s_%s' % (category, name) prnt('static int %s(PyObject *lib)' % funcname) prnt('{') prnt(' PyObject *o;') prnt(' int res;') if not is_int: prnt(' %s;' % (vartp or tp).get_c_name(' i', name)) else: assert category == 'const' # if check_value is not None: self._check_int_constant_value(name, check_value) # if not is_int: if category == 'var': realexpr = '&' + name else: realexpr = name prnt(' i = (%s);' % (realexpr,)) prnt(' o = %s;' % (self._convert_expr_from_c(tp, 'i', 'variable type'),)) assert delayed else: prnt(' o = _cffi_from_c_int_const(%s);' % name) prnt(' if (o == NULL)') prnt(' return -1;') if size_too: prnt(' {') prnt(' PyObject *o1 = o;') prnt(' o = Py_BuildValue("On", o1, (Py_ssize_t)sizeof(%s));' % (name,)) prnt(' Py_DECREF(o1);') prnt(' if (o == NULL)') prnt(' return -1;') prnt(' }') prnt(' res = PyObject_SetAttrString(lib, "%s", o);' % name) prnt(' Py_DECREF(o);') prnt(' if (res < 0)') prnt(' return -1;') prnt(' return %s;' % self._chained_list_constants[delayed]) self._chained_list_constants[delayed] = funcname + '(lib)' prnt('}') prnt() def _generate_cpy_constant_collecttype(self, tp, name): is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type() if not is_int: self._do_collect_type(tp) def _generate_cpy_constant_decl(self, tp, name): is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type() self._generate_cpy_const(is_int, name, tp) _generate_cpy_constant_method = _generate_nothing _loading_cpy_constant = _loaded_noop _loaded_cpy_constant = _loaded_noop # ---------- # enums def _check_int_constant_value(self, name, value, err_prefix=''): prnt = self._prnt if value <= 0: prnt(' if ((%s) > 0 || (long)(%s) != %dL) {' % ( name, name, value)) else: prnt(' if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {' % ( name, name, value)) prnt(' char buf[64];') prnt(' if ((%s) <= 0)' % name) prnt(' snprintf(buf, 63, "%%ld", (long)(%s));' % name) prnt(' else') prnt(' snprintf(buf, 63, "%%lu", (unsigned long)(%s));' % name) prnt(' PyErr_Format(_cffi_VerificationError,') prnt(' "%s%s has the real value %s, not %s",') prnt(' "%s", "%s", buf, "%d");' % ( err_prefix, name, value)) prnt(' return -1;') prnt(' }') def _enum_funcname(self, prefix, name): # "$enum_$1" => "___D_enum____D_1" name = name.replace('$', '___D_') return '_cffi_e_%s_%s' % (prefix, name) def _generate_cpy_enum_decl(self, tp, name, prefix='enum'): if tp.partial: for enumerator in tp.enumerators: self._generate_cpy_const(True, enumerator, delayed=False) return # funcname = self._enum_funcname(prefix, name) prnt = self._prnt prnt('static int %s(PyObject *lib)' % funcname) prnt('{') for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues): self._check_int_constant_value(enumerator, enumvalue, "enum %s: " % name) prnt(' return %s;' % self._chained_list_constants[True]) self._chained_list_constants[True] = funcname + '(lib)' prnt('}') prnt() _generate_cpy_enum_collecttype = _generate_nothing _generate_cpy_enum_method = _generate_nothing def _loading_cpy_enum(self, tp, name, module): if tp.partial: enumvalues = [getattr(module, enumerator) for enumerator in tp.enumerators] tp.enumvalues = tuple(enumvalues) tp.partial_resolved = True def _loaded_cpy_enum(self, tp, name, module, library): for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues): setattr(library, enumerator, enumvalue) # ---------- # macros: for now only for integers def _generate_cpy_macro_decl(self, tp, name): if tp == '...': check_value = None else: check_value = tp # an integer self._generate_cpy_const(True, name, check_value=check_value) _generate_cpy_macro_collecttype = _generate_nothing _generate_cpy_macro_method = _generate_nothing _loading_cpy_macro = _loaded_noop _loaded_cpy_macro = _loaded_noop # ---------- # global variables def _generate_cpy_variable_collecttype(self, tp, name): if isinstance(tp, model.ArrayType): tp_ptr = model.PointerType(tp.item) else: tp_ptr = model.PointerType(tp) self._do_collect_type(tp_ptr) def _generate_cpy_variable_decl(self, tp, name): if isinstance(tp, model.ArrayType): tp_ptr = model.PointerType(tp.item) self._generate_cpy_const(False, name, tp, vartp=tp_ptr, size_too = (tp.length == '...')) else: tp_ptr = model.PointerType(tp) self._generate_cpy_const(False, name, tp_ptr, category='var') _generate_cpy_variable_method = _generate_nothing _loading_cpy_variable = _loaded_noop def _loaded_cpy_variable(self, tp, name, module, library): value = getattr(library, name) if isinstance(tp, model.ArrayType): # int a[5] is "constant" in the # sense that "a=..." is forbidden if tp.length == '...': assert isinstance(value, tuple) (value, size) = value BItemType = self.ffi._get_cached_btype(tp.item) length, rest = divmod(size, self.ffi.sizeof(BItemType)) if rest != 0: raise VerificationError( "bad size: %r does not seem to be an array of %s" % (name, tp.item)) tp = tp.resolve_length(length) # 'value' is a which we have to replace with # a if the N is actually known if tp.length is not None: BArray = self.ffi._get_cached_btype(tp) value = self.ffi.cast(BArray, value) setattr(library, name, value) return # remove ptr= from the library instance, and replace # it by a property on the class, which reads/writes into ptr[0]. ptr = value delattr(library, name) def getter(library): return ptr[0] def setter(library, value): ptr[0] = value setattr(type(library), name, property(getter, setter)) type(library)._cffi_dir.append(name) # ---------- def _generate_setup_custom(self): prnt = self._prnt prnt('static int _cffi_setup_custom(PyObject *lib)') prnt('{') prnt(' return %s;' % self._chained_list_constants[True]) prnt('}') cffimod_header = r''' #include #include /* this block of #ifs should be kept exactly identical between c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py and cffi/_cffi_include.h */ #if defined(_MSC_VER) # include /* for alloca() */ # if _MSC_VER < 1600 /* MSVC < 2010 */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; typedef __int8 int_least8_t; typedef __int16 int_least16_t; typedef __int32 int_least32_t; typedef __int64 int_least64_t; typedef unsigned __int8 uint_least8_t; typedef unsigned __int16 uint_least16_t; typedef unsigned __int32 uint_least32_t; typedef unsigned __int64 uint_least64_t; typedef __int8 int_fast8_t; typedef __int16 int_fast16_t; typedef __int32 int_fast32_t; typedef __int64 int_fast64_t; typedef unsigned __int8 uint_fast8_t; typedef unsigned __int16 uint_fast16_t; typedef unsigned __int32 uint_fast32_t; typedef unsigned __int64 uint_fast64_t; typedef __int64 intmax_t; typedef unsigned __int64 uintmax_t; # else # include # endif # if _MSC_VER < 1800 /* MSVC < 2013 */ # ifndef __cplusplus typedef unsigned char _Bool; # endif # endif #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif #endif #if PY_MAJOR_VERSION < 3 # undef PyCapsule_CheckExact # undef PyCapsule_GetPointer # define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule)) # define PyCapsule_GetPointer(capsule, name) \ (PyCObject_AsVoidPtr(capsule)) #endif #if PY_MAJOR_VERSION >= 3 # define PyInt_FromLong PyLong_FromLong #endif #define _cffi_from_c_double PyFloat_FromDouble #define _cffi_from_c_float PyFloat_FromDouble #define _cffi_from_c_long PyInt_FromLong #define _cffi_from_c_ulong PyLong_FromUnsignedLong #define _cffi_from_c_longlong PyLong_FromLongLong #define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong #define _cffi_from_c__Bool PyBool_FromLong #define _cffi_to_c_double PyFloat_AsDouble #define _cffi_to_c_float PyFloat_AsDouble #define _cffi_from_c_int_const(x) \ (((x) > 0) ? \ ((unsigned long long)(x) <= (unsigned long long)LONG_MAX) ? \ PyInt_FromLong((long)(x)) : \ PyLong_FromUnsignedLongLong((unsigned long long)(x)) : \ ((long long)(x) >= (long long)LONG_MIN) ? \ PyInt_FromLong((long)(x)) : \ PyLong_FromLongLong((long long)(x))) #define _cffi_from_c_int(x, type) \ (((type)-1) > 0 ? /* unsigned */ \ (sizeof(type) < sizeof(long) ? \ PyInt_FromLong((long)x) : \ sizeof(type) == sizeof(long) ? \ PyLong_FromUnsignedLong((unsigned long)x) : \ PyLong_FromUnsignedLongLong((unsigned long long)x)) : \ (sizeof(type) <= sizeof(long) ? \ PyInt_FromLong((long)x) : \ PyLong_FromLongLong((long long)x))) #define _cffi_to_c_int(o, type) \ ((type)( \ sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \ : (type)_cffi_to_c_i8(o)) : \ sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \ : (type)_cffi_to_c_i16(o)) : \ sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \ : (type)_cffi_to_c_i32(o)) : \ sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \ : (type)_cffi_to_c_i64(o)) : \ (Py_FatalError("unsupported size for type " #type), (type)0))) #define _cffi_to_c_i8 \ ((int(*)(PyObject *))_cffi_exports[1]) #define _cffi_to_c_u8 \ ((int(*)(PyObject *))_cffi_exports[2]) #define _cffi_to_c_i16 \ ((int(*)(PyObject *))_cffi_exports[3]) #define _cffi_to_c_u16 \ ((int(*)(PyObject *))_cffi_exports[4]) #define _cffi_to_c_i32 \ ((int(*)(PyObject *))_cffi_exports[5]) #define _cffi_to_c_u32 \ ((unsigned int(*)(PyObject *))_cffi_exports[6]) #define _cffi_to_c_i64 \ ((long long(*)(PyObject *))_cffi_exports[7]) #define _cffi_to_c_u64 \ ((unsigned long long(*)(PyObject *))_cffi_exports[8]) #define _cffi_to_c_char \ ((int(*)(PyObject *))_cffi_exports[9]) #define _cffi_from_c_pointer \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[10]) #define _cffi_to_c_pointer \ ((char *(*)(PyObject *, CTypeDescrObject *))_cffi_exports[11]) #define _cffi_get_struct_layout \ ((PyObject *(*)(Py_ssize_t[]))_cffi_exports[12]) #define _cffi_restore_errno \ ((void(*)(void))_cffi_exports[13]) #define _cffi_save_errno \ ((void(*)(void))_cffi_exports[14]) #define _cffi_from_c_char \ ((PyObject *(*)(char))_cffi_exports[15]) #define _cffi_from_c_deref \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[16]) #define _cffi_to_c \ ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[17]) #define _cffi_from_c_struct \ ((PyObject *(*)(char *, CTypeDescrObject *))_cffi_exports[18]) #define _cffi_to_c_wchar_t \ ((wchar_t(*)(PyObject *))_cffi_exports[19]) #define _cffi_from_c_wchar_t \ ((PyObject *(*)(wchar_t))_cffi_exports[20]) #define _cffi_to_c_long_double \ ((long double(*)(PyObject *))_cffi_exports[21]) #define _cffi_to_c__Bool \ ((_Bool(*)(PyObject *))_cffi_exports[22]) #define _cffi_prepare_pointer_call_argument \ ((Py_ssize_t(*)(CTypeDescrObject *, PyObject *, char **))_cffi_exports[23]) #define _cffi_convert_array_from_object \ ((int(*)(char *, CTypeDescrObject *, PyObject *))_cffi_exports[24]) #define _CFFI_NUM_EXPORTS 25 typedef struct _ctypedescr CTypeDescrObject; static void *_cffi_exports[_CFFI_NUM_EXPORTS]; static PyObject *_cffi_types, *_cffi_VerificationError; static int _cffi_setup_custom(PyObject *lib); /* forward */ static PyObject *_cffi_setup(PyObject *self, PyObject *args) { PyObject *library; int was_alive = (_cffi_types != NULL); (void)self; /* unused */ if (!PyArg_ParseTuple(args, "OOO", &_cffi_types, &_cffi_VerificationError, &library)) return NULL; Py_INCREF(_cffi_types); Py_INCREF(_cffi_VerificationError); if (_cffi_setup_custom(library) < 0) return NULL; return PyBool_FromLong(was_alive); } static int _cffi_init(void) { PyObject *module, *c_api_object = NULL; module = PyImport_ImportModule("_cffi_backend"); if (module == NULL) goto failure; c_api_object = PyObject_GetAttrString(module, "_C_API"); if (c_api_object == NULL) goto failure; if (!PyCapsule_CheckExact(c_api_object)) { PyErr_SetNone(PyExc_ImportError); goto failure; } memcpy(_cffi_exports, PyCapsule_GetPointer(c_api_object, "cffi"), _CFFI_NUM_EXPORTS * sizeof(void *)); Py_DECREF(module); Py_DECREF(c_api_object); return 0; failure: Py_XDECREF(module); Py_XDECREF(c_api_object); return -1; } #define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num)) /**********/ ''' vengine_gen.py000064400000064064152344566040007425 0ustar00# # DEPRECATED: implementation for ffi.verify() # import sys, os import types from . import model from .error import VerificationError class VGenericEngine(object): _class_key = 'g' _gen_python_module = False def __init__(self, verifier): self.verifier = verifier self.ffi = verifier.ffi self.export_symbols = [] self._struct_pending_verification = {} def patch_extension_kwds(self, kwds): # add 'export_symbols' to the dictionary. Note that we add the # list before filling it. When we fill it, it will thus also show # up in kwds['export_symbols']. kwds.setdefault('export_symbols', self.export_symbols) def find_module(self, module_name, path, so_suffixes): for so_suffix in so_suffixes: basename = module_name + so_suffix if path is None: path = sys.path for dirname in path: filename = os.path.join(dirname, basename) if os.path.isfile(filename): return filename def collect_types(self): pass # not needed in the generic engine def _prnt(self, what=''): self._f.write(what + '\n') def write_source_to_f(self): prnt = self._prnt # first paste some standard set of lines that are mostly '#include' prnt(cffimod_header) # then paste the C source given by the user, verbatim. prnt(self.verifier.preamble) # # call generate_gen_xxx_decl(), for every xxx found from # ffi._parser._declarations. This generates all the functions. self._generate('decl') # # on Windows, distutils insists on putting init_cffi_xyz in # 'export_symbols', so instead of fighting it, just give up and # give it one if sys.platform == 'win32': if sys.version_info >= (3,): prefix = 'PyInit_' else: prefix = 'init' modname = self.verifier.get_module_name() prnt("void %s%s(void) { }\n" % (prefix, modname)) def load_library(self, flags=0): # import it with the CFFI backend backend = self.ffi._backend # needs to make a path that contains '/', on Posix filename = os.path.join(os.curdir, self.verifier.modulefilename) module = backend.load_library(filename, flags) # # call loading_gen_struct() to get the struct layout inferred by # the C compiler self._load(module, 'loading') # build the FFILibrary class and instance, this is a module subclass # because modules are expected to have usually-constant-attributes and # in PyPy this means the JIT is able to treat attributes as constant, # which we want. class FFILibrary(types.ModuleType): _cffi_generic_module = module _cffi_ffi = self.ffi _cffi_dir = [] def __dir__(self): return FFILibrary._cffi_dir library = FFILibrary("") # # finally, call the loaded_gen_xxx() functions. This will set # up the 'library' object. self._load(module, 'loaded', library=library) return library def _get_declarations(self): lst = [(key, tp) for (key, (tp, qual)) in self.ffi._parser._declarations.items()] lst.sort() return lst def _generate(self, step_name): for name, tp in self._get_declarations(): kind, realname = name.split(' ', 1) try: method = getattr(self, '_generate_gen_%s_%s' % (kind, step_name)) except AttributeError: raise VerificationError( "not implemented in verify(): %r" % name) try: method(tp, realname) except Exception as e: model.attach_exception_info(e, name) raise def _load(self, module, step_name, **kwds): for name, tp in self._get_declarations(): kind, realname = name.split(' ', 1) method = getattr(self, '_%s_gen_%s' % (step_name, kind)) try: method(tp, realname, module, **kwds) except Exception as e: model.attach_exception_info(e, name) raise def _generate_nothing(self, tp, name): pass def _loaded_noop(self, tp, name, module, **kwds): pass # ---------- # typedefs: generates no code so far _generate_gen_typedef_decl = _generate_nothing _loading_gen_typedef = _loaded_noop _loaded_gen_typedef = _loaded_noop # ---------- # function declarations def _generate_gen_function_decl(self, tp, name): assert isinstance(tp, model.FunctionPtrType) if tp.ellipsis: # cannot support vararg functions better than this: check for its # exact type (including the fixed arguments), and build it as a # constant function pointer (no _cffi_f_%s wrapper) self._generate_gen_const(False, name, tp) return prnt = self._prnt numargs = len(tp.args) argnames = [] for i, type in enumerate(tp.args): indirection = '' if isinstance(type, model.StructOrUnion): indirection = '*' argnames.append('%sx%d' % (indirection, i)) context = 'argument of %s' % name arglist = [type.get_c_name(' %s' % arg, context) for type, arg in zip(tp.args, argnames)] tpresult = tp.result if isinstance(tpresult, model.StructOrUnion): arglist.insert(0, tpresult.get_c_name(' *r', context)) tpresult = model.void_type arglist = ', '.join(arglist) or 'void' wrappername = '_cffi_f_%s' % name self.export_symbols.append(wrappername) if tp.abi: abi = tp.abi + ' ' else: abi = '' funcdecl = ' %s%s(%s)' % (abi, wrappername, arglist) context = 'result of %s' % name prnt(tpresult.get_c_name(funcdecl, context)) prnt('{') # if isinstance(tp.result, model.StructOrUnion): result_code = '*r = ' elif not isinstance(tp.result, model.VoidType): result_code = 'return ' else: result_code = '' prnt(' %s%s(%s);' % (result_code, name, ', '.join(argnames))) prnt('}') prnt() _loading_gen_function = _loaded_noop def _loaded_gen_function(self, tp, name, module, library): assert isinstance(tp, model.FunctionPtrType) if tp.ellipsis: newfunction = self._load_constant(False, tp, name, module) else: indirections = [] base_tp = tp if (any(isinstance(typ, model.StructOrUnion) for typ in tp.args) or isinstance(tp.result, model.StructOrUnion)): indirect_args = [] for i, typ in enumerate(tp.args): if isinstance(typ, model.StructOrUnion): typ = model.PointerType(typ) indirections.append((i, typ)) indirect_args.append(typ) indirect_result = tp.result if isinstance(indirect_result, model.StructOrUnion): if indirect_result.fldtypes is None: raise TypeError("'%s' is used as result type, " "but is opaque" % ( indirect_result._get_c_name(),)) indirect_result = model.PointerType(indirect_result) indirect_args.insert(0, indirect_result) indirections.insert(0, ("result", indirect_result)) indirect_result = model.void_type tp = model.FunctionPtrType(tuple(indirect_args), indirect_result, tp.ellipsis) BFunc = self.ffi._get_cached_btype(tp) wrappername = '_cffi_f_%s' % name newfunction = module.load_function(BFunc, wrappername) for i, typ in indirections: newfunction = self._make_struct_wrapper(newfunction, i, typ, base_tp) setattr(library, name, newfunction) type(library)._cffi_dir.append(name) def _make_struct_wrapper(self, oldfunc, i, tp, base_tp): backend = self.ffi._backend BType = self.ffi._get_cached_btype(tp) if i == "result": ffi = self.ffi def newfunc(*args): res = ffi.new(BType) oldfunc(res, *args) return res[0] else: def newfunc(*args): args = args[:i] + (backend.newp(BType, args[i]),) + args[i+1:] return oldfunc(*args) newfunc._cffi_base_type = base_tp return newfunc # ---------- # named structs def _generate_gen_struct_decl(self, tp, name): assert name == tp.name self._generate_struct_or_union_decl(tp, 'struct', name) def _loading_gen_struct(self, tp, name, module): self._loading_struct_or_union(tp, 'struct', name, module) def _loaded_gen_struct(self, tp, name, module, **kwds): self._loaded_struct_or_union(tp) def _generate_gen_union_decl(self, tp, name): assert name == tp.name self._generate_struct_or_union_decl(tp, 'union', name) def _loading_gen_union(self, tp, name, module): self._loading_struct_or_union(tp, 'union', name, module) def _loaded_gen_union(self, tp, name, module, **kwds): self._loaded_struct_or_union(tp) def _generate_struct_or_union_decl(self, tp, prefix, name): if tp.fldnames is None: return # nothing to do with opaque structs checkfuncname = '_cffi_check_%s_%s' % (prefix, name) layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) cname = ('%s %s' % (prefix, name)).strip() # prnt = self._prnt prnt('static void %s(%s *p)' % (checkfuncname, cname)) prnt('{') prnt(' /* only to generate compile-time warnings or errors */') prnt(' (void)p;') for fname, ftype, fbitsize, fqual in tp.enumfields(): if (isinstance(ftype, model.PrimitiveType) and ftype.is_integer_type()) or fbitsize >= 0: # accept all integers, but complain on float or double prnt(' (void)((p->%s) << 1);' % fname) else: # only accept exactly the type declared. try: prnt(' { %s = &p->%s; (void)tmp; }' % ( ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual), fname)) except VerificationError as e: prnt(' /* %s */' % str(e)) # cannot verify it, ignore prnt('}') self.export_symbols.append(layoutfuncname) prnt('intptr_t %s(intptr_t i)' % (layoutfuncname,)) prnt('{') prnt(' struct _cffi_aligncheck { char x; %s y; };' % cname) prnt(' static intptr_t nums[] = {') prnt(' sizeof(%s),' % cname) prnt(' offsetof(struct _cffi_aligncheck, y),') for fname, ftype, fbitsize, fqual in tp.enumfields(): if fbitsize >= 0: continue # xxx ignore fbitsize for now prnt(' offsetof(%s, %s),' % (cname, fname)) if isinstance(ftype, model.ArrayType) and ftype.length is None: prnt(' 0, /* %s */' % ftype._get_c_name()) else: prnt(' sizeof(((%s *)0)->%s),' % (cname, fname)) prnt(' -1') prnt(' };') prnt(' return nums[i];') prnt(' /* the next line is not executed, but compiled */') prnt(' %s(0);' % (checkfuncname,)) prnt('}') prnt() def _loading_struct_or_union(self, tp, prefix, name, module): if tp.fldnames is None: return # nothing to do with opaque structs layoutfuncname = '_cffi_layout_%s_%s' % (prefix, name) # BFunc = self.ffi._typeof_locked("intptr_t(*)(intptr_t)")[0] function = module.load_function(BFunc, layoutfuncname) layout = [] num = 0 while True: x = function(num) if x < 0: break layout.append(x) num += 1 if isinstance(tp, model.StructOrUnion) and tp.partial: # use the function()'s sizes and offsets to guide the # layout of the struct totalsize = layout[0] totalalignment = layout[1] fieldofs = layout[2::2] fieldsize = layout[3::2] tp.force_flatten() assert len(fieldofs) == len(fieldsize) == len(tp.fldnames) tp.fixedlayout = fieldofs, fieldsize, totalsize, totalalignment else: cname = ('%s %s' % (prefix, name)).strip() self._struct_pending_verification[tp] = layout, cname def _loaded_struct_or_union(self, tp): if tp.fldnames is None: return # nothing to do with opaque structs self.ffi._get_cached_btype(tp) # force 'fixedlayout' to be considered if tp in self._struct_pending_verification: # check that the layout sizes and offsets match the real ones def check(realvalue, expectedvalue, msg): if realvalue != expectedvalue: raise VerificationError( "%s (we have %d, but C compiler says %d)" % (msg, expectedvalue, realvalue)) ffi = self.ffi BStruct = ffi._get_cached_btype(tp) layout, cname = self._struct_pending_verification.pop(tp) check(layout[0], ffi.sizeof(BStruct), "wrong total size") check(layout[1], ffi.alignof(BStruct), "wrong total alignment") i = 2 for fname, ftype, fbitsize, fqual in tp.enumfields(): if fbitsize >= 0: continue # xxx ignore fbitsize for now check(layout[i], ffi.offsetof(BStruct, fname), "wrong offset for field %r" % (fname,)) if layout[i+1] != 0: BField = ffi._get_cached_btype(ftype) check(layout[i+1], ffi.sizeof(BField), "wrong size for field %r" % (fname,)) i += 2 assert i == len(layout) # ---------- # 'anonymous' declarations. These are produced for anonymous structs # or unions; the 'name' is obtained by a typedef. def _generate_gen_anonymous_decl(self, tp, name): if isinstance(tp, model.EnumType): self._generate_gen_enum_decl(tp, name, '') else: self._generate_struct_or_union_decl(tp, '', name) def _loading_gen_anonymous(self, tp, name, module): if isinstance(tp, model.EnumType): self._loading_gen_enum(tp, name, module, '') else: self._loading_struct_or_union(tp, '', name, module) def _loaded_gen_anonymous(self, tp, name, module, **kwds): if isinstance(tp, model.EnumType): self._loaded_gen_enum(tp, name, module, **kwds) else: self._loaded_struct_or_union(tp) # ---------- # constants, likely declared with '#define' def _generate_gen_const(self, is_int, name, tp=None, category='const', check_value=None): prnt = self._prnt funcname = '_cffi_%s_%s' % (category, name) self.export_symbols.append(funcname) if check_value is not None: assert is_int assert category == 'const' prnt('int %s(char *out_error)' % funcname) prnt('{') self._check_int_constant_value(name, check_value) prnt(' return 0;') prnt('}') elif is_int: assert category == 'const' prnt('int %s(long long *out_value)' % funcname) prnt('{') prnt(' *out_value = (long long)(%s);' % (name,)) prnt(' return (%s) <= 0;' % (name,)) prnt('}') else: assert tp is not None assert check_value is None if category == 'var': ampersand = '&' else: ampersand = '' extra = '' if category == 'const' and isinstance(tp, model.StructOrUnion): extra = 'const *' ampersand = '&' prnt(tp.get_c_name(' %s%s(void)' % (extra, funcname), name)) prnt('{') prnt(' return (%s%s);' % (ampersand, name)) prnt('}') prnt() def _generate_gen_constant_decl(self, tp, name): is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type() self._generate_gen_const(is_int, name, tp) _loading_gen_constant = _loaded_noop def _load_constant(self, is_int, tp, name, module, check_value=None): funcname = '_cffi_const_%s' % name if check_value is not None: assert is_int self._load_known_int_constant(module, funcname) value = check_value elif is_int: BType = self.ffi._typeof_locked("long long*")[0] BFunc = self.ffi._typeof_locked("int(*)(long long*)")[0] function = module.load_function(BFunc, funcname) p = self.ffi.new(BType) negative = function(p) value = int(p[0]) if value < 0 and not negative: BLongLong = self.ffi._typeof_locked("long long")[0] value += (1 << (8*self.ffi.sizeof(BLongLong))) else: assert check_value is None fntypeextra = '(*)(void)' if isinstance(tp, model.StructOrUnion): fntypeextra = '*' + fntypeextra BFunc = self.ffi._typeof_locked(tp.get_c_name(fntypeextra, name))[0] function = module.load_function(BFunc, funcname) value = function() if isinstance(tp, model.StructOrUnion): value = value[0] return value def _loaded_gen_constant(self, tp, name, module, library): is_int = isinstance(tp, model.PrimitiveType) and tp.is_integer_type() value = self._load_constant(is_int, tp, name, module) setattr(library, name, value) type(library)._cffi_dir.append(name) # ---------- # enums def _check_int_constant_value(self, name, value): prnt = self._prnt if value <= 0: prnt(' if ((%s) > 0 || (long)(%s) != %dL) {' % ( name, name, value)) else: prnt(' if ((%s) <= 0 || (unsigned long)(%s) != %dUL) {' % ( name, name, value)) prnt(' char buf[64];') prnt(' if ((%s) <= 0)' % name) prnt(' sprintf(buf, "%%ld", (long)(%s));' % name) prnt(' else') prnt(' sprintf(buf, "%%lu", (unsigned long)(%s));' % name) prnt(' sprintf(out_error, "%s has the real value %s, not %s",') prnt(' "%s", buf, "%d");' % (name[:100], value)) prnt(' return -1;') prnt(' }') def _load_known_int_constant(self, module, funcname): BType = self.ffi._typeof_locked("char[]")[0] BFunc = self.ffi._typeof_locked("int(*)(char*)")[0] function = module.load_function(BFunc, funcname) p = self.ffi.new(BType, 256) if function(p) < 0: error = self.ffi.string(p) if sys.version_info >= (3,): error = str(error, 'utf-8') raise VerificationError(error) def _enum_funcname(self, prefix, name): # "$enum_$1" => "___D_enum____D_1" name = name.replace('$', '___D_') return '_cffi_e_%s_%s' % (prefix, name) def _generate_gen_enum_decl(self, tp, name, prefix='enum'): if tp.partial: for enumerator in tp.enumerators: self._generate_gen_const(True, enumerator) return # funcname = self._enum_funcname(prefix, name) self.export_symbols.append(funcname) prnt = self._prnt prnt('int %s(char *out_error)' % funcname) prnt('{') for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues): self._check_int_constant_value(enumerator, enumvalue) prnt(' return 0;') prnt('}') prnt() def _loading_gen_enum(self, tp, name, module, prefix='enum'): if tp.partial: enumvalues = [self._load_constant(True, tp, enumerator, module) for enumerator in tp.enumerators] tp.enumvalues = tuple(enumvalues) tp.partial_resolved = True else: funcname = self._enum_funcname(prefix, name) self._load_known_int_constant(module, funcname) def _loaded_gen_enum(self, tp, name, module, library): for enumerator, enumvalue in zip(tp.enumerators, tp.enumvalues): setattr(library, enumerator, enumvalue) type(library)._cffi_dir.append(enumerator) # ---------- # macros: for now only for integers def _generate_gen_macro_decl(self, tp, name): if tp == '...': check_value = None else: check_value = tp # an integer self._generate_gen_const(True, name, check_value=check_value) _loading_gen_macro = _loaded_noop def _loaded_gen_macro(self, tp, name, module, library): if tp == '...': check_value = None else: check_value = tp # an integer value = self._load_constant(True, tp, name, module, check_value=check_value) setattr(library, name, value) type(library)._cffi_dir.append(name) # ---------- # global variables def _generate_gen_variable_decl(self, tp, name): if isinstance(tp, model.ArrayType): if tp.length == '...': prnt = self._prnt funcname = '_cffi_sizeof_%s' % (name,) self.export_symbols.append(funcname) prnt("size_t %s(void)" % funcname) prnt("{") prnt(" return sizeof(%s);" % (name,)) prnt("}") tp_ptr = model.PointerType(tp.item) self._generate_gen_const(False, name, tp_ptr) else: tp_ptr = model.PointerType(tp) self._generate_gen_const(False, name, tp_ptr, category='var') _loading_gen_variable = _loaded_noop def _loaded_gen_variable(self, tp, name, module, library): if isinstance(tp, model.ArrayType): # int a[5] is "constant" in the # sense that "a=..." is forbidden if tp.length == '...': funcname = '_cffi_sizeof_%s' % (name,) BFunc = self.ffi._typeof_locked('size_t(*)(void)')[0] function = module.load_function(BFunc, funcname) size = function() BItemType = self.ffi._get_cached_btype(tp.item) length, rest = divmod(size, self.ffi.sizeof(BItemType)) if rest != 0: raise VerificationError( "bad size: %r does not seem to be an array of %s" % (name, tp.item)) tp = tp.resolve_length(length) tp_ptr = model.PointerType(tp.item) value = self._load_constant(False, tp_ptr, name, module) # 'value' is a which we have to replace with # a if the N is actually known if tp.length is not None: BArray = self.ffi._get_cached_btype(tp) value = self.ffi.cast(BArray, value) setattr(library, name, value) type(library)._cffi_dir.append(name) return # remove ptr= from the library instance, and replace # it by a property on the class, which reads/writes into ptr[0]. funcname = '_cffi_var_%s' % name BFunc = self.ffi._typeof_locked(tp.get_c_name('*(*)(void)', name))[0] function = module.load_function(BFunc, funcname) ptr = function() def getter(library): return ptr[0] def setter(library, value): ptr[0] = value setattr(type(library), name, property(getter, setter)) type(library)._cffi_dir.append(name) cffimod_header = r''' #include #include #include #include #include /* XXX for ssize_t on some platforms */ /* this block of #ifs should be kept exactly identical between c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py and cffi/_cffi_include.h */ #if defined(_MSC_VER) # include /* for alloca() */ # if _MSC_VER < 1600 /* MSVC < 2010 */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; typedef __int8 int_least8_t; typedef __int16 int_least16_t; typedef __int32 int_least32_t; typedef __int64 int_least64_t; typedef unsigned __int8 uint_least8_t; typedef unsigned __int16 uint_least16_t; typedef unsigned __int32 uint_least32_t; typedef unsigned __int64 uint_least64_t; typedef __int8 int_fast8_t; typedef __int16 int_fast16_t; typedef __int32 int_fast32_t; typedef __int64 int_fast64_t; typedef unsigned __int8 uint_fast8_t; typedef unsigned __int16 uint_fast16_t; typedef unsigned __int32 uint_fast32_t; typedef unsigned __int64 uint_fast64_t; typedef __int64 intmax_t; typedef unsigned __int64 uintmax_t; # else # include # endif # if _MSC_VER < 1800 /* MSVC < 2013 */ # ifndef __cplusplus typedef unsigned char _Bool; # endif # endif #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif #endif ''' cparser.py000064400000114522152344566040006573 0ustar00from . import model from .commontypes import COMMON_TYPES, resolve_common_type from .error import FFIError, CDefError try: from . import _pycparser as pycparser except ImportError: import pycparser import weakref, re, sys try: if sys.version_info < (3,): import thread as _thread else: import _thread lock = _thread.allocate_lock() except ImportError: lock = None CDEF_SOURCE_STRING = "" _r_comment = re.compile(r"/\*.*?\*/|//([^\n\\]|\\.)*?$", re.DOTALL | re.MULTILINE) _r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)" r"\b((?:[^\n\\]|\\.)*?)$", re.DOTALL | re.MULTILINE) _r_partial_enum = re.compile(r"=\s*\.\.\.\s*[,}]|\.\.\.\s*\}") _r_enum_dotdotdot = re.compile(r"__dotdotdot\d+__$") _r_partial_array = re.compile(r"\[\s*\.\.\.\s*\]") _r_words = re.compile(r"\w+|\S") _parser_cache = None _r_int_literal = re.compile(r"-?0?x?[0-9a-f]+[lu]*$", re.IGNORECASE) _r_stdcall1 = re.compile(r"\b(__stdcall|WINAPI)\b") _r_stdcall2 = re.compile(r"[(]\s*(__stdcall|WINAPI)\b") _r_cdecl = re.compile(r"\b__cdecl\b") _r_extern_python = re.compile(r'\bextern\s*"' r'(Python|Python\s*\+\s*C|C\s*\+\s*Python)"\s*.') _r_star_const_space = re.compile( # matches "* const " r"[*]\s*((const|volatile|restrict)\b\s*)+") _r_int_dotdotdot = re.compile(r"(\b(int|long|short|signed|unsigned|char)\s*)+" r"\.\.\.") _r_float_dotdotdot = re.compile(r"\b(double|float)\s*\.\.\.") def _get_parser(): global _parser_cache if _parser_cache is None: _parser_cache = pycparser.CParser() return _parser_cache def _workaround_for_old_pycparser(csource): # Workaround for a pycparser issue (fixed between pycparser 2.10 and # 2.14): "char*const***" gives us a wrong syntax tree, the same as # for "char***(*const)". This means we can't tell the difference # afterwards. But "char(*const(***))" gives us the right syntax # tree. The issue only occurs if there are several stars in # sequence with no parenthesis inbetween, just possibly qualifiers. # Attempt to fix it by adding some parentheses in the source: each # time we see "* const" or "* const *", we add an opening # parenthesis before each star---the hard part is figuring out where # to close them. parts = [] while True: match = _r_star_const_space.search(csource) if not match: break #print repr(''.join(parts)+csource), '=>', parts.append(csource[:match.start()]) parts.append('('); closing = ')' parts.append(match.group()) # e.g. "* const " endpos = match.end() if csource.startswith('*', endpos): parts.append('('); closing += ')' level = 0 i = endpos while i < len(csource): c = csource[i] if c == '(': level += 1 elif c == ')': if level == 0: break level -= 1 elif c in ',;=': if level == 0: break i += 1 csource = csource[endpos:i] + closing + csource[i:] #print repr(''.join(parts)+csource) parts.append(csource) return ''.join(parts) def _preprocess_extern_python(csource): # input: `extern "Python" int foo(int);` or # `extern "Python" { int foo(int); }` # output: # void __cffi_extern_python_start; # int foo(int); # void __cffi_extern_python_stop; # # input: `extern "Python+C" int foo(int);` # output: # void __cffi_extern_python_plus_c_start; # int foo(int); # void __cffi_extern_python_stop; parts = [] while True: match = _r_extern_python.search(csource) if not match: break endpos = match.end() - 1 #print #print ''.join(parts)+csource #print '=>' parts.append(csource[:match.start()]) if 'C' in match.group(1): parts.append('void __cffi_extern_python_plus_c_start; ') else: parts.append('void __cffi_extern_python_start; ') if csource[endpos] == '{': # grouping variant closing = csource.find('}', endpos) if closing < 0: raise CDefError("'extern \"Python\" {': no '}' found") if csource.find('{', endpos + 1, closing) >= 0: raise NotImplementedError("cannot use { } inside a block " "'extern \"Python\" { ... }'") parts.append(csource[endpos+1:closing]) csource = csource[closing+1:] else: # non-grouping variant semicolon = csource.find(';', endpos) if semicolon < 0: raise CDefError("'extern \"Python\": no ';' found") parts.append(csource[endpos:semicolon+1]) csource = csource[semicolon+1:] parts.append(' void __cffi_extern_python_stop;') #print ''.join(parts)+csource #print parts.append(csource) return ''.join(parts) def _preprocess(csource): # Remove comments. NOTE: this only work because the cdef() section # should not contain any string literal! csource = _r_comment.sub(' ', csource) # Remove the "#define FOO x" lines macros = {} for match in _r_define.finditer(csource): macroname, macrovalue = match.groups() macrovalue = macrovalue.replace('\\\n', '').strip() macros[macroname] = macrovalue csource = _r_define.sub('', csource) # if pycparser.__version__ < '2.14': csource = _workaround_for_old_pycparser(csource) # # BIG HACK: replace WINAPI or __stdcall with "volatile const". # It doesn't make sense for the return type of a function to be # "volatile volatile const", so we abuse it to detect __stdcall... # Hack number 2 is that "int(volatile *fptr)();" is not valid C # syntax, so we place the "volatile" before the opening parenthesis. csource = _r_stdcall2.sub(' volatile volatile const(', csource) csource = _r_stdcall1.sub(' volatile volatile const ', csource) csource = _r_cdecl.sub(' ', csource) # # Replace `extern "Python"` with start/end markers csource = _preprocess_extern_python(csource) # # Replace "[...]" with "[__dotdotdotarray__]" csource = _r_partial_array.sub('[__dotdotdotarray__]', csource) # # Replace "...}" with "__dotdotdotNUM__}". This construction should # occur only at the end of enums; at the end of structs we have "...;}" # and at the end of vararg functions "...);". Also replace "=...[,}]" # with ",__dotdotdotNUM__[,}]": this occurs in the enums too, when # giving an unknown value. matches = list(_r_partial_enum.finditer(csource)) for number, match in enumerate(reversed(matches)): p = match.start() if csource[p] == '=': p2 = csource.find('...', p, match.end()) assert p2 > p csource = '%s,__dotdotdot%d__ %s' % (csource[:p], number, csource[p2+3:]) else: assert csource[p:p+3] == '...' csource = '%s __dotdotdot%d__ %s' % (csource[:p], number, csource[p+3:]) # Replace "int ..." or "unsigned long int..." with "__dotdotdotint__" csource = _r_int_dotdotdot.sub(' __dotdotdotint__ ', csource) # Replace "float ..." or "double..." with "__dotdotdotfloat__" csource = _r_float_dotdotdot.sub(' __dotdotdotfloat__ ', csource) # Replace all remaining "..." with the same name, "__dotdotdot__", # which is declared with a typedef for the purpose of C parsing. return csource.replace('...', ' __dotdotdot__ '), macros def _common_type_names(csource): # Look in the source for what looks like usages of types from the # list of common types. A "usage" is approximated here as the # appearance of the word, minus a "definition" of the type, which # is the last word in a "typedef" statement. Approximative only # but should be fine for all the common types. look_for_words = set(COMMON_TYPES) look_for_words.add(';') look_for_words.add(',') look_for_words.add('(') look_for_words.add(')') look_for_words.add('typedef') words_used = set() is_typedef = False paren = 0 previous_word = '' for word in _r_words.findall(csource): if word in look_for_words: if word == ';': if is_typedef: words_used.discard(previous_word) look_for_words.discard(previous_word) is_typedef = False elif word == 'typedef': is_typedef = True paren = 0 elif word == '(': paren += 1 elif word == ')': paren -= 1 elif word == ',': if is_typedef and paren == 0: words_used.discard(previous_word) look_for_words.discard(previous_word) else: # word in COMMON_TYPES words_used.add(word) previous_word = word return words_used class Parser(object): def __init__(self): self._declarations = {} self._included_declarations = set() self._anonymous_counter = 0 self._structnode2type = weakref.WeakKeyDictionary() self._options = {} self._int_constants = {} self._recomplete = [] self._uses_new_feature = None def _parse(self, csource): csource, macros = _preprocess(csource) # XXX: for more efficiency we would need to poke into the # internals of CParser... the following registers the # typedefs, because their presence or absence influences the # parsing itself (but what they are typedef'ed to plays no role) ctn = _common_type_names(csource) typenames = [] for name in sorted(self._declarations): if name.startswith('typedef '): name = name[8:] typenames.append(name) ctn.discard(name) typenames += sorted(ctn) # csourcelines = [] csourcelines.append('# 1 ""') for typename in typenames: csourcelines.append('typedef int %s;' % typename) csourcelines.append('typedef int __dotdotdotint__, __dotdotdotfloat__,' ' __dotdotdot__;') # this forces pycparser to consider the following in the file # called from line 1 csourcelines.append('# 1 "%s"' % (CDEF_SOURCE_STRING,)) csourcelines.append(csource) fullcsource = '\n'.join(csourcelines) if lock is not None: lock.acquire() # pycparser is not thread-safe... try: ast = _get_parser().parse(fullcsource) except pycparser.c_parser.ParseError as e: self.convert_pycparser_error(e, csource) finally: if lock is not None: lock.release() # csource will be used to find buggy source text return ast, macros, csource def _convert_pycparser_error(self, e, csource): # xxx look for ":NUM:" at the start of str(e) # and interpret that as a line number. This will not work if # the user gives explicit ``# NUM "FILE"`` directives. line = None msg = str(e) match = re.match(r"%s:(\d+):" % (CDEF_SOURCE_STRING,), msg) if match: linenum = int(match.group(1), 10) csourcelines = csource.splitlines() if 1 <= linenum <= len(csourcelines): line = csourcelines[linenum-1] return line def convert_pycparser_error(self, e, csource): line = self._convert_pycparser_error(e, csource) msg = str(e) if line: msg = 'cannot parse "%s"\n%s' % (line.strip(), msg) else: msg = 'parse error\n%s' % (msg,) raise CDefError(msg) def parse(self, csource, override=False, packed=False, dllexport=False): prev_options = self._options try: self._options = {'override': override, 'packed': packed, 'dllexport': dllexport} self._internal_parse(csource) finally: self._options = prev_options def _internal_parse(self, csource): ast, macros, csource = self._parse(csource) # add the macros self._process_macros(macros) # find the first "__dotdotdot__" and use that as a separator # between the repeated typedefs and the real csource iterator = iter(ast.ext) for decl in iterator: if decl.name == '__dotdotdot__': break else: assert 0 current_decl = None # try: self._inside_extern_python = '__cffi_extern_python_stop' for decl in iterator: current_decl = decl if isinstance(decl, pycparser.c_ast.Decl): self._parse_decl(decl) elif isinstance(decl, pycparser.c_ast.Typedef): if not decl.name: raise CDefError("typedef does not declare any name", decl) quals = 0 if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType) and decl.type.type.names[-1].startswith('__dotdotdot')): realtype = self._get_unknown_type(decl) elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and isinstance(decl.type.type.type, pycparser.c_ast.IdentifierType) and decl.type.type.type.names[-1].startswith('__dotdotdot')): realtype = self._get_unknown_ptr_type(decl) else: realtype, quals = self._get_type_and_quals( decl.type, name=decl.name, partial_length_ok=True) self._declare('typedef ' + decl.name, realtype, quals=quals) elif decl.__class__.__name__ == 'Pragma': pass # skip pragma, only in pycparser 2.15 else: raise CDefError("unexpected <%s>: this construct is valid " "C but not valid in cdef()" % decl.__class__.__name__, decl) except CDefError as e: if len(e.args) == 1: e.args = e.args + (current_decl,) raise except FFIError as e: msg = self._convert_pycparser_error(e, csource) if msg: e.args = (e.args[0] + "\n *** Err: %s" % msg,) raise def _add_constants(self, key, val): if key in self._int_constants: if self._int_constants[key] == val: return # ignore identical double declarations raise FFIError( "multiple declarations of constant: %s" % (key,)) self._int_constants[key] = val def _add_integer_constant(self, name, int_str): int_str = int_str.lower().rstrip("ul") neg = int_str.startswith('-') if neg: int_str = int_str[1:] # "010" is not valid oct in py3 if (int_str.startswith("0") and int_str != '0' and not int_str.startswith("0x")): int_str = "0o" + int_str[1:] pyvalue = int(int_str, 0) if neg: pyvalue = -pyvalue self._add_constants(name, pyvalue) self._declare('macro ' + name, pyvalue) def _process_macros(self, macros): for key, value in macros.items(): value = value.strip() if _r_int_literal.match(value): self._add_integer_constant(key, value) elif value == '...': self._declare('macro ' + key, value) else: raise CDefError( 'only supports one of the following syntax:\n' ' #define %s ... (literally dot-dot-dot)\n' ' #define %s NUMBER (with NUMBER an integer' ' constant, decimal/hex/octal)\n' 'got:\n' ' #define %s %s' % (key, key, key, value)) def _declare_function(self, tp, quals, decl): tp = self._get_type_pointer(tp, quals) if self._options.get('dllexport'): tag = 'dllexport_python ' elif self._inside_extern_python == '__cffi_extern_python_start': tag = 'extern_python ' elif self._inside_extern_python == '__cffi_extern_python_plus_c_start': tag = 'extern_python_plus_c ' else: tag = 'function ' self._declare(tag + decl.name, tp) def _parse_decl(self, decl): node = decl.type if isinstance(node, pycparser.c_ast.FuncDecl): tp, quals = self._get_type_and_quals(node, name=decl.name) assert isinstance(tp, model.RawFunctionType) self._declare_function(tp, quals, decl) else: if isinstance(node, pycparser.c_ast.Struct): self._get_struct_union_enum_type('struct', node) elif isinstance(node, pycparser.c_ast.Union): self._get_struct_union_enum_type('union', node) elif isinstance(node, pycparser.c_ast.Enum): self._get_struct_union_enum_type('enum', node) elif not decl.name: raise CDefError("construct does not declare any variable", decl) # if decl.name: tp, quals = self._get_type_and_quals(node, partial_length_ok=True) if tp.is_raw_function: self._declare_function(tp, quals, decl) elif (tp.is_integer_type() and hasattr(decl, 'init') and hasattr(decl.init, 'value') and _r_int_literal.match(decl.init.value)): self._add_integer_constant(decl.name, decl.init.value) elif (tp.is_integer_type() and isinstance(decl.init, pycparser.c_ast.UnaryOp) and decl.init.op == '-' and hasattr(decl.init.expr, 'value') and _r_int_literal.match(decl.init.expr.value)): self._add_integer_constant(decl.name, '-' + decl.init.expr.value) elif (tp is model.void_type and decl.name.startswith('__cffi_extern_python_')): # hack: `extern "Python"` in the C source is replaced # with "void __cffi_extern_python_start;" and # "void __cffi_extern_python_stop;" self._inside_extern_python = decl.name else: if self._inside_extern_python !='__cffi_extern_python_stop': raise CDefError( "cannot declare constants or " "variables with 'extern \"Python\"'") if (quals & model.Q_CONST) and not tp.is_array_type: self._declare('constant ' + decl.name, tp, quals=quals) else: self._declare('variable ' + decl.name, tp, quals=quals) def parse_type(self, cdecl): return self.parse_type_and_quals(cdecl)[0] def parse_type_and_quals(self, cdecl): ast, macros = self._parse('void __dummy(\n%s\n);' % cdecl)[:2] assert not macros exprnode = ast.ext[-1].type.args.params[0] if isinstance(exprnode, pycparser.c_ast.ID): raise CDefError("unknown identifier '%s'" % (exprnode.name,)) return self._get_type_and_quals(exprnode.type) def _declare(self, name, obj, included=False, quals=0): if name in self._declarations: prevobj, prevquals = self._declarations[name] if prevobj is obj and prevquals == quals: return if not self._options.get('override'): raise FFIError( "multiple declarations of %s (for interactive usage, " "try cdef(xx, override=True))" % (name,)) assert '__dotdotdot__' not in name.split() self._declarations[name] = (obj, quals) if included: self._included_declarations.add(obj) def _extract_quals(self, type): quals = 0 if isinstance(type, (pycparser.c_ast.TypeDecl, pycparser.c_ast.PtrDecl)): if 'const' in type.quals: quals |= model.Q_CONST if 'volatile' in type.quals: quals |= model.Q_VOLATILE if 'restrict' in type.quals: quals |= model.Q_RESTRICT return quals def _get_type_pointer(self, type, quals, declname=None): if isinstance(type, model.RawFunctionType): return type.as_function_pointer() if (isinstance(type, model.StructOrUnionOrEnum) and type.name.startswith('$') and type.name[1:].isdigit() and type.forcename is None and declname is not None): return model.NamedPointerType(type, declname, quals) return model.PointerType(type, quals) def _get_type_and_quals(self, typenode, name=None, partial_length_ok=False): # first, dereference typedefs, if we have it already parsed, we're good if (isinstance(typenode, pycparser.c_ast.TypeDecl) and isinstance(typenode.type, pycparser.c_ast.IdentifierType) and len(typenode.type.names) == 1 and ('typedef ' + typenode.type.names[0]) in self._declarations): tp, quals = self._declarations['typedef ' + typenode.type.names[0]] quals |= self._extract_quals(typenode) return tp, quals # if isinstance(typenode, pycparser.c_ast.ArrayDecl): # array type if typenode.dim is None: length = None else: length = self._parse_constant( typenode.dim, partial_length_ok=partial_length_ok) tp, quals = self._get_type_and_quals(typenode.type, partial_length_ok=partial_length_ok) return model.ArrayType(tp, length), quals # if isinstance(typenode, pycparser.c_ast.PtrDecl): # pointer type itemtype, itemquals = self._get_type_and_quals(typenode.type) tp = self._get_type_pointer(itemtype, itemquals, declname=name) quals = self._extract_quals(typenode) return tp, quals # if isinstance(typenode, pycparser.c_ast.TypeDecl): quals = self._extract_quals(typenode) type = typenode.type if isinstance(type, pycparser.c_ast.IdentifierType): # assume a primitive type. get it from .names, but reduce # synonyms to a single chosen combination names = list(type.names) if names != ['signed', 'char']: # keep this unmodified prefixes = {} while names: name = names[0] if name in ('short', 'long', 'signed', 'unsigned'): prefixes[name] = prefixes.get(name, 0) + 1 del names[0] else: break # ignore the 'signed' prefix below, and reorder the others newnames = [] for prefix in ('unsigned', 'short', 'long'): for i in range(prefixes.get(prefix, 0)): newnames.append(prefix) if not names: names = ['int'] # implicitly if names == ['int']: # but kill it if 'short' or 'long' if 'short' in prefixes or 'long' in prefixes: names = [] names = newnames + names ident = ' '.join(names) if ident == 'void': return model.void_type, quals if ident == '__dotdotdot__': raise FFIError(':%d: bad usage of "..."' % typenode.coord.line) tp0, quals0 = resolve_common_type(self, ident) return tp0, (quals | quals0) # if isinstance(type, pycparser.c_ast.Struct): # 'struct foobar' tp = self._get_struct_union_enum_type('struct', type, name) return tp, quals # if isinstance(type, pycparser.c_ast.Union): # 'union foobar' tp = self._get_struct_union_enum_type('union', type, name) return tp, quals # if isinstance(type, pycparser.c_ast.Enum): # 'enum foobar' tp = self._get_struct_union_enum_type('enum', type, name) return tp, quals # if isinstance(typenode, pycparser.c_ast.FuncDecl): # a function type return self._parse_function_type(typenode, name), 0 # # nested anonymous structs or unions end up here if isinstance(typenode, pycparser.c_ast.Struct): return self._get_struct_union_enum_type('struct', typenode, name, nested=True), 0 if isinstance(typenode, pycparser.c_ast.Union): return self._get_struct_union_enum_type('union', typenode, name, nested=True), 0 # raise FFIError(":%d: bad or unsupported type declaration" % typenode.coord.line) def _parse_function_type(self, typenode, funcname=None): params = list(getattr(typenode.args, 'params', [])) for i, arg in enumerate(params): if not hasattr(arg, 'type'): raise CDefError("%s arg %d: unknown type '%s'" " (if you meant to use the old C syntax of giving" " untyped arguments, it is not supported)" % (funcname or 'in expression', i + 1, getattr(arg, 'name', '?'))) ellipsis = ( len(params) > 0 and isinstance(params[-1].type, pycparser.c_ast.TypeDecl) and isinstance(params[-1].type.type, pycparser.c_ast.IdentifierType) and params[-1].type.type.names == ['__dotdotdot__']) if ellipsis: params.pop() if not params: raise CDefError( "%s: a function with only '(...)' as argument" " is not correct C" % (funcname or 'in expression')) args = [self._as_func_arg(*self._get_type_and_quals(argdeclnode.type)) for argdeclnode in params] if not ellipsis and args == [model.void_type]: args = [] result, quals = self._get_type_and_quals(typenode.type) # the 'quals' on the result type are ignored. HACK: we absure them # to detect __stdcall functions: we textually replace "__stdcall" # with "volatile volatile const" above. abi = None if hasattr(typenode.type, 'quals'): # else, probable syntax error anyway if typenode.type.quals[-3:] == ['volatile', 'volatile', 'const']: abi = '__stdcall' return model.RawFunctionType(tuple(args), result, ellipsis, abi) def _as_func_arg(self, type, quals): if isinstance(type, model.ArrayType): return model.PointerType(type.item, quals) elif isinstance(type, model.RawFunctionType): return type.as_function_pointer() else: return type def _get_struct_union_enum_type(self, kind, type, name=None, nested=False): # First, a level of caching on the exact 'type' node of the AST. # This is obscure, but needed because pycparser "unrolls" declarations # such as "typedef struct { } foo_t, *foo_p" and we end up with # an AST that is not a tree, but a DAG, with the "type" node of the # two branches foo_t and foo_p of the trees being the same node. # It's a bit silly but detecting "DAG-ness" in the AST tree seems # to be the only way to distinguish this case from two independent # structs. See test_struct_with_two_usages. try: return self._structnode2type[type] except KeyError: pass # # Note that this must handle parsing "struct foo" any number of # times and always return the same StructType object. Additionally, # one of these times (not necessarily the first), the fields of # the struct can be specified with "struct foo { ...fields... }". # If no name is given, then we have to create a new anonymous struct # with no caching; in this case, the fields are either specified # right now or never. # force_name = name name = type.name # # get the type or create it if needed if name is None: # 'force_name' is used to guess a more readable name for # anonymous structs, for the common case "typedef struct { } foo". if force_name is not None: explicit_name = '$%s' % force_name else: self._anonymous_counter += 1 explicit_name = '$%d' % self._anonymous_counter tp = None else: explicit_name = name key = '%s %s' % (kind, name) tp, _ = self._declarations.get(key, (None, None)) # if tp is None: if kind == 'struct': tp = model.StructType(explicit_name, None, None, None) elif kind == 'union': tp = model.UnionType(explicit_name, None, None, None) elif kind == 'enum': if explicit_name == '__dotdotdot__': raise CDefError("Enums cannot be declared with ...") tp = self._build_enum_type(explicit_name, type.values) else: raise AssertionError("kind = %r" % (kind,)) if name is not None: self._declare(key, tp) else: if kind == 'enum' and type.values is not None: raise NotImplementedError( "enum %s: the '{}' declaration should appear on the first " "time the enum is mentioned, not later" % explicit_name) if not tp.forcename: tp.force_the_name(force_name) if tp.forcename and '$' in tp.name: self._declare('anonymous %s' % tp.forcename, tp) # self._structnode2type[type] = tp # # enums: done here if kind == 'enum': return tp # # is there a 'type.decls'? If yes, then this is the place in the # C sources that declare the fields. If no, then just return the # existing type, possibly still incomplete. if type.decls is None: return tp # if tp.fldnames is not None: raise CDefError("duplicate declaration of struct %s" % name) fldnames = [] fldtypes = [] fldbitsize = [] fldquals = [] for decl in type.decls: if (isinstance(decl.type, pycparser.c_ast.IdentifierType) and ''.join(decl.type.names) == '__dotdotdot__'): # XXX pycparser is inconsistent: 'names' should be a list # of strings, but is sometimes just one string. Use # str.join() as a way to cope with both. self._make_partial(tp, nested) continue if decl.bitsize is None: bitsize = -1 else: bitsize = self._parse_constant(decl.bitsize) self._partial_length = False type, fqual = self._get_type_and_quals(decl.type, partial_length_ok=True) if self._partial_length: self._make_partial(tp, nested) if isinstance(type, model.StructType) and type.partial: self._make_partial(tp, nested) fldnames.append(decl.name or '') fldtypes.append(type) fldbitsize.append(bitsize) fldquals.append(fqual) tp.fldnames = tuple(fldnames) tp.fldtypes = tuple(fldtypes) tp.fldbitsize = tuple(fldbitsize) tp.fldquals = tuple(fldquals) if fldbitsize != [-1] * len(fldbitsize): if isinstance(tp, model.StructType) and tp.partial: raise NotImplementedError("%s: using both bitfields and '...;'" % (tp,)) tp.packed = self._options.get('packed') if tp.completed: # must be re-completed: it is not opaque any more tp.completed = 0 self._recomplete.append(tp) return tp def _make_partial(self, tp, nested): if not isinstance(tp, model.StructOrUnion): raise CDefError("%s cannot be partial" % (tp,)) if not tp.has_c_name() and not nested: raise NotImplementedError("%s is partial but has no C name" %(tp,)) tp.partial = True def _parse_constant(self, exprnode, partial_length_ok=False): # for now, limited to expressions that are an immediate number # or positive/negative number if isinstance(exprnode, pycparser.c_ast.Constant): s = exprnode.value if s.startswith('0'): if s.startswith('0x') or s.startswith('0X'): return int(s, 16) return int(s, 8) elif '1' <= s[0] <= '9': return int(s, 10) elif s[0] == "'" and s[-1] == "'" and ( len(s) == 3 or (len(s) == 4 and s[1] == "\\")): return ord(s[-2]) else: raise CDefError("invalid constant %r" % (s,)) # if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and exprnode.op == '+'): return self._parse_constant(exprnode.expr) # if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and exprnode.op == '-'): return -self._parse_constant(exprnode.expr) # load previously defined int constant if (isinstance(exprnode, pycparser.c_ast.ID) and exprnode.name in self._int_constants): return self._int_constants[exprnode.name] # if (isinstance(exprnode, pycparser.c_ast.ID) and exprnode.name == '__dotdotdotarray__'): if partial_length_ok: self._partial_length = True return '...' raise FFIError(":%d: unsupported '[...]' here, cannot derive " "the actual array length in this context" % exprnode.coord.line) # if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and exprnode.op == '+'): return (self._parse_constant(exprnode.left) + self._parse_constant(exprnode.right)) # if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and exprnode.op == '-'): return (self._parse_constant(exprnode.left) - self._parse_constant(exprnode.right)) # raise FFIError(":%d: unsupported expression: expected a " "simple numeric constant" % exprnode.coord.line) def _build_enum_type(self, explicit_name, decls): if decls is not None: partial = False enumerators = [] enumvalues = [] nextenumvalue = 0 for enum in decls.enumerators: if _r_enum_dotdotdot.match(enum.name): partial = True continue if enum.value is not None: nextenumvalue = self._parse_constant(enum.value) enumerators.append(enum.name) enumvalues.append(nextenumvalue) self._add_constants(enum.name, nextenumvalue) nextenumvalue += 1 enumerators = tuple(enumerators) enumvalues = tuple(enumvalues) tp = model.EnumType(explicit_name, enumerators, enumvalues) tp.partial = partial else: # opaque enum tp = model.EnumType(explicit_name, (), ()) return tp def include(self, other): for name, (tp, quals) in other._declarations.items(): if name.startswith('anonymous $enum_$'): continue # fix for test_anonymous_enum_include kind = name.split(' ', 1)[0] if kind in ('struct', 'union', 'enum', 'anonymous', 'typedef'): self._declare(name, tp, included=True, quals=quals) for k, v in other._int_constants.items(): self._add_constants(k, v) def _get_unknown_type(self, decl): typenames = decl.type.type.names if typenames == ['__dotdotdot__']: return model.unknown_type(decl.name) if typenames == ['__dotdotdotint__']: if self._uses_new_feature is None: self._uses_new_feature = "'typedef int... %s'" % decl.name return model.UnknownIntegerType(decl.name) if typenames == ['__dotdotdotfloat__']: # note: not for 'long double' so far if self._uses_new_feature is None: self._uses_new_feature = "'typedef float... %s'" % decl.name return model.UnknownFloatType(decl.name) raise FFIError(':%d: unsupported usage of "..." in typedef' % decl.coord.line) def _get_unknown_ptr_type(self, decl): if decl.type.type.type.names == ['__dotdotdot__']: return model.unknown_ptr_type(decl.name) raise FFIError(':%d: unsupported usage of "..." in typedef' % decl.coord.line) __init__.py000064400000000737152344566040006675 0ustar00__all__ = ['FFI', 'VerificationError', 'VerificationMissing', 'CDefError', 'FFIError'] from .api import FFI from .error import CDefError, FFIError, VerificationError, VerificationMissing __version__ = "1.11.5" __version_info__ = (1, 11, 5) # The verifier module file names are based on the CRC32 of a string that # contains the following version number. It may be older than __version__ # if nothing is clearly incompatible. __version_verifier_modules__ = "0.8.6" _cffi_include.h000064400000027565152344566040007516 0ustar00#define _CFFI_ /* We try to define Py_LIMITED_API before including Python.h. Mess: we can only define it if Py_DEBUG, Py_TRACE_REFS and Py_REF_DEBUG are not defined. This is a best-effort approximation: we can learn about Py_DEBUG from pyconfig.h, but it is unclear if the same works for the other two macros. Py_DEBUG implies them, but not the other way around. Issue #350 is still open: on Windows, the code here causes it to link with PYTHON36.DLL (for example) instead of PYTHON3.DLL. A fix was attempted in 164e526a5515 and 14ce6985e1c3, but reverted: virtualenv does not make PYTHON3.DLL available, and so the "correctly" compiled version would not run inside a virtualenv. We will re-apply the fix after virtualenv has been fixed for some time. For explanation, see issue #355. For a workaround if you want PYTHON3.DLL and don't worry about virtualenv, see issue #350. See also 'py_limited_api' in setuptools_ext.py. */ #if !defined(_CFFI_USE_EMBEDDING) && !defined(Py_LIMITED_API) # include # if !defined(Py_DEBUG) && !defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) # define Py_LIMITED_API # endif #endif #include #ifdef __cplusplus extern "C" { #endif #include #include "parse_c_type.h" /* this block of #ifs should be kept exactly identical between c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py and cffi/_cffi_include.h */ #if defined(_MSC_VER) # include /* for alloca() */ # if _MSC_VER < 1600 /* MSVC < 2010 */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; typedef __int8 int_least8_t; typedef __int16 int_least16_t; typedef __int32 int_least32_t; typedef __int64 int_least64_t; typedef unsigned __int8 uint_least8_t; typedef unsigned __int16 uint_least16_t; typedef unsigned __int32 uint_least32_t; typedef unsigned __int64 uint_least64_t; typedef __int8 int_fast8_t; typedef __int16 int_fast16_t; typedef __int32 int_fast32_t; typedef __int64 int_fast64_t; typedef unsigned __int8 uint_fast8_t; typedef unsigned __int16 uint_fast16_t; typedef unsigned __int32 uint_fast32_t; typedef unsigned __int64 uint_fast64_t; typedef __int64 intmax_t; typedef unsigned __int64 uintmax_t; # else # include # endif # if _MSC_VER < 1800 /* MSVC < 2013 */ # ifndef __cplusplus typedef unsigned char _Bool; # endif # endif #else # include # if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) # include # endif #endif #ifdef __GNUC__ # define _CFFI_UNUSED_FN __attribute__((unused)) #else # define _CFFI_UNUSED_FN /* nothing */ #endif #ifdef __cplusplus # ifndef _Bool typedef bool _Bool; /* semi-hackish: C++ has no _Bool; bool is builtin */ # endif #endif /********** CPython-specific section **********/ #ifndef PYPY_VERSION #if PY_MAJOR_VERSION >= 3 # define PyInt_FromLong PyLong_FromLong #endif #define _cffi_from_c_double PyFloat_FromDouble #define _cffi_from_c_float PyFloat_FromDouble #define _cffi_from_c_long PyInt_FromLong #define _cffi_from_c_ulong PyLong_FromUnsignedLong #define _cffi_from_c_longlong PyLong_FromLongLong #define _cffi_from_c_ulonglong PyLong_FromUnsignedLongLong #define _cffi_from_c__Bool PyBool_FromLong #define _cffi_to_c_double PyFloat_AsDouble #define _cffi_to_c_float PyFloat_AsDouble #define _cffi_from_c_int(x, type) \ (((type)-1) > 0 ? /* unsigned */ \ (sizeof(type) < sizeof(long) ? \ PyInt_FromLong((long)x) : \ sizeof(type) == sizeof(long) ? \ PyLong_FromUnsignedLong((unsigned long)x) : \ PyLong_FromUnsignedLongLong((unsigned long long)x)) : \ (sizeof(type) <= sizeof(long) ? \ PyInt_FromLong((long)x) : \ PyLong_FromLongLong((long long)x))) #define _cffi_to_c_int(o, type) \ ((type)( \ sizeof(type) == 1 ? (((type)-1) > 0 ? (type)_cffi_to_c_u8(o) \ : (type)_cffi_to_c_i8(o)) : \ sizeof(type) == 2 ? (((type)-1) > 0 ? (type)_cffi_to_c_u16(o) \ : (type)_cffi_to_c_i16(o)) : \ sizeof(type) == 4 ? (((type)-1) > 0 ? (type)_cffi_to_c_u32(o) \ : (type)_cffi_to_c_i32(o)) : \ sizeof(type) == 8 ? (((type)-1) > 0 ? (type)_cffi_to_c_u64(o) \ : (type)_cffi_to_c_i64(o)) : \ (Py_FatalError("unsupported size for type " #type), (type)0))) #define _cffi_to_c_i8 \ ((int(*)(PyObject *))_cffi_exports[1]) #define _cffi_to_c_u8 \ ((int(*)(PyObject *))_cffi_exports[2]) #define _cffi_to_c_i16 \ ((int(*)(PyObject *))_cffi_exports[3]) #define _cffi_to_c_u16 \ ((int(*)(PyObject *))_cffi_exports[4]) #define _cffi_to_c_i32 \ ((int(*)(PyObject *))_cffi_exports[5]) #define _cffi_to_c_u32 \ ((unsigned int(*)(PyObject *))_cffi_exports[6]) #define _cffi_to_c_i64 \ ((long long(*)(PyObject *))_cffi_exports[7]) #define _cffi_to_c_u64 \ ((unsigned long long(*)(PyObject *))_cffi_exports[8]) #define _cffi_to_c_char \ ((int(*)(PyObject *))_cffi_exports[9]) #define _cffi_from_c_pointer \ ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[10]) #define _cffi_to_c_pointer \ ((char *(*)(PyObject *, struct _cffi_ctypedescr *))_cffi_exports[11]) #define _cffi_get_struct_layout \ not used any more #define _cffi_restore_errno \ ((void(*)(void))_cffi_exports[13]) #define _cffi_save_errno \ ((void(*)(void))_cffi_exports[14]) #define _cffi_from_c_char \ ((PyObject *(*)(char))_cffi_exports[15]) #define _cffi_from_c_deref \ ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[16]) #define _cffi_to_c \ ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[17]) #define _cffi_from_c_struct \ ((PyObject *(*)(char *, struct _cffi_ctypedescr *))_cffi_exports[18]) #define _cffi_to_c_wchar_t \ ((_cffi_wchar_t(*)(PyObject *))_cffi_exports[19]) #define _cffi_from_c_wchar_t \ ((PyObject *(*)(_cffi_wchar_t))_cffi_exports[20]) #define _cffi_to_c_long_double \ ((long double(*)(PyObject *))_cffi_exports[21]) #define _cffi_to_c__Bool \ ((_Bool(*)(PyObject *))_cffi_exports[22]) #define _cffi_prepare_pointer_call_argument \ ((Py_ssize_t(*)(struct _cffi_ctypedescr *, \ PyObject *, char **))_cffi_exports[23]) #define _cffi_convert_array_from_object \ ((int(*)(char *, struct _cffi_ctypedescr *, PyObject *))_cffi_exports[24]) #define _CFFI_CPIDX 25 #define _cffi_call_python \ ((void(*)(struct _cffi_externpy_s *, char *))_cffi_exports[_CFFI_CPIDX]) #define _cffi_to_c_wchar3216_t \ ((int(*)(PyObject *))_cffi_exports[26]) #define _cffi_from_c_wchar3216_t \ ((PyObject *(*)(int))_cffi_exports[27]) #define _CFFI_NUM_EXPORTS 28 struct _cffi_ctypedescr; static void *_cffi_exports[_CFFI_NUM_EXPORTS]; #define _cffi_type(index) ( \ assert((((uintptr_t)_cffi_types[index]) & 1) == 0), \ (struct _cffi_ctypedescr *)_cffi_types[index]) static PyObject *_cffi_init(const char *module_name, Py_ssize_t version, const struct _cffi_type_context_s *ctx) { PyObject *module, *o_arg, *new_module; void *raw[] = { (void *)module_name, (void *)version, (void *)_cffi_exports, (void *)ctx, }; module = PyImport_ImportModule("_cffi_backend"); if (module == NULL) goto failure; o_arg = PyLong_FromVoidPtr((void *)raw); if (o_arg == NULL) goto failure; new_module = PyObject_CallMethod( module, (char *)"_init_cffi_1_0_external_module", (char *)"O", o_arg); Py_DECREF(o_arg); Py_DECREF(module); return new_module; failure: Py_XDECREF(module); return NULL; } #ifdef HAVE_WCHAR_H typedef wchar_t _cffi_wchar_t; #else typedef uint16_t _cffi_wchar_t; /* same random pick as _cffi_backend.c */ #endif _CFFI_UNUSED_FN static uint16_t _cffi_to_c_char16_t(PyObject *o) { if (sizeof(_cffi_wchar_t) == 2) return (uint16_t)_cffi_to_c_wchar_t(o); else return (uint16_t)_cffi_to_c_wchar3216_t(o); } _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char16_t(uint16_t x) { if (sizeof(_cffi_wchar_t) == 2) return _cffi_from_c_wchar_t((_cffi_wchar_t)x); else return _cffi_from_c_wchar3216_t((int)x); } _CFFI_UNUSED_FN static int _cffi_to_c_char32_t(PyObject *o) { if (sizeof(_cffi_wchar_t) == 4) return (int)_cffi_to_c_wchar_t(o); else return (int)_cffi_to_c_wchar3216_t(o); } _CFFI_UNUSED_FN static PyObject *_cffi_from_c_char32_t(int x) { if (sizeof(_cffi_wchar_t) == 4) return _cffi_from_c_wchar_t((_cffi_wchar_t)x); else return _cffi_from_c_wchar3216_t(x); } /********** end CPython-specific section **********/ #else _CFFI_UNUSED_FN static void (*_cffi_call_python_org)(struct _cffi_externpy_s *, char *); # define _cffi_call_python _cffi_call_python_org #endif #define _cffi_array_len(array) (sizeof(array) / sizeof((array)[0])) #define _cffi_prim_int(size, sign) \ ((size) == 1 ? ((sign) ? _CFFI_PRIM_INT8 : _CFFI_PRIM_UINT8) : \ (size) == 2 ? ((sign) ? _CFFI_PRIM_INT16 : _CFFI_PRIM_UINT16) : \ (size) == 4 ? ((sign) ? _CFFI_PRIM_INT32 : _CFFI_PRIM_UINT32) : \ (size) == 8 ? ((sign) ? _CFFI_PRIM_INT64 : _CFFI_PRIM_UINT64) : \ _CFFI__UNKNOWN_PRIM) #define _cffi_prim_float(size) \ ((size) == sizeof(float) ? _CFFI_PRIM_FLOAT : \ (size) == sizeof(double) ? _CFFI_PRIM_DOUBLE : \ (size) == sizeof(long double) ? _CFFI__UNKNOWN_LONG_DOUBLE : \ _CFFI__UNKNOWN_FLOAT_PRIM) #define _cffi_check_int(got, got_nonpos, expected) \ ((got_nonpos) == (expected <= 0) && \ (got) == (unsigned long long)expected) #ifdef MS_WIN32 # define _cffi_stdcall __stdcall #else # define _cffi_stdcall /* nothing */ #endif #ifdef __cplusplus } #endif cffi_opcode.py000064400000013134152344566040007371 0ustar00from .error import VerificationError class CffiOp(object): def __init__(self, op, arg): self.op = op self.arg = arg def as_c_expr(self): if self.op is None: assert isinstance(self.arg, str) return '(_cffi_opcode_t)(%s)' % (self.arg,) classname = CLASS_NAME[self.op] return '_CFFI_OP(_CFFI_OP_%s, %s)' % (classname, self.arg) def as_python_bytes(self): if self.op is None and self.arg.isdigit(): value = int(self.arg) # non-negative: '-' not in self.arg if value >= 2**31: raise OverflowError("cannot emit %r: limited to 2**31-1" % (self.arg,)) return format_four_bytes(value) if isinstance(self.arg, str): raise VerificationError("cannot emit to Python: %r" % (self.arg,)) return format_four_bytes((self.arg << 8) | self.op) def __str__(self): classname = CLASS_NAME.get(self.op, self.op) return '(%s %s)' % (classname, self.arg) def format_four_bytes(num): return '\\x%02X\\x%02X\\x%02X\\x%02X' % ( (num >> 24) & 0xFF, (num >> 16) & 0xFF, (num >> 8) & 0xFF, (num ) & 0xFF) OP_PRIMITIVE = 1 OP_POINTER = 3 OP_ARRAY = 5 OP_OPEN_ARRAY = 7 OP_STRUCT_UNION = 9 OP_ENUM = 11 OP_FUNCTION = 13 OP_FUNCTION_END = 15 OP_NOOP = 17 OP_BITFIELD = 19 OP_TYPENAME = 21 OP_CPYTHON_BLTN_V = 23 # varargs OP_CPYTHON_BLTN_N = 25 # noargs OP_CPYTHON_BLTN_O = 27 # O (i.e. a single arg) OP_CONSTANT = 29 OP_CONSTANT_INT = 31 OP_GLOBAL_VAR = 33 OP_DLOPEN_FUNC = 35 OP_DLOPEN_CONST = 37 OP_GLOBAL_VAR_F = 39 OP_EXTERN_PYTHON = 41 PRIM_VOID = 0 PRIM_BOOL = 1 PRIM_CHAR = 2 PRIM_SCHAR = 3 PRIM_UCHAR = 4 PRIM_SHORT = 5 PRIM_USHORT = 6 PRIM_INT = 7 PRIM_UINT = 8 PRIM_LONG = 9 PRIM_ULONG = 10 PRIM_LONGLONG = 11 PRIM_ULONGLONG = 12 PRIM_FLOAT = 13 PRIM_DOUBLE = 14 PRIM_LONGDOUBLE = 15 PRIM_WCHAR = 16 PRIM_INT8 = 17 PRIM_UINT8 = 18 PRIM_INT16 = 19 PRIM_UINT16 = 20 PRIM_INT32 = 21 PRIM_UINT32 = 22 PRIM_INT64 = 23 PRIM_UINT64 = 24 PRIM_INTPTR = 25 PRIM_UINTPTR = 26 PRIM_PTRDIFF = 27 PRIM_SIZE = 28 PRIM_SSIZE = 29 PRIM_INT_LEAST8 = 30 PRIM_UINT_LEAST8 = 31 PRIM_INT_LEAST16 = 32 PRIM_UINT_LEAST16 = 33 PRIM_INT_LEAST32 = 34 PRIM_UINT_LEAST32 = 35 PRIM_INT_LEAST64 = 36 PRIM_UINT_LEAST64 = 37 PRIM_INT_FAST8 = 38 PRIM_UINT_FAST8 = 39 PRIM_INT_FAST16 = 40 PRIM_UINT_FAST16 = 41 PRIM_INT_FAST32 = 42 PRIM_UINT_FAST32 = 43 PRIM_INT_FAST64 = 44 PRIM_UINT_FAST64 = 45 PRIM_INTMAX = 46 PRIM_UINTMAX = 47 PRIM_FLOATCOMPLEX = 48 PRIM_DOUBLECOMPLEX = 49 PRIM_CHAR16 = 50 PRIM_CHAR32 = 51 _NUM_PRIM = 52 _UNKNOWN_PRIM = -1 _UNKNOWN_FLOAT_PRIM = -2 _UNKNOWN_LONG_DOUBLE = -3 _IO_FILE_STRUCT = -1 PRIMITIVE_TO_INDEX = { 'char': PRIM_CHAR, 'short': PRIM_SHORT, 'int': PRIM_INT, 'long': PRIM_LONG, 'long long': PRIM_LONGLONG, 'signed char': PRIM_SCHAR, 'unsigned char': PRIM_UCHAR, 'unsigned short': PRIM_USHORT, 'unsigned int': PRIM_UINT, 'unsigned long': PRIM_ULONG, 'unsigned long long': PRIM_ULONGLONG, 'float': PRIM_FLOAT, 'double': PRIM_DOUBLE, 'long double': PRIM_LONGDOUBLE, 'float _Complex': PRIM_FLOATCOMPLEX, 'double _Complex': PRIM_DOUBLECOMPLEX, '_Bool': PRIM_BOOL, 'wchar_t': PRIM_WCHAR, 'char16_t': PRIM_CHAR16, 'char32_t': PRIM_CHAR32, 'int8_t': PRIM_INT8, 'uint8_t': PRIM_UINT8, 'int16_t': PRIM_INT16, 'uint16_t': PRIM_UINT16, 'int32_t': PRIM_INT32, 'uint32_t': PRIM_UINT32, 'int64_t': PRIM_INT64, 'uint64_t': PRIM_UINT64, 'intptr_t': PRIM_INTPTR, 'uintptr_t': PRIM_UINTPTR, 'ptrdiff_t': PRIM_PTRDIFF, 'size_t': PRIM_SIZE, 'ssize_t': PRIM_SSIZE, 'int_least8_t': PRIM_INT_LEAST8, 'uint_least8_t': PRIM_UINT_LEAST8, 'int_least16_t': PRIM_INT_LEAST16, 'uint_least16_t': PRIM_UINT_LEAST16, 'int_least32_t': PRIM_INT_LEAST32, 'uint_least32_t': PRIM_UINT_LEAST32, 'int_least64_t': PRIM_INT_LEAST64, 'uint_least64_t': PRIM_UINT_LEAST64, 'int_fast8_t': PRIM_INT_FAST8, 'uint_fast8_t': PRIM_UINT_FAST8, 'int_fast16_t': PRIM_INT_FAST16, 'uint_fast16_t': PRIM_UINT_FAST16, 'int_fast32_t': PRIM_INT_FAST32, 'uint_fast32_t': PRIM_UINT_FAST32, 'int_fast64_t': PRIM_INT_FAST64, 'uint_fast64_t': PRIM_UINT_FAST64, 'intmax_t': PRIM_INTMAX, 'uintmax_t': PRIM_UINTMAX, } F_UNION = 0x01 F_CHECK_FIELDS = 0x02 F_PACKED = 0x04 F_EXTERNAL = 0x08 F_OPAQUE = 0x10 G_FLAGS = dict([('_CFFI_' + _key, globals()[_key]) for _key in ['F_UNION', 'F_CHECK_FIELDS', 'F_PACKED', 'F_EXTERNAL', 'F_OPAQUE']]) CLASS_NAME = {} for _name, _value in list(globals().items()): if _name.startswith('OP_') and isinstance(_value, int): CLASS_NAME[_value] = _name[3:]