Skip to content

Commit 78bcc97

Browse files
author
A. Jesse Jiryu Davis
committed
Handle unicode connection URIs in Python 2.4
Unicode MongoDB connection URIs, if they have options like "/?fsync=true", raised a TypeError in old Pythons.
1 parent 50d00ef commit 78bcc97

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pymongo/uri_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ def validate_options(opts):
150150
normalized = {}
151151
for option, value in opts.iteritems():
152152
option, value = validate(option, value)
153-
normalized[option] = value
153+
# str(option) to ensure that a unicode URI results in plain 'str'
154+
# option names. 'normalized' is then suitable to be passed as kwargs
155+
# in all Python versions.
156+
normalized[str(option)] = value
154157
return normalized
155158

156159

test/test_uri_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ def test_parse_uri(self):
335335
parse_uri("mongodb://user%40domain.com"
336336
"@localhost/foo?authMechanism=GSSAPI"))
337337

338+
def test_parse_uri_unicode(self):
339+
# Ensure parsing a unicode returns option names that can be passed
340+
# as kwargs. In Python 2.4, keyword argument names must be ASCII.
341+
# In all Pythons, str is the type of valid keyword arg names.
342+
res = parse_uri(unicode("mongodb://localhost/?fsync=true"))
343+
for key in res['options']:
344+
self.assertTrue(isinstance(key, str))
345+
338346

339347
if __name__ == "__main__":
340348
unittest.main()

0 commit comments

Comments
 (0)