-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suppress "Compile called before Add" in re2.Filter
When compiling an empty set, ``FilteredRE2::Compile`` logs a warning to stderr which can not be suppressed (google/re2#485). Replace `re2.Filter` by a null object if the corresponding matchers list is empty: not only do we need to skip `Filter.Compile` to suppress the warning message, we need to skip `Filter.Match` or the program will segfault (google/re2#484). Using a null object seems safer and more reliable than adding conditionals, even if it requires more code and reindenting half the file. Doing this also seems safer than my first instinct of trying to use low-level fd redirection: fd redirection suffers from race conditions[^thread] and could suffer from other cross-platform compatibility issues (e.g. does every python-supported OS have stderr on fd 2 and correctly supports dup, dup2, and close?) [^thread]: AFAIK CPython does not provide a python-level GIL-pin feature (even less so with the GILectomy plans), so we have no way to prevent context-switching and any message sent to stderr by sibling threads would be lost
- Loading branch information
Showing
3 changed files
with
47 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest # type: ignore | ||
|
||
from ua_parser import Domain, PartialParseResult | ||
|
||
re2 = pytest.importorskip("ua_parser.re2") | ||
|
||
|
||
def test_empty(capfd: pytest.CaptureFixture[str]) -> None: | ||
r = re2.Resolver(([], [], [])) | ||
assert r("", Domain.ALL) == PartialParseResult(Domain.ALL, None, None, None, "") | ||
out, err = capfd.readouterr() | ||
assert out == "" | ||
assert err == "" |