Skip to content

Surprising choices_provider behavior in 4.0 when using sqlite3 #1680

@bknotwell

Description

@bknotwell

People upgrading single-threaded applications to 4.0 may be surprised. Reproduce with the following methodology (tested with Python3.14.5 on OSX):

  1. Start the shell.
  2. Type t and then TAB TAB and a SQLite error occurs.

Failed query: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 8421351040 and this is thread id 6186856448.

MWE:

#!/usr/bin/env python3
"""A simple cmd2 application."""
from cmd2 import Cmd, Cmd2ArgumentParser, with_argparser, Choices
import sqlite3

class FirstApp(Cmd):
    # possibly unwise workaround
    # conn = sqlite3.connect(':memory:', check_same_thread=False)
    conn = sqlite3.connect(':memory:')
    conn.executescript('''
        CREATE TABLE xxx (X INTEGER);
        INSERT INTO  xxx (X) values(3);
        INSERT INTO  xxx (X) values(4);
        INSERT INTO  xxx (X) values(5);
        INSERT INTO  xxx (X) values(6);
    ''')

    """A simple cmd2 application."""
    def do_hello_world(self, _: cmd2.Statement):
        self.poutput(f'Hello World: {self.conn}')

    def _t_completer(self):
        try:
            ids = list(map(lambda x: x[0], self.conn.execute('SELECT X FROM xxx').fetchall()))
            return Choices.from_values(ids)
        except sqlite3.Error as exc:
            self.perror(f'Failed query: {exc}')

    set_t_parser = Cmd2ArgumentParser(description='Test')
    set_t_parser.add_argument('ttt', help='ttt help', choices_provider=_t_completer)
    @with_argparser(set_t_parser)
    def do_t(self, args):
        self.poutput(args.ttt)

if __name__ == '__main__':
    import sys
    
    c = FirstApp()
    sys.exit(c.cmdloop())

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions