Skip to content

Commit 9ca0217

Browse files
author
anthony.baxter
committed
Update to pysqlite 2.2.0
git-svn-id: http://svn.python.org/projects/python/trunk@43620 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 52088fd commit 9ca0217

17 files changed

Lines changed: 589 additions & 131 deletions

Lib/sqlite3/test/dbapi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ def CheckRowcountExecutemany(self):
268268
self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
269269
self.failUnlessEqual(self.cu.rowcount, 3)
270270

271+
def CheckTotalChanges(self):
272+
self.cu.execute("insert into test(name) values ('foo')")
273+
self.cu.execute("insert into test(name) values ('foo')")
274+
if self.cx.total_changes < 2:
275+
self.fail("total changes reported wrong value")
276+
271277
# Checks for executemany:
272278
# Sequences are required by the DB-API, iterators
273279
# enhancements in pysqlite.

Lib/sqlite3/test/hooks.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#-*- coding: ISO-8859-1 -*-
2+
# pysqlite2/test/hooks.py: tests for various SQLite-specific hooks
3+
#
4+
# Copyright (C) 2006 Gerhard Häring <gh@ghaering.de>
5+
#
6+
# This file is part of pysqlite.
7+
#
8+
# This software is provided 'as-is', without any express or implied
9+
# warranty. In no event will the authors be held liable for any damages
10+
# arising from the use of this software.
11+
#
12+
# Permission is granted to anyone to use this software for any purpose,
13+
# including commercial applications, and to alter it and redistribute it
14+
# freely, subject to the following restrictions:
15+
#
16+
# 1. The origin of this software must not be misrepresented; you must not
17+
# claim that you wrote the original software. If you use this software
18+
# in a product, an acknowledgment in the product documentation would be
19+
# appreciated but is not required.
20+
# 2. Altered source versions must be plainly marked as such, and must not be
21+
# misrepresented as being the original software.
22+
# 3. This notice may not be removed or altered from any source distribution.
23+
24+
import os, unittest
25+
import pysqlite2.dbapi2 as sqlite
26+
27+
class CollationTests(unittest.TestCase):
28+
def setUp(self):
29+
pass
30+
31+
def tearDown(self):
32+
pass
33+
34+
def CheckCreateCollationNotCallable(self):
35+
con = sqlite.connect(":memory:")
36+
try:
37+
con.create_collation("X", 42)
38+
self.fail("should have raised a TypeError")
39+
except TypeError, e:
40+
self.failUnlessEqual(e.args[0], "parameter must be callable")
41+
42+
def CheckCreateCollationNotAscii(self):
43+
con = sqlite.connect(":memory:")
44+
try:
45+
con.create_collation("collä", cmp)
46+
self.fail("should have raised a ProgrammingError")
47+
except sqlite.ProgrammingError, e:
48+
pass
49+
50+
def CheckCollationIsUsed(self):
51+
def mycoll(x, y):
52+
# reverse order
53+
return -cmp(x, y)
54+
55+
con = sqlite.connect(":memory:")
56+
con.create_collation("mycoll", mycoll)
57+
sql = """
58+
select x from (
59+
select 'a' as x
60+
union
61+
select 'b' as x
62+
union
63+
select 'c' as x
64+
) order by x collate mycoll
65+
"""
66+
result = con.execute(sql).fetchall()
67+
if result[0][0] != "c" or result[1][0] != "b" or result[2][0] != "a":
68+
self.fail("the expected order was not returned")
69+
70+
con.create_collation("mycoll", None)
71+
try:
72+
result = con.execute(sql).fetchall()
73+
self.fail("should have raised an OperationalError")
74+
except sqlite.OperationalError, e:
75+
self.failUnlessEqual(e.args[0], "no such collation sequence: mycoll")
76+
77+
def CheckCollationRegisterTwice(self):
78+
"""
79+
Register two different collation functions under the same name.
80+
Verify that the last one is actually used.
81+
"""
82+
con = sqlite.connect(":memory:")
83+
con.create_collation("mycoll", cmp)
84+
con.create_collation("mycoll", lambda x, y: -cmp(x, y))
85+
result = con.execute("""
86+
select x from (select 'a' as x union select 'b' as x) order by x collate mycoll
87+
""").fetchall()
88+
if result[0][0] != 'b' or result[1][0] != 'a':
89+
self.fail("wrong collation function is used")
90+
91+
def CheckDeregisterCollation(self):
92+
"""
93+
Register a collation, then deregister it. Make sure an error is raised if we try
94+
to use it.
95+
"""
96+
con = sqlite.connect(":memory:")
97+
con.create_collation("mycoll", cmp)
98+
con.create_collation("mycoll", None)
99+
try:
100+
con.execute("select 'a' as x union select 'b' as x order by x collate mycoll")
101+
self.fail("should have raised an OperationalError")
102+
except sqlite.OperationalError, e:
103+
if not e.args[0].startswith("no such collation sequence"):
104+
self.fail("wrong OperationalError raised")
105+
106+
def suite():
107+
collation_suite = unittest.makeSuite(CollationTests, "Check")
108+
return unittest.TestSuite((collation_suite,))
109+
110+
def test():
111+
runner = unittest.TextTestRunner()
112+
runner.run(suite())
113+
114+
if __name__ == "__main__":
115+
test()

