mitogen icon indicating copy to clipboard operation
mitogen copied to clipboard

core: python3: need mechanism to avoid stdlib imports

Open dw opened this issue 7 years ago • 4 comments

See also: #16

Given 'import urlparse' running on a 3.x target, this should raise ImportError. Instead it is forwarded to the 2.x host which then attempts to serve it, resulting in a syntax error.

python3 branch has a check within ModuleResponder to refuse any import from the standard library, but this costs a network round-trip. Need a complete list of renamed stdlib modules in core.py, and to select between them at startup. The inverse of this situation is true -- 2.x target will attempt to import 3.x host's stdlib too.

https://www.python.org/dev/peps/pep-3108/#previously-deprecated-done

dw avatar Jun 11 '18 00:06 dw

Python 3.x ships _compat_pickle, which maps many (all?) renamed modules. The file comments say it's a copy of lib2to3.fixes.fix_imports. I haven't checked how much the two have diverged.

moreati avatar Oct 03 '18 18:10 moreati

O Pickle the thorn in my side, O Pickle the solution in my hand! Really awesome find :) I'm not sure I quite grok the format, but it looks like the child may just need a copy of the IMPORT_MAPPING dict keys.

I'm a bit foggy just now, but I /think/ the same list (or perhaps a transform of it) can be used for the other direction too? 2.x child -> 3.x host

dw avatar Oct 03 '18 18:10 dw

>>> import _compat_pickle
>>> python2_modules = sorted(_compat_pickle.IMPORT_MAPPING.keys())
>>> '|'.join(python2_modules)
'BaseHTTPServer|CGIHTTPServer|ConfigParser|Cookie|Dialog|DocXMLRPCServer|FileDialog|HTMLParser|Queue|ScrolledText|SimpleDialog|SimpleHTTPServer|SimpleXMLRPCServer|SocketServer|StringIO|Tix|Tkconstants|Tkdnd|Tkinter|UserDict|UserList|UserString|__builtin__|_abcoll|_elementtree|_winreg|anydbm|cPickle|cStringIO|commands|cookielib|copy_reg|dbhash|dbm|dumbdbm|dummy_thread|gdbm|htmlentitydefs|httplib|markupbase|repr|robotparser|test.test_support|thread|tkColorChooser|tkCommonDialog|tkFileDialog|tkFont|tkMessageBox|tkSimpleDialog|ttk|urllib2|urlparse|whichdb|xmlrpclib'

Running that through regex-opt gives

__builtin__|dbhash|ttk|urllib2|(?:rep|(?:Tkint|(?:Socket|(?:CGI|(?:Bas|Simpl)e)HTTP|(?:Doc|Simple)XMLRPC)Serv|(?:tkColorChoo|(?:robotp|(?:Config|HTML)P)ar)s)e)r|(?:Cooki|Queu|cPickl|(?:markupba|urlpar)s)e|(?:UserStrin|(?:tkCommon|(?:tk)?(?:Fi|Simp)le)?Dialo|(?:_win|copy_)re)g|(?:ScrolledTex|User(?:Dic|Lis)|t(?:est.test_suppor|kFon))t|c?StringIO|(?:Ti|tkMessageBo)x|(?:Tkconstant|command|htmlentitydef)s|(?:Tkdn|(?:dummy_)?threa)d|(?:any|dumb|g)?dbm|(?:whichd|(?:cookie|http|xmlrpc)li)b|_(?:abcoll|elementtree)

moreati avatar Oct 03 '18 18:10 moreati

Alternatively https://github.com/jackmaney/python-stdlib-list has lists for Python 2.6-2.7 and 3.2-3.6

moreati avatar Oct 03 '18 19:10 moreati