#!/usr/bin/env python '''This test program demonstrates incorrect handling of long integer arguments to sqlite functions. These failures can not be reproduced on a 64 bit machine since sizeof(long) == sizeof(long long). On a 32 bit machine, the tests will fail for all integers outside the range of a 32 bit integer.''' import sqlite3 # open a memory table to be sure we won't change anything db = sqlite3.connect(':memory:') # define a function that returns its argument without modification db.create_function('DoNothing', 1, lambda x: x) # define 2 queries that should be equivalent based on the definition of the # above function testQueries = ['SELECT ?', 'SELECT DoNothing(?)'] # define interesting test values for 32/64 bit integer boundaries testValues = [ -(1<<63) , # min int64, fails on systems where long is 32 bit -(1<<31)-1, # min int32-1, fails on systems where long is 32 bit -(1<<31) , # min int32, should always pass 0, # should always pass (1<<31)-1, # max int32, should always pass (1<<31) , # max int32+1, fails on systems where long is 32 bit (1<<63)-1, # max int64, fails on systems where long is 32 bit ] # run each test query on each value and report pass/FAIL for query in testQueries: print 'testing:', query for value in testValues: sqlValue = db.execute(query, (value,)).fetchone()[0] if value == sqlValue: print 'pass:', value, '==', sqlValue else: print 'FAIL:', value, '!=', sqlValue print