3 jfB:+@sdZddlZejdFkr ddlZnddlZddlZddlZddlZddlm Z ddl m Z ddl m Z yddlmZmZWnek rdZeZYnXejdd dGkrddlZejeZejejee jee jee jee jZd d d ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-g#ZGd.d/d/Zd0d1Z d2d3Z!d4d5Z"d6d7Z#Gd8d9d9eZ$Gd:d;d;eZ%e%Z&fffdd?Z(d@dAZ)GdBdCdCeZ*GdDdEdEeZ+e'Z,dS)Ha' past.translation ================== The ``past.translation`` package provides an import hook for Python 3 which transparently runs ``futurize`` fixers over Python 2 code on import to convert print statements into functions, etc. It is intended to assist users in migrating to Python 3.x even if some dependencies still only support Python 2.x. Usage ----- Once your Py2 package is installed in the usual module search path, the import hook is invoked as follows: >>> from past.translation import autotranslate >>> autotranslate('mypackagename') Or: >>> autotranslate(['mypackage1', 'mypackage2']) You can unregister the hook using:: >>> from past.translation import remove_hooks >>> remove_hooks() Author: Ed Schofield. Inspired by and based on ``uprefix`` by Vinay M. Sajip. N) ParseError)RefactoringTool)fixes) PathFinderSourceFileLoaderzlib2to3.fixes.fix_applyzlib2to3.fixes.fix_exceptzlib2to3.fixes.fix_execfilezlib2to3.fixes.fix_exitfunczlib2to3.fixes.fix_funcattrszlib2to3.fixes.fix_filterzlib2to3.fixes.fix_has_keyzlib2to3.fixes.fix_idiomszlib2to3.fixes.fix_importzlib2to3.fixes.fix_internzlib2to3.fixes.fix_isinstancezlib2to3.fixes.fix_methodattrszlib2to3.fixes.fix_nezlib2to3.fixes.fix_numliteralszlib2to3.fixes.fix_parenzlib2to3.fixes.fix_printzlib2to3.fixes.fix_raisezlib2to3.fixes.fix_renameszlib2to3.fixes.fix_reducezlib2to3.fixes.fix_reprzlib2to3.fixes.fix_standarderrorzlib2to3.fixes.fix_sys_exczlib2to3.fixes.fix_throwzlib2to3.fixes.fix_tuple_paramszlib2to3.fixes.fix_typeszlib2to3.fixes.fix_ws_commazlib2to3.fixes.fix_xreadlineszlib2to3.fixes.fix_basestringzlib2to3.fixes.fix_execzlib2to3.fixes.fix_getcwduzlib2to3.fixes.fix_longzlib2to3.fixes.fix_nextzlib2to3.fixes.fix_nonzerozlib2to3.fixes.fix_raw_inputzlib2to3.fixes.fix_xrangec@s8eZdZdZdZdZdZdZeddZ eddZ dS)RTsa& A namespace for the refactoring tools. This avoids creating these at the module level, which slows down the module import. (See issue #117). There are two possible grammars: with or without the print statement. Hence we have two possible refactoring tool implementations. NcCs.dtjtjgkr*ttt_ttddit_dS)zj Call this before using the refactoring tools to create them on demand if needed. Nprint_functionT)r _rt_rtprmyfixesrr;/tmp/pip-build-s1i5078x/future/past/translation/__init__.pysetups z RTs.setupcCs.dtjtjgkr*ttt_ttddit_dS)zj Call this before using the refactoring tools to create them on demand if needed. Nr T)r _rt_py2_detect_rtp_py2_detectrpy2_detect_fixersrrrrsetup_detect_python2s zRTs.setup_detect_python2) __name__ __module__ __qualname____doc__r rrr staticmethodrrrrrrr s r cCsrg}xhtjj|}|d|kr2|jd|dPq|d|krR|jd|dPq|d}|jd|dqW|S)zA Split a path into all components. From Python Cookbook. r)ospathsplitinsert)rZallpartspartsrrrsplitalls   r"cCsLg}t|}t|}x*t||D]\}}||kr2P|j|q Wtjj|S)z^ Returns the longest common substring to the two strings, starting from the left. )r"zipappendrrjoin)s1s2chunkspath1path2Zdir1Zdir2rrrcommon_substringsr+cCstjytjj||}WnFtk r`}z*|jdks@|jdkrBtjj||}WYdd}~XnX|t|dd krt j dj |dSt j dj |dSdS) zD Returns a bool indicating whether we think the code is Py2 z bad input=NrzDetected Python 2 code: {0}TzDetected Python 3 code: {0}F) r rrrefactor_stringrmsgvaluerstrloggerdebugformat)sourcepathnametreeerrrdetect_python2s r9cCsztj|d7}ytjj||}WnFtk rh}z*|jdksH|jdkrJtjj||}WYdd}~XnXt|ddS)N z bad inputr,rr-) r rr r.rr/r0rr1)r5r6r7r8rrr transforms r;cs@eZdZgZgZddZddZfddZfddZZ S) PastSourceFileLoadercsJ|jtfdd|jDr$d}n"tfdd|jDrBd}nd}|S)Nc3s|]}j|VqdS)N) startswith).0r)fullnamerr sz7PastSourceFileLoader._convert_needed..Fc3s|]}j|VqdS)N)r=)r>r)r?rrr@ sT)nameany exclude_paths include_paths)selfconvertr)r?r_convert_needed sz$PastSourceFileLoader._convert_neededcCsB|j|j}|j}t||r&t||}t||d}t||jdS)Nexec) get_sourcerArr9r;compilerH__dict__)rEmoduler5r6coderrr_exec_transformed_modules     z-PastSourceFileLoader._exec_transformed_modulecstjd||tjkr"tj|}nv|jrtjd|tj|}|tj|<|j|_||_ |j |rpg|_ ||_ n|j dd|_ |j|n tj|}|S)NzRunning load_module for %szAutoconverting %s.r)r2r3sysmodulesrGimp new_moduler__file__ __loader__ is_package__path__ __package__ rpartitionrNsuper load_module)rEr?mod) __class__rrr[s          z PastSourceFileLoader.load_modulecs>tjd||jr.tjd|j|j|n tj|dS)NzRunning exec_module for %szAutoconverting %s)r2r3rGrArNrZ exec_module)rErL)r]rrr^4s   z PastSourceFileLoader.exec_module) rrrrCrDrGrNr[r^ __classcell__rr)r]rr<s   r<c@s@eZdZdZdZddZddZddZdd d Zdd d Z d S)Py2Fixerzi An import hook class that uses lib2to3 for source-to-source translation of Py2 code to Py3. TcCs(d|_ddg|_tj|j|_g|_dS)NfutureZpast)foundZbase_exclude_pathscopyrCrD)rErrr__init__Hs zPy2Fixer.__init__cCs|j|7_dS)z Pass in a sequence of module names such as 'plotrique.plotting' that, if present at the leftmost side of the full package name, would specify the module to be transformed from Py2 to Py3. N)rD)rEpathsrrrincludeNszPy2Fixer.includecCs|j|7_dS)z Pass in a sequence of strings such as 'mymodule' that, if present at the leftmost side of the full package name, would cause the module not to undergo any source transformation. N)rC)rErerrrexcludeVszPy2Fixer.excludeNcCsHtjd||tj||}|s.tjd|dSt|_|j|_|j|_|S)NzRunning find_module: (%s, %s)zPy2Fixer could not find %s)r2r3r find_moduler<r]rCrD)rEr?rloaderrrrrh_s  zPy2Fixer.find_modulecCsRtjd|||tj|||}|s2tjd|dSt|j_|j|j_|j|j_|S)NzRunning find_spec: (%s, %s, %s)zPy2Fixer could not find %s) r2r3r find_specr<rir]rCrD)rEr?rtargetspecrrrrjks   zPy2Fixer.find_spec)N)NN) rrrrZPY2FIXERrdrfrgrhrjrrrrr`=s r`cCs~t|tr|f}t|tr |f}t|t|dks>> from past import translation >>> with translation.hooks(): ... import mypy2module >>> import requests # py2/3 compatible anyway >>> # etc. cCst|_t|S)N)rwhooks_were_installedrt)rErrr __enter__szhooks.__enter__cGs|js tdS)N)ryrv)rEargsrrr__exit__szhooks.__exit__N)rrrrrzr|rrrrrxs rxc@s eZdZdZddZddZdS) suspend_hooksax Acts as a context manager. Use like this: >>> from past import translation >>> translation.install_hooks() >>> import http.client >>> # ... >>> with translation.suspend_hooks(): >>> import requests # or others that support Py2/3 If the hooks were disabled before the context, they are not installed when the context is left. cCst|_t|S)N)rwryrv)rErrrrzszsuspend_hooks.__enter__cGs|jr tdS)N)ryrt)rEr{rrrr|szsuspend_hooks.__exit__N)rrrrrzr|rrrrr}s r})rr)rr )-rrPrq importlibrRloggingrrcZlib2to3.pgen2.parserlib2to3.refactorrZ libfuturizerimportlib.machineryrr ImportErrorobject getLoggerrr2setLevelDEBUGlistZlibfuturize_fix_names_stage1Zlib2to3_fix_names_stage1Zlibfuturize_fix_names_stage2Zlib2to3_fix_names_stage2rrr r"r+r9r;r<r`rprtrvrwrxr}Z autotranslaterrrr!s         58: