title: Building Self-Referencing Tuples
body: <p>After seeing a conversation in a forum from many years ago that was never resolved, it caused me to wonder how one would correctly create a tuple that referenced itself. Technically, this is a very bad idea since tuples are supposed to be immutable. How could an immutable object possibly contain itself? However, this question is not about best practices but is a query regarding what is possible in Python.</p>
  
  <pre><code>import ctypes
  
  def self_reference(array, index):
      if not isinstance(array, tuple):
          raise TypeError('array must be a tuple')
      if not isinstance(index, int):
          raise TypeError('index must be an int')
      if not 0 &lt;= index &lt; len(array):
          raise ValueError('index is out of range')
      address = id(array)
      obj_refcnt = ctypes.cast(address, ctypes.POINTER(ctypes.c_ssize_t))
      obj_refcnt.contents.value += 1
      if ctypes.cdll.python32.PyTuple_SetItem(ctypes.py_object(array),
                                              ctypes.c_ssize_t(index),
                                              ctypes.py_object(array)):
          raise RuntimeError('PyTuple_SetItem signaled an error')
  </code></pre>
  
  <p>The previous function was designed to access the C API of Python while keeping internal structures and datatypes in mind. However, the following error is usually generated when running the function. Through unknown processes, it has been possible to create a self-referencing tuple via similar techniques before. </p>
  
  <p><strong>Question:</strong> How should the function <code>self_reference</code> be modified to consistently work all of the time?</p>
  
  <pre><code>&gt;&gt;&gt; import string
  &gt;&gt;&gt; a = tuple(string.ascii_lowercase)
  &gt;&gt;&gt; self_reference(a, 2)
  Traceback (most recent call last):
    File "&lt;pyshell#56&gt;", line 1, in &lt;module&gt;
      self_reference(a, 2)
    File "C:/Users/schappell/Downloads/srt.py", line 15, in self_reference
      ctypes.py_object(array)):
  WindowsError: exception: access violation reading 0x0000003C
  &gt;&gt;&gt; 
  </code></pre>
  
  <p><strong>Edit:</strong> Here are two different conversations with the interpreter that are somewhat confusing. The code up above appears to be correct if I understand the documentation correctly. However, the conversations down below appear to both conflict with each other and the <code>self_reference</code> function up above.</p>
  
  <p><strong>Conversation 1:</strong></p>
  
  <pre><code>Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)]
  on win32
  Type "copyright", "credits" or "license()" for more information.
  &gt;&gt;&gt; from ctypes import *
  &gt;&gt;&gt; array = tuple(range(10))
  &gt;&gt;&gt; cast(id(array), POINTER(c_ssize_t)).contents.value
  1
  &gt;&gt;&gt; cast(id(array), POINTER(c_ssize_t)).contents.value += 1
  &gt;&gt;&gt; cast(id(array), POINTER(c_ssize_t)).contents.value
  2
  &gt;&gt;&gt; array
  (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  &gt;&gt;&gt; cdll.python32.PyTuple_SetItem(c_void_p(id(array)), 0,
                                    c_void_p(id(array)))
  Traceback (most recent call last):
    File "&lt;pyshell#6&gt;", line 1, in &lt;module&gt;
      cdll.python32.PyTuple_SetItem(c_void_p(id(array)), 0,
                                    c_void_p(id(array)))
  WindowsError: exception: access violation reading 0x0000003C
  &gt;&gt;&gt; cdll.python32.PyTuple_SetItem(c_void_p(id(array)), 0,
                                    c_void_p(id(array)))
  Traceback (most recent call last):
    File "&lt;pyshell#7&gt;", line 1, in &lt;module&gt;
      cdll.python32.PyTuple_SetItem(c_void_p(id(array)), 0,
                                    c_void_p(id(array)))
  WindowsError: exception: access violation reading 0x0000003C
  &gt;&gt;&gt; array
  (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  &gt;&gt;&gt; cdll.python32.PyTuple_SetItem(c_void_p(id(array)), 0,
                                    c_void_p(id(array)))
  0
  &gt;&gt;&gt; array
  ((&lt;NULL&gt;, &lt;code object __init__ at 0x02E68C50, file "C:\Python32\lib
  kinter\simpledialog.py", line 121&gt;, &lt;code object destroy at 0x02E68CF0,
  file "C:\Python32\lib   kinter\simpledialog.py", line 171&gt;, &lt;code object
  body at 0x02E68D90, file "C:\Python32\lib      kinter\simpledialog.py",
  line 179&gt;, &lt;code object buttonbox at 0x02E68E30, file "C:\Python32\lib
  kinter\simpledialog.py", line 188&gt;, &lt;code object ok at 0x02E68ED0, file
  "C:\Python32\lib        kinter\simpledialog.py", line 209&gt;, &lt;code object
  cancel at 0x02E68F70, file "C:\Python32\lib    kinter\simpledialog.py",
  line 223&gt;, &lt;code object validate at 0x02E6F070, file "C:\Python32\lib
  kinter\simpledialog.py", line 233&gt;, &lt;code object apply at 0x02E6F110, file
  "C:\Python32\lib     kinter\simpledialog.py", line 242&gt;, None), 1, 2, 3, 4,
  5, 6, 7, 8, 9)
  &gt;&gt;&gt;
  </code></pre>
  
  <p><strong>Conversation 2:</strong></p>
  
  <pre><code>Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)]
  on win32
  Type "copyright", "credits" or "license()" for more information.
  &gt;&gt;&gt; from ctypes import *
  &gt;&gt;&gt; array = tuple(range(10))
  &gt;&gt;&gt; cdll.python32.PyTuple_SetItem(c_void_p(id(array)), c_ssize_t(1),
                                    c_void_p(id(array)))
  0
  &gt;&gt;&gt; array
  (0, (...), 2, 3, 4, 5, 6, 7, 8, 9)
  &gt;&gt;&gt; array[1] is array
  True
  &gt;&gt;&gt;
  </code></pre>