o ,]_l@sdZddlmZmZmZddlZddlZddlZddlZddl Z ddl Z ddl Z e dZ eejZeZeee ee ejddlmZmZegdZddd d d d d ddddddd Zeeeee@dksuJgdZGddde Z!Gddde Z"ereeeeej#@dksJddZ$ddZ%ddZ&Gd d!d!e Z'd"d#Z(d$d%Z)d&d'Z*d(d)Z+d=d+d,Z,d-d.Z-d/d0Z.e/ed1sie_0d2d3Z1d=d4d5Z2d6d7Z3Gd8d9d9e Z4gd:Z5d;d<Z6dS)>a Python 3 reorganized the standard library (PEP 3108). This module exposes several standard library modules to Python 2 under their new Python 3 names. It is designed to be used as follows:: from future import standard_library standard_library.install_aliases() And then these normal Py3 imports work on both Py3 and Py2:: import builtins import copyreg import queue import reprlib import socketserver import winreg # on Windows only import test.support import html, html.parser, html.entites import http, http.client, http.server import http.cookies, http.cookiejar import urllib.parse, urllib.request, urllib.response, urllib.error, urllib.robotparser import xmlrpc.client, xmlrpc.server import _thread import _dummy_thread import _markupbase from itertools import filterfalse, zip_longest from sys import intern from collections import UserDict, UserList, UserString from collections import OrderedDict, Counter, ChainMap # even on Py2.6 from subprocess import getoutput, getstatusoutput from subprocess import check_output # even on Py2.6 (The renamed modules and functions are still available under their old names on Python 2.) This is a cleaner alternative to this idiom (see http://docs.pythonsprints.com/python3_porting/py-porting.html):: try: import queue except ImportError: import Queue as queue Limitations ----------- We don't currently support these modules, but would like to:: import dbm import dbm.dumb import dbm.gnu import collections.abc # on Py33 import pickle # should (optionally) bring in cPickle on Python 2 )absolute_importdivisionprint_functionN future_stdlib)PY2PY3)testurllibpickledbmbuiltinscopyregqueue socketserver configparserreprlibwinreg_thread _dummy_threadxmlrpchtmlhttp _markupbase) __builtin__copy_regQueuezfuture.moves.socketserver ConfigParserrepr_winregthread dummy_threadzfuture.moves.xmlrpczfuture.moves.htmlzfuture.moves.httpzfuture.moves._markupbase)) collectionsUserListr"r")r!UserDictr#r#)r! UserStringr$r$r!ChainMapfuture.backports.miscr&) itertools filterfalser( ifilterfalse)r( zip_longestr( izip_longest)sysinternrr.)reASCIIstatST_MODE)base64 encodebytesr3 encodestring)r3 decodebytesr3 decodestring) subprocess getoutputcommandsr9)r8getstatusoutputr:r;)r8 check_outputr'r<)mathceilr'r>)r! OrderedDictr'r?)r!Counterr'r@r%)r(countr'rA)rrecursive_reprr'rB) functools cmp_to_keyr'rDc@s8eZdZdZdZddZd ddZdd Zd d d ZdS) RenameImportzX A class for import hooks mapping Py3 module names etc. to the Py2 equivalents. TcCsj||_t|t|@}t|dkr#tt|t|ks'Jdtdd|D|_dS)z Pass in a dictionary-like object mapping from old names to new names. E.g. {'ConfigParser': 'configparser', 'cPickle': 'pickle'} rz/Ambiguity in renaming (handler not implemented)css|] \}}||fVqdSN).0oldnewrGrGB/usr/lib/python3/dist-packages/future/standard_library/__init__.py sz(RenameImport.__init__..N) old_to_newsetkeysvalueslendictitems new_to_old)selfrMbothrGrGrK__init__s zRenameImport.__init__NcCs$tdd|jD}||vr|SdS)NcSsg|] }|ddqS).r)split)rHsrGrGrK sz,RenameImport.find_module..)rNrT)rUfullnamepathnew_base_namesrGrGrK find_moduleszRenameImport.find_modulecCsPd}|tjvr tj|S||jvr|j|}||}n||}|tj|<|SrF)r-modulesrT_find_and_load_module)rUnamer]oldnamemodulerGrGrK load_module s       zRenameImport.load_modulecCs|d}t|dkrD|d}|||}z|j}Wn"ty=td||t j vr6t j |YStdYnwt|dks |d}t ||}t j |g|RS)zb Finds and loads it. But if there's a . in the name, handles it properly. rXrzPackage {0} has no __path__.zWhat to do here?)rYrQpopra__path__AttributeErrorflogdebugformatr-r`impr_re)rUrbr]bits packagenamepackage module_inforGrGrKras         z"RenameImport._find_and_load_modulerF) __name__ __module__ __qualname____doc__RENAMERrWr_rerarGrGrGrKrEs rEc@ eZdZdZddZddZdS)hooksa Acts as a context manager. Saves the state of sys.modules and restores it after the 'with' block. Use like this: >>> from future import standard_library >>> with standard_library.hooks(): ... import http.client >>> import requests For this to work, http.client will be scrubbed from sys.modules after the 'with' block. That way the modules imported in the 'with' block will continue to be accessible in the current namespace but not from any imported modules (like requests). cCs ttj|_t|_t|SrF)copyr-r`old_sys_modules detect_hookshooks_were_installed install_hooksrUrGrGrK __enter__Aszhooks.__enter__cGs|jstdSdSrF)r| remove_hooksrUargsrGrGrK__exit__Is zhooks.__exit__NrrrsrtrurrrGrGrGrKrx0s rxcCstrdSdtjvr,tjtjtjg}dd|D}tt|dks't d||dt_ |j t jvr4dSt|d rPtj|j}|dtj rPd |dvrPdSdS) z Tries to infer whether the module m is from the Python 2 standard library. This may not be reliable on all systems. F stdlib_pathcSsg|] }tj|dqS)r)osr]rY)rHfrGrGrKr[_sz(is_py2_stdlib_module..rfz>> from future import standard_library >>> standard_library.install_hooks() >>> import http.client >>> # ... >>> with standard_library.suspend_hooks(): >>> import requests # incompatible with ``future``'s standard library hooks If the hooks were disabled before the context, they are not installed when the context is left. cCst|_t|SrF)r{r|rr~rGrGrKrszsuspend_hooks.__enter__cGs|jrtdSdSrF)r|r}rrGrGrKrs zsuspend_hooks.__exit__NrrGrGrGrKrs rcCsHttjt|@}t|dkrt|d}td|tj|dS)zp Add any previously scrubbed modules back to the sys.modules cache, but only if it's safe to do so. rz(future module {} clashes with Py2 moduleN)rNr-r`rQlist ImportErrorrlupdate)rclashfirstrGrGrKrestore_sys_moduless  rcCstrdStD]#\}}}}t|tj|}t|tj|}t||}t|||qddl}ddlm }ddlm } ddlm } ddlm } ddlm } ||_ | |_ | |_ | |_ | |_ |tjd<| tjd <| tjd <| tjd <| tjd <zddl} Wn tyYnwzdd lm}Wn tyYn w|| _|tjd<zddl}Wn tyYdSwddlm}||_|tjd<zddlm}Wn tyYn w||_|tjd<zddlm}Wn tyYdSw||_|tjd<dS)zm Monkey-patches the standard library in Py2.6/7 to provide aliases for better Py3 compatibility. Nr)request)response)parse)error) robotparserzurllib.requestzurllib.responsez urllib.parsez urllib.errorzurllib.robotparser)supportz test.support)dumbzdbm.dumb)gnuzdbm.gnu)ndbmzdbm.ndbm)rMOVES __import__r-r`getattrsetattrr future.backports.urllibrrrrrrrfuture.moves.testrr future.moves.dbmrrr) newmodname newobjname oldmodname oldobjnamenewmodoldmodobjr rrrrrrrr rrrrGrGrKinstall_aliasessv                        rcCsZtrdSttdtjtdtt}t s"tj |tdtjdS)z` This function installs the future.standard_library import hook into sys.meta_path. Nzsys.meta_path was: {0}zInstalling hooks ...zsys.meta_path is now: {0}) rrrjrkrlr- meta_pathrErr{append)newhookrGrGrKr} s  r}cC tdS)z_ Deprecated. Use install_hooks() instead. This will be removed by ``future`` v1.0. N)r}rGrGrGrK enable_hooks rFcCsXtrdStdtttjdddD] \}}t|dr"tj|=q|r*tdSdS)zC This function removes the import hook from sys.meta_path. NzUninstalling hooks ...rv) rrjrkr enumerater-rrr)scrub_sys_modulesihookrGrGrKr's    rcCr)z^ Deprecated. Use remove_hooks() instead. This will be removed by ``future`` v1.0. N)rrGrGrGrK disable_hooks;rrcCs>tdtddtjD}|rtd|Std|S)zG Returns True if the import hooks are installed, False if not. zDetecting hooks ...cSsg|]}t|dqS)rv)r)rHrrGrGrKr[Hz detect_hooks..z Detected.z Not detected.)rjrkanyr-r)presentrGrGrKr{Cs   r{ py2_modulescCsVttjdkr dStrJddl}|tjd<ddl}|tjd<ddl}|tjd<dS)z Currently this function is unneeded, as we are not attempting to provide import hooks for modules with ambiguous names: email, urllib, pickle. rNr emailr )rQr-rr{r rr )r rr rGrGrKcache_py2_modulesXs   rc Csddl}tr t|S|rd}nd}|d|d}g}t|D]\}}d|d|d}|||q!tt t|D]\}}|dkrN|dSt ||d|||qA|dS)a Pass a (potentially dotted) module name of a Python 3 standard library module. This function imports the module compatibly on Py2 and Py3 and returns the top-level module. Example use: >>> http = import_('http.client') >>> http = import_('http.server') >>> urllib = import_('urllib.request') Then: >>> conn = http.client.HTTPConnection(...) >>> response = urllib.request.urlopen('http://mywebsite.com') >>> # etc. Use as follows: >>> package_name = import_(module_name) On Py3, equivalent to this: >>> import module_name On Py2, equivalent to this if backport=False: >>> from future.moves import module_name or to this if backport=True: >>> from future.backports import module_name except that it also handles dotted module names such as ``http.client`` The effect then is like this: >>> from future.backports import module >>> from future.backports.module import submodule >>> module.submodule = submodule Note that this would be a SyntaxError in Python: >>> from future.backports import http.client rNfuture.backports future.movesrXrf) importlibrrrYrjoinr import_modulereversedrr) module_namebackportrprefixpartsr`rpartsofarrGrGrKimport_ts".rcsztrt|Sd|vrt|drd}nd}|d|d}t|d|fdd|D}t|dkr;|dS|S) aa Example use: >>> HTTPConnection = from_import('http.client', 'HTTPConnection') >>> HTTPServer = from_import('http.server', 'HTTPServer') >>> urlopen, urlparse = from_import('urllib.request', 'urlopen', 'urlparse') Equivalent to this on Py3: >>> from module_name import symbol_names[0], symbol_names[1], ... and this on Py2: >>> from future.moves.module_name import symbol_names[0], ... or: >>> from future.backports.module_name import symbol_names[0], ... except that it also handles dotted module names such as ``http.client``. rrrrXcsg|]}t|qSrG)r)rHrbrdrGrKr[rzfrom_import..rfr)rrboolrYrrrQ)r symbol_nameskwargsrroutputrGrrK from_imports rc@s(eZdZdZddZddZddZdS) exclude_local_folder_importsaZ A context-manager that prevents standard library modules like configparser from being imported from the local python-future source folder on Py3. (This was need prior to v0.16.0 because the presence of a configparser folder would otherwise have prevented setuptools from running on Py3. Maybe it's not needed any more?) cGs6t|dksJ||_tdd|jDrtddS)NrcSsg|]}d|vqS)rXrG)rHrrGrGrKr[sz9exclude_local_folder_imports.__init__..z%Dotted module names are not supported)rQ module_namesrNotImplementedErrorrrGrGrKrWs z%exclude_local_folder_imports.__init__c sttj|_ttj|_tjddkrdSgd}|jD]tfdd|Dr1tjq|j D]}zt |dd}Wq5t yHYq5wdS)Nr)futurepast libfuturize libpasteurizer cs"g|] }tjtj|qSrG)rr]existsr)rH subfolderfolderrGrKr[sz:exclude_local_folder_imports.__enter__..)level) ryr-r] old_sys_pathr`rz version_infoallremoverrr)rUFUTURE_SOURCE_SUBFOLDERSrrdrGrrKrs$     z&exclude_local_folder_imports.__enter__cGs>|jt_t|jttjD] }|j|tj|<qdSrF)rr-r]rNrzrOr`)rUrrrGrGrKrs z%exclude_local_folder_imports.__exit__N)rrrsrtrurWrrrGrGrGrKrs  r)r r rrrrrrtkinterrrrrrc CsVtttD]}zt|WqtyYqwWddS1s$wYdSrF)rTOP_LEVEL_MODULESrr)rrGrGrKimport_top_level_modules)s   "r)F)7ru __future__rrrr-loggingrmrtypesryr getLoggerrj Formatter BASIC_FORMAT _formatter StreamHandler_handler setFormatter addHandlersetLevelWARN future.utilsrrrNrrrQrProbjectrErxrrrrrrrr}rrrr{rrrrrrrrrGrGrGrKsp<       C 6F""P   I&6