Skip to content

Commit 6117f42

Browse files
committed
Issue #3659: Values of string subclasses were not handled correctly when used
as bind parameters. Reviewed by Bejnamin Peterson.
1 parent d0db98f commit 6117f42

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

Lib/sqlite3/test/regression.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ def CheckSetIsolationLevel(self):
169169
con = sqlite.connect(":memory:")
170170
setattr(con, "isolation_level", "\xe9")
171171

172+
def CheckStrSubclass(self):
173+
"""
174+
The Python 3.0 port of the module didn't cope with values of subclasses of str.
175+
"""
176+
class MyStr(str): pass
177+
self.con.execute("select ?", (MyStr("abc"),))
172178

173179
def suite():
174180
regression_suite = unittest.makeSuite(RegressionTests, "Check")

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Library
2020

2121
- Bug #3884: Make the turtle module toplevel again.
2222

23+
Extension Modules
24+
-----------------
25+
26+
- Issue #3659: Subclasses of str didn't work as SQL parameters.
27+
2328

2429
What's New in Python 3.0 release candidate 1
2530
============================================

Modules/_sqlite/statement.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ typedef enum {
4343
typedef enum {
4444
TYPE_LONG,
4545
TYPE_FLOAT,
46-
TYPE_STRING,
4746
TYPE_UNICODE,
4847
TYPE_BUFFER,
4948
TYPE_UNKNOWN
@@ -96,7 +95,6 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
9695
char* string;
9796
Py_ssize_t buflen;
9897
parameter_type paramtype;
99-
char* c;
10098

10199
if (parameter == Py_None) {
102100
rc = sqlite3_bind_null(self->st, pos);
@@ -114,24 +112,13 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
114112
} else if (PyFloat_Check(parameter)) {
115113
paramtype = TYPE_FLOAT;
116114
} else if (PyUnicode_Check(parameter)) {
117-
paramtype = TYPE_STRING;
115+
paramtype = TYPE_UNICODE;
118116
} else if (PyObject_CheckBuffer(parameter)) {
119117
paramtype = TYPE_BUFFER;
120118
} else {
121119
paramtype = TYPE_UNKNOWN;
122120
}
123121

124-
if (paramtype == TYPE_STRING && !allow_8bit_chars) {
125-
string = PyBytes_AS_STRING(parameter);
126-
for (c = string; *c != 0; c++) {
127-
if (*c & 0x80) {
128-
PyErr_SetString(pysqlite_ProgrammingError, "You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.");
129-
rc = -1;
130-
goto final;
131-
}
132-
}
133-
}
134-
135122
switch (paramtype) {
136123
case TYPE_LONG:
137124
/* in the overflow error case, longval/longlongval is -1, and an exception is set */

0 commit comments

Comments
 (0)