Lib/sqlite3/test/regression.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#-*- coding: ISO-8859-1 -*-
2+
# pysqlite2/test/regression.py: pysqlite regression tests
3+
#
4+
# Copyright (C) 2006 Gerhard Häring <gh@ghaering.de>
5+
#
6+
# This file is part of pysqlite.
7+
#
8+
# This software is provided 'as-is', without any express or implied
9+
# warranty. In no event will the authors be held liable for any damages
10+
# arising from the use of this software.
11+
#
12+
# Permission is granted to anyone to use this software for any purpose,
13+
# including commercial applications, and to alter it and redistribute it
14+
# freely, subject to the following restrictions:
15+
#
16+
# 1. The origin of this software must not be misrepresented; you must not
17+
# claim that you wrote the original software. If you use this software
18+
# in a product, an acknowledgment in the product documentation would be
19+
# appreciated but is not required.
20+
# 2. Altered source versions must be plainly marked as such, and must not be
21+
# misrepresented as being the original software.
22+
# 3. This notice may not be removed or altered from any source distribution.
23+
24+
import unittest
25+
import pysqlite2.dbapi2 as sqlite
26+
27+
class RegressionTests(unittest.TestCase):
28+
def setUp(self):
29+
self.con = sqlite.connect(":memory:")
30+
31+
def tearDown(self):
32+
self.con.close()
33+
34+
def CheckPragmaUserVersion(self):
35+
# This used to crash pysqlite because this pragma command returns NULL for the column name
36+
cur = self.con.cursor()
37+
cur.execute("pragma user_version")
38+
39+
def suite():
40+
regression_suite = unittest.makeSuite(RegressionTests, "Check")
41+
return unittest.TestSuite((regression_suite,))
42+
43+
def test():
44+
runner = unittest.TextTestRunner()
45+
runner.run(suite())
46+
47+
if __name__ == "__main__":
48+
test()

Lib/sqlite3/test/transactions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import sqlite3 as sqlite
2626

2727
def get_db_path():
28-
return "testdb"
28+
return "sqlite_testdb"
2929

3030
class TransactionTests(unittest.TestCase):
3131
def setUp(self):
@@ -47,6 +47,8 @@ def tearDown(self):
4747
self.cur2.close()
4848
self.con2.close()
4949

50+
os.unlink(get_db_path())
51+
5052
def CheckDMLdoesAutoCommitBefore(self):
5153
self.cur1.execute("create table test(i)")
5254
self.cur1.execute("insert into test(i) values (5)")

