Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion rivescript/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

# Python3 compat
from __future__ import print_function, unicode_literals
from six import text_type

__docformat__ = 'plaintext'

Expand Down Expand Up @@ -98,7 +99,7 @@ def call(self, rs, name, user, fields):
reply = ''
except Exception as e:
raise PythonObjectError("Error executing Python object: " + str(e))
return str(reply)
return text_type(reply)


class PythonObjectError(Exception):
Expand Down
26 changes: 20 additions & 6 deletions rivescript/rivescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,7 +2208,7 @@ def _precompile_substitution(self, kind, pattern):
:param str pattern: The substitution pattern.
"""
if pattern not in self._regexc[kind]:
qm = re.escape(pattern)
qm = self._quotemeta(pattern)
self._regexc[kind][pattern] = {
"qm": qm,
"sub1": re.compile(r'^' + qm + r'$'),
Expand Down Expand Up @@ -2292,11 +2292,11 @@ def _reply_regexp(self, user, regexp):
# If this optional had a star or anything in it, make it
# non-matching.
pipes = '|'.join(new)
pipes = re.sub(re.escape('(.+?)'), '(?:.+?)', pipes)
pipes = re.sub(re.escape('(\d+?)'), '(?:\d+?)', pipes)
pipes = re.sub(re.escape('([A-Za-z]+?)'), '(?:[A-Za-z]+?)', pipes)
pipes = re.sub(self._quotemeta(r'(.+?)'), '(?:.+?)', pipes)
pipes = re.sub(self._quotemeta(r'(\d+?)'), '(?:\d+?)', pipes)
pipes = re.sub(self._quotemeta(r'([A-Za-z]+?)'), '(?:[A-Za-z]+?)', pipes)

regexp = re.sub(r'\s*\[' + re.escape(match) + '\]\s*',
regexp = re.sub(r'\s*\[' + self._quotemeta(match) + '\]\s*',
'(?:' + pipes + r'|(?:\\s|\\b))', regexp)

# _ wildcards can't match numbers!
Expand All @@ -2308,7 +2308,7 @@ def _reply_regexp(self, user, regexp):
rep = ''
if array in self._arrays:
rep = r'(?:' + '|'.join(self._expand_array(array)) + ')'
regexp = re.sub(r'\@' + re.escape(array) + r'\b', rep, regexp)
regexp = re.sub(r'\@' + self._quotemeta(array) + r'\b', rep, regexp)

# Filter in bot variables.
bvars = re.findall(RE.bot_tag, regexp)
Expand Down Expand Up @@ -2770,6 +2770,20 @@ def _get_topic_tree(self, topic, depth=0):
# Miscellaneous Private Methods #
############################################################################

def _quotemeta(self, s):
"""Escape a string to make it safe for a regular expression.

This is used instead of ``self._quotemeta()`` because Python 3.6 obsoleted
the ``\d`` escape sequence for some reason for that function.

:param str s: The string to make regexp-safe.
:return str: The string with all regexp metacharacters escaped.
"""
unsafe = list(r'\.+?[^]$(){}=!<>|:')
for char in unsafe:
s = s.replace(char, "\\{}".format(char))
return s

def _is_atomic(self, trigger):
"""Determine if a trigger is atomic or not.

Expand Down