title: Accessing a compiled Python module's AST or symbol table
body: <p><strong>Background:</strong></p>
  
  <p>I have a pure-Python module that defines a few sentinels:</p>
  
  <pre><code>foo = object()
  
  # for backwards compatibility
  bar = foo
  </code></pre>
  
  <p>I'd like to get a mapping between the <code>object</code> instances and the variable names. To do that, I import the module and loop over the variables:</p>
  
  <pre><code>signals = {}
  
  for name, obj in vars(module).items():
      if type(obj) == object:
          signals[obj] = name
  </code></pre>
  
  <p><strong>Problem</strong>:</p>
  
  <p>The order of the variables isn't preserved, so <code>bar</code> ends up wrongly replacing <code>foo</code> in the mapping.</p>
  
  <p>How do I get only the variables defined like <code>foo = object()</code> and not <code>bar = foo</code>?</p>
  
  <p><strong>Somewhat working solutions</strong>:</p>
  
  <p>I know it can be done with the <code>ast</code> module, but my module may not have a corresponding <code>.py</code> file, so <code>inspect.getsource(module)</code> just returns the contents of the <code>pyc</code> file. <code>ast.parse()</code> won't parse that and I don't really want to add a new dependency.</p>
  
  <p>It can also be done with the <code>symtable</code> module via the <code>Symbol.is_referenced()</code> method, but that suffers from the same problem as the <code>ast</code> approach.</p>
  
  <p>Is there an elegant way of doing this without hard-coding the mapping?</p>