Modules/_sqlite/cache.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* cache .c - a LRU cache
22
*
3-
* Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
3+
* Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
44
*
55
* This file is part of pysqlite.
66
*
@@ -29,7 +29,6 @@ Node* new_node(PyObject* key, PyObject* data)
2929
Node* node;
3030

3131
node = (Node*) (NodeType.tp_alloc(&NodeType, 0));
32-
/*node = PyObject_New(Node, &NodeType);*/
3332
if (!node) {
3433
return NULL;
3534
}
@@ -72,7 +71,12 @@ int cache_init(Cache* self, PyObject* args, PyObject* kwargs)
7271
self->size = size;
7372
self->first = NULL;
7473
self->last = NULL;
74+
7575
self->mapping = PyDict_New();
76+
if (!self->mapping) {
77+
return -1;
78+
}
79+
7680
Py_INCREF(factory);
7781
self->factory = factory;
7882

@@ -108,16 +112,11 @@ void cache_dealloc(Cache* self)
108112

109113
PyObject* cache_get(Cache* self, PyObject* args)
110114
{
111-
PyObject* key;
115+
PyObject* key = args;
112116
Node* node;
113117
Node* ptr;
114118
PyObject* data;
115119

116-
if (!PyArg_ParseTuple(args, "O", &key))
117-
{
118-
return NULL;
119-
}
120-
121120
node = (Node*)PyDict_GetItem(self->mapping, key);
122121
if (node) {
123122
node->count++;
@@ -153,7 +152,11 @@ PyObject* cache_get(Cache* self, PyObject* args)
153152
if (PyDict_Size(self->mapping) == self->size) {
154153
if (self->last) {
155154
node = self->last;
156-
PyDict_DelItem(self->mapping, self->last->key);
155+
156+
if (PyDict_DelItem(self->mapping, self->last->key) != 0) {
157+
return NULL;
158+
}
159+
157160
if (node->prev) {
158161
node->prev->next = NULL;
159162
}
@@ -171,17 +174,24 @@ PyObject* cache_get(Cache* self, PyObject* args)
171174
}
172175

173176
node = new_node(key, data);
177+
if (!node) {
178+
return NULL;
179+
}
174180
node->prev = self->last;
175181

176182
Py_DECREF(data);
177183

184+
if (PyDict_SetItem(self->mapping, key, (PyObject*)node) != 0) {
185+
Py_DECREF(node);
186+
return NULL;
187+
}
188+
178189
if (self->last) {
179190
self->last->next = node;
180191
} else {
181192
self->first = node;
182193
}
183194
self->last = node;
184-
PyDict_SetItem(self->mapping, key, (PyObject*)node);
185195
}
186196

187197
Py_INCREF(node->data);
@@ -215,10 +225,19 @@ PyObject* cache_display(Cache* self, PyObject* args)
215225
Py_INCREF(nextkey);
216226

217227
fmt_args = Py_BuildValue("OOO", prevkey, ptr->key, nextkey);
228+
if (!fmt_args) {
229+
return NULL;
230+
}
218231
template = PyString_FromString("%s <- %s ->%s\n");
232+
if (!template) {
233+
return NULL;
234+
}
219235
display_str = PyString_Format(template, fmt_args);
220236
Py_DECREF(template);
221237
Py_DECREF(fmt_args);
238+
if (!display_str) {
239+
return NULL;
240+
}
222241
PyObject_Print(display_str, stdout, Py_PRINT_RAW);
223242
Py_DECREF(display_str);
224243

@@ -233,7 +252,7 @@ PyObject* cache_display(Cache* self, PyObject* args)
233252
}
234253

235254
static PyMethodDef cache_methods[] = {
236-
{"get", (PyCFunction)cache_get, METH_VARARGS,
255+
{"get", (PyCFunction)cache_get, METH_O,
237256
PyDoc_STR("Gets an entry from the cache.")},
238257
{"display", (PyCFunction)cache_display, METH_NOARGS,
239258
PyDoc_STR("For debugging only.")},

0 commit comments

Comments
 (0)