Skip to content

Instantly share code, notes, and snippets.

@yaniv-aknin
Created September 14, 2012 05:50
Show Gist options
  • Save yaniv-aknin/3720047 to your computer and use it in GitHub Desktop.
Save yaniv-aknin/3720047 to your computer and use it in GitHub Desktop.
demonstration of how embedding IPython (pulling in SQLite) can trigger unwanted side effects atexit
$ pip freeze
ipython==0.13
readline==6.2.2
wsgiref==0.1.2
$ cat test.py
from IPython import embed ; embed()
import thread ; thread.start_new_thread(lambda: 5/0, ())
$ python test.py
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
Type "copyright", "credits" or "license" for more information.
IPython 0.13 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
Do you really want to exit ([y]/n)?
Unhandled exception in thread started by <function <lambda> at 0x10ff22b18>
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/private/tmp/ipython/test.py in <lambda>()
1 from IPython import embed ; embed()
----> 2 import thread ; thread.start_new_thread(lambda: 5/0, ())
ZeroDivisionError: integer division or modulo by zero
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2986, in atexit_operations
self.history_manager.end_session()
File "/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.py", line 432, in end_session
self.writeout_cache()
File "<string>", line 2, in writeout_cache
File "/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.py", line 60, in needs_sqlite
return f(*a,**kw)
File "/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.py", line 607, in writeout_cache
self._writeout_input_cache(conn)
File "/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.py", line 591, in _writeout_input_cache
(self.session_number,)+line)
ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 4565094400 and this is thread id 140735320852832
Error in sys.exitfunc:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/atexit.pyc in _run_exitfuncs()
22 func, targs, kargs = _exithandlers.pop()
23 try:
---> 24 func(*targs, **kargs)
25 except SystemExit:
26 exc_info = sys.exc_info()
/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in atexit_operations(self)
2984 # this must be *before* the tempfile cleanup, in case of temporary
2985 # history db
-> 2986 self.history_manager.end_session()
2987
2988 # Cleanup all tempfiles left around
/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.pyc in end_session(self)
430 def end_session(self):
431 """Close the database session, filling in the end time and line count."""
--> 432 self.writeout_cache()
433 with self.db:
434 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE
/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.pyc in writeout_cache(self, conn)
/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.pyc in needs_sqlite(f, *a, **kw)
58 return []
59 else:
---> 60 return f(*a,**kw)
61
62
/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.pyc in writeout_cache(self, conn)
605 with self.db_input_cache_lock:
606 try:
--> 607 self._writeout_input_cache(conn)
608 except sqlite3.IntegrityError:
609 self.new_session(conn)
/private/tmp/ipython/.venv/lib/python2.7/site-packages/IPython/core/history.pyc in _writeout_input_cache(self, conn)
589 for line in self.db_input_cache:
590 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
--> 591 (self.session_number,)+line)
592
593 def _writeout_output_cache(self, conn):
ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 4565094400 and this is thread id 140735320852832
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment