Skip to content

Commit 06c2ea0

Browse files
committed
Match literal backslash in CIF regex, relates #176
1 parent 5d1e3dd commit 06c2ea0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

ihm/dictionary.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ def __init__(self, name, primitive_code, construct):
288288
# Extend this to match any Unicode "word" character so we don't
289289
# fail to validate as soon as we see an accented character.
290290
self.construct = construct.replace('A-Za-z0-9', r'\w')
291+
# CIF regexes often include '\{}' which is interpreted by Python re
292+
# as matching '{' or '}', but it is presumably intended to also
293+
# match a literal backslash
294+
self.construct = self.construct.replace(r'\{}', r'\\{}')
291295
self.primitive_code = primitive_code
292296
# Ensure that regex matches the entire value
293297
try:

test/test_dictionary.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def make_test_dictionary():
3838
c.mandatory = False
3939
k = add_keyword("foo", False, c)
4040
# For testing we only accept upper case values
41-
k.item_type = ihm.dictionary.ItemType('text', 'char', r'[ \n\t_()A-Z]+')
41+
k.item_type = ihm.dictionary.ItemType('text', 'char',
42+
r'[ \n\t_()/\{}A-Z]+')
4243
k = add_keyword("bar", True, c)
4344
k.enumeration = set(('enum1', 'enum2'))
4445
add_keyword("baz", False, c)
@@ -389,11 +390,14 @@ def test_validate_item_type_multiline(self):
389390
d.validate(StringIO(prefix + '"FOO_BAR"'))
390391
d.validate(StringIO(prefix + '"FOO\tBAR"'))
391392
d.validate(StringIO(prefix + '\n;FOO\nBAR\n;'))
393+
d.validate(StringIO(prefix + '"FOO{BAR"'))
394+
d.validate(StringIO(prefix + '"FOO}BAR"'))
395+
d.validate(StringIO(prefix + '"FOO\\BAR"'))
392396
# Bad strings
393397
self.assertRaises(ihm.dictionary.ValidatorError, d.validate,
394398
StringIO(prefix + '"foo BAR"'))
395399
self.assertRaises(ihm.dictionary.ValidatorError, d.validate,
396-
StringIO(prefix + '"FOO\\BAR"'))
400+
StringIO(prefix + '"foo#BAR"'))
397401
self.assertRaises(ihm.dictionary.ValidatorError, d.validate,
398402
StringIO(prefix + 'n'))
399403
self.assertRaises(ihm.dictionary.ValidatorError, d.validate,

0 commit comments

Comments
 (0)