Skip to content

Commit b51ca24

Browse files
committed
Merge pull request #367 from pymssql/issue-185
Use encoded string to match substitutions
2 parents 387723f + 2e8efab commit b51ca24

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

_mssql.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,8 +1788,8 @@ cdef _quote_data(data, charset='utf8'):
17881788

17891789
raise ValueError('expected a simple type, a tuple or a dictionary.')
17901790

1791-
_re_pos_param = re.compile(r'(%([sd]))')
1792-
_re_name_param = re.compile(r'(%\(([^\)]+)\)(?:[sd]))')
1791+
_re_pos_param = re.compile(br'(%([sd]))')
1792+
_re_name_param = re.compile(br'(%\(([^\)]+)\)(?:[sd]))')
17931793
cdef _substitute_params(toformat, params, charset):
17941794
if params is None:
17951795
return toformat
@@ -1814,8 +1814,8 @@ cdef _substitute_params(toformat, params, charset):
18141814
if isinstance(params, dict):
18151815
""" assume name based substitutions """
18161816
offset = 0
1817-
for match in _re_name_param.finditer(toformat.decode(charset)):
1818-
param_key = match.group(2)
1817+
for match in _re_name_param.finditer(toformat):
1818+
param_key = match.group(2).decode(charset)
18191819

18201820
if not param_key in params:
18211821
raise ValueError('params dictionary did not contain value for placeholder: %s' % param_key)
@@ -1842,7 +1842,7 @@ cdef _substitute_params(toformat, params, charset):
18421842
else:
18431843
""" assume position based substitutions """
18441844
offset = 0
1845-
for count, match in enumerate(_re_pos_param.finditer(toformat.decode(charset))):
1845+
for count, match in enumerate(_re_pos_param.finditer(toformat)):
18461846
# calculate string positions so we can keep track of the offset to
18471847
# be used in future substitutions on this string. This is
18481848
# necessary b/c the match start() and end() are based on the

tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ def test_param_quote():
3232
eq_(res, b"SELECT * FROM empl WHERE name = N'John''s Doe'")
3333

3434

35+
def test_unicode_params():
36+
res = substitute_params(
37+
u'SELECT * FROM \u0394 WHERE name = %s',
38+
u'\u03A8'
39+
)
40+
eq_(res, b"SELECT * FROM \xce\x94 WHERE name = N'\xce\xa8'")
41+
42+
res = substitute_params(u"testing ascii (\u0105\u010D\u0119) 1=%d 'one'=%s", (1, u'str'))
43+
eq_(res, b"testing ascii (\xc4\x85\xc4\x8d\xc4\x99) 1=1 'one'=N'str'")
44+
45+
3546
def test_single_param_with_d():
3647
res = substitute_params(
3748
'SELECT * FROM employees WHERE id = %d',

0 commit comments

Comments
 (0)