hello,could we make modules work more like this...tl;dr: __code__ object available (potentially a __self__ object), reload by calling, makes working with modules more natural/safe!ENCLOSED: modules-next.py
--
C Anthony#----------------------------------------------------------------------------------------( modules-next.py )# encoding: utf-8## modules-next## C Anthony Risingerimport sysm_name = 'modules_next'm_source = r"""\if __name__ == '{0}':from . import __call__ as reexecfrom . import __code__ as codefrom . import __dict__ as nsprint('>>> {{0}}: {{1}}\n{{2}}\n'.format(__name__, code, ns.keys()))__name__ = 'modules_next_reloaded'reexec()print('>>> {{0}}: {{1}}\n{{2}}\n'.format(__name__, code, ns.keys()))else:print('!!! __name__ == {{0}}\n'.format(__name__))
def throw():raise Exception(__name__)""".format(m_name)m_code = compile(m_source, m_name, 'exec')m_dict = {'__file__': __file__ + '.next','__name__': m_name,'__path__': list(),'__doc__': None,}#...bind namespace/functionsmodule = eval('lambda: None', m_dict)#...automatically expose as attributesmodule.__dict__ = module.__globals__#...must be set twice, __code__ sees __dict__, but we see descriptormodule.__name__ = m_name#...redefine the function body!module.__code__ = m_code#...yay! we haz legitimate module!sys.modules[m_name] = moduleprint('--* importing...')import modules_nextprint('>>> {0}: {1}\n{2}\n'.format(__name__,modules_next.__code__,modules_next.__dict__.keys(),))print('--* importing (for real this time)...')#...import/reload by calling our func-y-modulemodule()print('>>> {0}: {1}\n{2}\n'.format(__name__,modules_next.__code__,modules_next.__dict__.keys(),))print('--* throwing...')modules_next.throw()