Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
023fe334
Commit
023fe334
authored
Feb 01, 2012
by
Petri Lehtinen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sqlite3: Handle strings with embedded zeros correctly
Closes #13676.
parent
fc3ba6b8
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
54 additions
and
9 deletions
+54
-9
Lib/sqlite3/test/dbapi.py
Lib/sqlite3/test/dbapi.py
+7
-0
Lib/sqlite3/test/factory.py
Lib/sqlite3/test/factory.py
+36
-1
Misc/NEWS
Misc/NEWS
+2
-0
Modules/_sqlite/cursor.c
Modules/_sqlite/cursor.c
+7
-6
Modules/_sqlite/statement.c
Modules/_sqlite/statement.c
+2
-2
No files found.
Lib/sqlite3/test/dbapi.py
View file @
023fe334
...
...
@@ -225,6 +225,13 @@ class CursorTests(unittest.TestCase):
def
CheckExecuteArgString
(
self
):
self
.
cu
.
execute
(
"insert into test(name) values (?)"
,
(
"Hugo"
,))
def
CheckExecuteArgStringWithZeroByte
(
self
):
self
.
cu
.
execute
(
"insert into test(name) values (?)"
,
(
"Hu
\
x00
go"
,))
self
.
cu
.
execute
(
"select name from test where id=?"
,
(
self
.
cu
.
lastrowid
,))
row
=
self
.
cu
.
fetchone
()
self
.
assertEqual
(
row
[
0
],
"Hu
\
x00
go"
)
def
CheckExecuteWrongNoOfArgs1
(
self
):
# too many parameters
try
:
...
...
Lib/sqlite3/test/factory.py
View file @
023fe334
...
...
@@ -189,13 +189,48 @@ class TextFactoryTests(unittest.TestCase):
def
tearDown
(
self
):
self
.
con
.
close
()
class
TextFactoryTestsWithEmbeddedZeroBytes
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
con
=
sqlite
.
connect
(
":memory:"
)
self
.
con
.
execute
(
"create table test (value text)"
)
self
.
con
.
execute
(
"insert into test (value) values (?)"
,
(
"a
\
x00
b"
,))
def
CheckString
(
self
):
# text_factory defaults to str
row
=
self
.
con
.
execute
(
"select value from test"
).
fetchone
()
self
.
assertIs
(
type
(
row
[
0
]),
str
)
self
.
assertEqual
(
row
[
0
],
"a
\
x00
b"
)
def
CheckBytes
(
self
):
self
.
con
.
text_factory
=
bytes
row
=
self
.
con
.
execute
(
"select value from test"
).
fetchone
()
self
.
assertIs
(
type
(
row
[
0
]),
bytes
)
self
.
assertEqual
(
row
[
0
],
b"a
\
x00
b"
)
def
CheckBytearray
(
self
):
self
.
con
.
text_factory
=
bytearray
row
=
self
.
con
.
execute
(
"select value from test"
).
fetchone
()
self
.
assertIs
(
type
(
row
[
0
]),
bytearray
)
self
.
assertEqual
(
row
[
0
],
b"a
\
x00
b"
)
def
CheckCustom
(
self
):
# A custom factory should receive a bytes argument
self
.
con
.
text_factory
=
lambda
x
:
x
row
=
self
.
con
.
execute
(
"select value from test"
).
fetchone
()
self
.
assertIs
(
type
(
row
[
0
]),
bytes
)
self
.
assertEqual
(
row
[
0
],
b"a
\
x00
b"
)
def
tearDown
(
self
):
self
.
con
.
close
()
def
suite
():
connection_suite
=
unittest
.
makeSuite
(
ConnectionFactoryTests
,
"Check"
)
cursor_suite
=
unittest
.
makeSuite
(
CursorFactoryTests
,
"Check"
)
row_suite_compat
=
unittest
.
makeSuite
(
RowFactoryTestsBackwardsCompat
,
"Check"
)
row_suite
=
unittest
.
makeSuite
(
RowFactoryTests
,
"Check"
)
text_suite
=
unittest
.
makeSuite
(
TextFactoryTests
,
"Check"
)
return
unittest
.
TestSuite
((
connection_suite
,
cursor_suite
,
row_suite_compat
,
row_suite
,
text_suite
))
text_zero_bytes_suite
=
unittest
.
makeSuite
(
TextFactoryTestsWithEmbeddedZeroBytes
,
"Check"
)
return
unittest
.
TestSuite
((
connection_suite
,
cursor_suite
,
row_suite_compat
,
row_suite
,
text_suite
,
text_zero_bytes_suite
))
def
test
():
runner
=
unittest
.
TextTestRunner
()
...
...
Misc/NEWS
View file @
023fe334
...
...
@@ -113,6 +113,8 @@ Core and Builtins
Library
-------
- Issue #13676: Handle strings with embedded zeros correctly in sqlite3.
- Issue #13506: Add '' to path for IDLE Shell when started and restarted with Restart Shell.
Original patches by Marco Scataglini and Roger Serwy.
...
...
Modules/_sqlite/cursor.c
View file @
023fe334
...
...
@@ -268,9 +268,9 @@ PyObject* _pysqlite_build_column_name(const char* colname)
}
}
PyObject
*
pysqlite_unicode_from_string
(
const
char
*
val_str
,
int
optimize
)
PyObject
*
pysqlite_unicode_from_string
(
const
char
*
val_str
,
Py_ssize_t
size
,
int
optimize
)
{
return
PyUnicode_FromString
(
val_str
);
return
PyUnicode_FromString
AndSize
(
val_str
,
size
);
}
/*
...
...
@@ -355,10 +355,11 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
converted
=
PyFloat_FromDouble
(
sqlite3_column_double
(
self
->
statement
->
st
,
i
));
}
else
if
(
coltype
==
SQLITE_TEXT
)
{
val_str
=
(
const
char
*
)
sqlite3_column_text
(
self
->
statement
->
st
,
i
);
nbytes
=
sqlite3_column_bytes
(
self
->
statement
->
st
,
i
);
if
((
self
->
connection
->
text_factory
==
(
PyObject
*
)
&
PyUnicode_Type
)
||
(
self
->
connection
->
text_factory
==
pysqlite_OptimizedUnicode
))
{
converted
=
pysqlite_unicode_from_string
(
val_str
,
converted
=
pysqlite_unicode_from_string
(
val_str
,
nbytes
,
self
->
connection
->
text_factory
==
pysqlite_OptimizedUnicode
?
1
:
0
);
if
(
!
converted
)
{
...
...
@@ -383,11 +384,11 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
}
}
}
else
if
(
self
->
connection
->
text_factory
==
(
PyObject
*
)
&
PyBytes_Type
)
{
converted
=
PyBytes_FromString
(
val_str
);
converted
=
PyBytes_FromString
AndSize
(
val_str
,
nbytes
);
}
else
if
(
self
->
connection
->
text_factory
==
(
PyObject
*
)
&
PyByteArray_Type
)
{
converted
=
PyByteArray_FromStringAndSize
(
val_str
,
strlen
(
val_str
)
);
converted
=
PyByteArray_FromStringAndSize
(
val_str
,
nbytes
);
}
else
{
converted
=
PyObject_CallFunction
(
self
->
connection
->
text_factory
,
"y
"
,
val_str
);
converted
=
PyObject_CallFunction
(
self
->
connection
->
text_factory
,
"y
#"
,
val_str
,
nbytes
);
}
}
else
{
/* coltype == SQLITE_BLOB */
...
...
Modules/_sqlite/statement.c
View file @
023fe334
...
...
@@ -129,9 +129,9 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
rc
=
sqlite3_bind_double
(
self
->
st
,
pos
,
PyFloat_AsDouble
(
parameter
));
break
;
case
TYPE_UNICODE
:
string
=
_PyUnicode_AsString
(
parameter
);
string
=
_PyUnicode_AsString
AndSize
(
parameter
,
&
buflen
);
if
(
string
!=
NULL
)
rc
=
sqlite3_bind_text
(
self
->
st
,
pos
,
string
,
-
1
,
SQLITE_TRANSIENT
);
rc
=
sqlite3_bind_text
(
self
->
st
,
pos
,
string
,
buflen
,
SQLITE_TRANSIENT
);
else
rc
=
-
1
;
break
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment