Skip to content

Commit

Permalink
Fix invalid regular expression on Windows
Browse files Browse the repository at this point in the history
The code was trying to be portable by using the OS-specific path
separator in a regular expression. However, on Windows the path
separator is a backslash which is a special character in a regular
expression.

However, simply wrapping `os.pathsep` in `re.compile()` would not be the
right thing to do here. It is very common to also use paths with
Unix-style forward slashes on Windows, especially with portable projects
which want to use the same scripts on Unix. Since forward slashes are
not allowed in file names on Windows (and they can be used "instead of"
backslashes in a lot of contexts), using both is OK on Windows. Anyone
using backslashes in, say, Linux, will see a change of behavior, but
come on, that would not exactly be the sanest thing to do. Also, YANG
disallows a lot of funny characters in module names, so let's be
reasonable here.

Of course the best fix here would be to use something like pathlib for
path handling, and only apply the regex on actual file names, but I
would prefer to use pyang in Windows CI for our project without doing a
major refactoring here.

Fixes: dad5c68 Fix issue #809: revision-date parsed wrong if >1 @ in path
  • Loading branch information
jktjkt committed Jul 7, 2022
1 parent 3fc1671 commit 23c51f1
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion pyang/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

# Not part of YANG syntax per se but useful for pyang in several places
re_filename = re.compile(
r"^(?:.*" + os.sep + r")?" + # ignore all before os.sep
r"^(?:.*[/\\])?" + # ignore all before path separator (either / or \)
r"([^@]*?)" + # putative module name
r"(?:@([^.]*?))?" + # putative revision
r"(?:\.yang|\.yin)*" + # foo@bar.yang.yin.yang.yin ?
Expand Down

0 comments on commit 23c51f1

Please sign in to comment.