title: What's with the integer cache maintained by the interpreter?
body: <p>After dive into Python's source code, I find out that it maintains an array of <code>PyInt_Object</code>s ranging from <code>int(-5)</code> to <code>int(256)</code> (@src/Objects/intobject.c)</p>
  <p>A little experiment proves it:</p>
  <pre><code>&gt;&gt;&gt; a = 1
  &gt;&gt;&gt; b = 1
  &gt;&gt;&gt; a is b
  True
  &gt;&gt;&gt; a = 257
  &gt;&gt;&gt; b = 257
  &gt;&gt;&gt; a is b
  False
  </code></pre>
  <p>But if I run those code together in a py file (or join them with semi-colons), the result is different:</p>
  <pre><code>&gt;&gt;&gt; a = 257; b = 257; a is b
  True
  </code></pre>
  <p>I'm curious why they are still the same object, so I digg deeper into the syntax tree and compiler, I came up with a calling hierarchy listed below:</p>
  <pre class="lang-none prettyprint-override"><code>PyRun_FileExFlags() 
      mod = PyParser_ASTFromFile() 
          node *n = PyParser_ParseFileFlagsEx() //source to cst
              parsetoke() 
                  ps = PyParser_New() 
                  for (;;)
                      PyTokenizer_Get() 
                      PyParser_AddToken(ps, ...)
          mod = PyAST_FromNode(n, ...)  //cst to ast
      run_mod(mod, ...)
          co = PyAST_Compile(mod, ...) //ast to CFG
              PyFuture_FromAST()
              PySymtable_Build()
              co = compiler_mod()
          PyEval_EvalCode(co, ...)
              PyEval_EvalCodeEx()
  </code></pre>
  <p>Then I added some debug code in <code>PyInt_FromLong</code> and before/after <code>PyAST_FromNode</code>, and executed a test.py:</p>
  <pre><code>a = 257
  b = 257
  print &quot;id(a) = %d, id(b) = %d&quot; % (id(a), id(b))
  </code></pre>
  <p>the output looks like:</p>
  <pre class="lang-none prettyprint-override"><code>DEBUG: before PyAST_FromNode
  name = a
  ival = 257, id = 176046536
  name = b
  ival = 257, id = 176046752
  name = a
  name = b
  DEBUG: after PyAST_FromNode
  run_mod
  PyAST_Compile ok
  id(a) = 176046536, id(b) = 176046536
  Eval ok
  </code></pre>
  <p>It means that during the <code>cst</code> to <code>ast</code> transform, two different <code>PyInt_Object</code>s are created (actually it's performed in the <code>ast_for_atom()</code> function), but they are later merged.</p>
  <p>I find it hard to comprehend the source in <code>PyAST_Compile</code> and <code>PyEval_EvalCode</code>, so I'm here to ask for help, I'll be appreciative if some one gives a hint?</p>