title: importlib._bootstrap and python interpreter initialization
body: <p>CPython interpreter modules import system is written in python itself and is initialized in <code>importlib._bootstrap</code>. Its source can be seen in <code>importlib/_bootstrap.py</code>, but it is not loaded from there. Instead it is loaded from so called frozen module, which bytecode is <a href="https://github.com/python/cpython/blob/master/Python/frozen.c#L31L45" rel="nofollow noreferrer">baked</a> into the interpreter library (<code>python37.dll</code>).</p>
  
  <p>The interpreter initializes itself by <a href="https://github.com/python/cpython/blob/master/Python/pylifecycle.c#L186" rel="nofollow noreferrer">importing <code>_frozen_importlib</code> (it's a name for <code>importlib._bootstrap</code> baked into the interpreter)</a> and <a href="https://github.com/python/cpython/blob/master/Python/pylifecycle.c#L217" rel="nofollow noreferrer">calling <code>_install</code> function defined there, passing <code>sys</code> and <code>_imp</code> modules as arguments</a>. <a href="https://github.com/python/cpython/blob/master/Lib/importlib/_bootstrap.py#L1158" rel="nofollow noreferrer"><code>_install</code></a> calls <a href="https://github.com/python/cpython/blob/master/Lib/importlib/_bootstrap.py#L1123L1146" rel="nofollow noreferrer"><code>_setup</code></a>, which initializes runtime needed for this module, and then adds 2 importers, implementing <a href="https://www.python.org/dev/peps/pep-0451/" rel="nofollow noreferrer">PEP 451</a>.</p>
  
  <p>When developing a custom importer I have encountered several bugs either in <code>_bootstrap.py</code> or in my importer, so I need to add some debug output into <code>_bootstrap.py</code>.</p>
  
  <p>So I try to make the interpreter to load <code>_bootstrap.py</code> from disk, not from baked-in bytecode.</p>
  
  <p>Analysis of cpython source code revealed that in order to import builtin modules I don't need the most of spec class. So builtin modules can be imported using <code>_create_builtin</code></p>
  
  <pre class="lang-py prettyprint-override"><code>class UltraSimpleSpec:
      __slots__=("name",)
      def __init__(self, name):
         self.name = name
  
  def _install(sys, _imp):
      io = _imp.create_builtin(UltraSimpleSpec("_io"))
      _bi = _imp.create_builtin(UltraSimpleSpec("builtins"))
      fd = io.open("path/to/_bootstrap.py", "rt") # exits the _install function without any exception, how can it do it?
      raise _bi.Exception("Never called") # _bi.print doesn't work, because sys.stdout is not initialized, so I have to use exceptions for debug output
      ...
  </code></pre>
  
  <p>Unfortunately something strange happens and call to io.open causes <code>_install</code> to exit, control flow never reaches the next statement. And no exception seems to be raised, when it is, interpreter prints it, instead an exception is raised in another place because of missing side effect of <code>_install</code>.</p>
  
  <p>Yet ahother nuisance is that <code>sys.stdout</code> is not initialized, so we cannot <code>print</code> and have to rely on exceptions for debug output.</p>
  
  <p>So</p>
  
  <ol>
  <li><p>How should I make <code>open</code> work?</p></li>
  <li><p>How should I initialize <code>sys.stdout</code>?</p></li>
  </ol>