-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-2883 Regex decoding error tests in top.json have unexpected, invalid syntax #721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
bson/json_util.py
Outdated
@@ -634,6 +634,8 @@ def _parse_canonical_code(doc): | |||
def _parse_canonical_regex(doc): | |||
"""Decode a JSON regex to bson.regex.Regex.""" | |||
regex = doc['$regularExpression'] | |||
if not isinstance(regex['options'], str): | |||
raise TypeError('Bad $regularExpression options, options must be string') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this check to below the len(doc) and len(regex) checks to ensure we give good error messages in all cases. For the same reason can you also add the type() of regex['options'] to the error message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
bson/json_util.py
Outdated
@@ -639,6 +639,8 @@ def _parse_canonical_regex(doc): | |||
if len(regex) != 2: | |||
raise TypeError('Bad $regularExpression must include only "pattern"' | |||
'and "options" components: %s' % (doc,)) | |||
if not isinstance(regex['options'], str): | |||
raise TypeError('Bad $regularExpression options, options must be string, was type %s' % (type(regex['options']))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We limit our lines to 79 chars according to pep 8: https://www.python.org/dev/peps/pep-0008/#maximum-line-length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor comment
bson/json_util.py
Outdated
@@ -639,6 +639,9 @@ def _parse_canonical_regex(doc): | |||
if len(regex) != 2: | |||
raise TypeError('Bad $regularExpression must include only "pattern"' | |||
'and "options" components: %s' % (doc,)) | |||
if not isinstance(regex['options'], str): | |||
raise TypeError('Bad $regularExpression options, options must be ' | |||
'string, was type %s' % (type(regex['options']))) | |||
return Regex(regex['pattern'], regex['options']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest refactoring to regex['options']
to a local variable to avoid looking it up repeatedly and improve readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
https://jira.mongodb.org/browse/PYTHON-2883