diff --git a/bson/json_util.py b/bson/json_util.py index b9e088943..5498bf044 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -666,7 +666,11 @@ def _parse_canonical_regex(doc): if len(regex) != 2: raise TypeError('Bad $regularExpression must include only "pattern"' 'and "options" components: %s' % (doc,)) - return Regex(regex['pattern'], regex['options']) + opts = regex['options'] + if not isinstance(opts, str): + raise TypeError('Bad $regularExpression options, options must be ' + 'string, was type %s' % (type(opts))) + return Regex(regex['pattern'], opts) def _parse_canonical_dbref(doc): diff --git a/test/bson_corpus/top.json b/test/bson_corpus/top.json index ce7d4cdf9..fe5be0eae 100644 --- a/test/bson_corpus/top.json +++ b/test/bson_corpus/top.json @@ -92,11 +92,11 @@ }, { "description": "Bad $regularExpression (pattern is number, not string)", - "string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": 42, \"$options\" : \"\"}}}" + "string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": 42, \"options\" : \"\"}}}" }, { "description": "Bad $regularExpression (options are number, not string)", - "string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": \"a\", \"$options\" : 0}}}" + "string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": \"a\", \"options\" : 0}}}" }, { "description" : "Bad $regularExpression (missing pattern field)",