Skip to content

Commit ccc5f7a

Browse files
committed
Prohibit passing non-string instances as text arguments to queries.
1 parent 10d95d4 commit ccc5f7a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

asyncpg/protocol/codecs/text.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cdef inline as_pg_string_and_size(
99
ConnectionSettings settings, obj, char **cstr, ssize_t *size):
1010

1111
if not cpython.PyUnicode_Check(obj):
12-
obj = str(obj)
12+
raise TypeError('expected str, got {}'.format(type(obj).__name__))
1313

1414
if settings.is_encoding_utf8():
1515
cstr[0] = PyUnicode_AsUTF8AndSize(obj, size)

tests/test_codecs.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ async def test_invalid_input(self):
487487
('int8', TypeError, 'an integer is required', [
488488
'2',
489489
'aa',
490+
]),
491+
('text', TypeError, 'expected str, got bytes', [
492+
b'foo'
493+
]),
494+
('text', TypeError, 'expected str, got list', [
495+
[1]
490496
])
491497
]
492498

@@ -735,7 +741,7 @@ async def test_extra_codec_alias(self):
735741

736742
res = await self.con.fetchval('''
737743
SELECT $1::hstore AS result
738-
''', (('foo', 2), ('bar', 3)))
744+
''', (('foo', '2'), ('bar', '3')))
739745

740746
self.assertEqual(res, {'foo': '2', 'bar': '3'})
741747

0 commit comments

Comments
 (0)