Skip to content
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

Fallback to re if re2 can't be imported #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions libgrabsite/wpull_hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import re
import re2
try:
import re2
except ImportError:
pass
import os
import sys
import time
Expand Down Expand Up @@ -28,13 +31,14 @@ def cf(fname):
def re_compile(regexp):
# Validate with re first, because re2 may be more prone to segfaulting on
# bad regexps, and because re returns useful errors.
re.compile(regexp)
compiled = re.compile(regexp)
try:
return re2.compile(regexp)
except re.error:
compiled = re2.compile(regexp)
except (re.error, NameError):
# Regular expressions with lookaround expressions cannot be compiled with
# re2, so on error try compiling with re.
return re.compile(regexp)
# re2, so on error (or if re2 is unavailable) use the one compiled with re.
pass
return compiled

def compile_combined_regexp(patterns):
# If there are no patterns, we want to ignore nothing, not everything.
Expand Down Expand Up @@ -100,9 +104,12 @@ def has_changed(self):
print(f"Imported {self.fname}")
return changed


ICY_FIELD_PATTERN = re2.compile("(?i)^icy-|ice-|x-audiocast-")
ICY_VALUE_PATTERN = re2.compile("(?i)^icecast")
try:
ICY_FIELD_PATTERN = re2.compile("(?i)^icy-|ice-|x-audiocast-")
ICY_VALUE_PATTERN = re2.compile("(?i)^icecast")
except NameError:
ICY_FIELD_PATTERN = re.compile("(?i)^icy-|ice-|x-audiocast-")
ICY_VALUE_PATTERN = re.compile("(?i)^icecast")

def get_content_length(response) -> int:
try:
Expand Down