Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_raw_strings(self):
"""test that raw strings go straight thru with default_filters
turned off, bytestring_passthrough enabled.
"""
self._do_memory_test(
u("## -*- coding: utf-8 -*-\nhello ${x}"),
"hello śląsk",
default_filters=[],
template_args={"x": "śląsk"},
unicode_=False,
bytestring_passthrough=True,
output_encoding=None, # 'ascii'
)
# now, the way you *should* be doing it....
self._do_memory_test(
u("## -*- coding: utf-8 -*-\nhello ${x}"),
u("hello śląsk"),
template_args={"x": u("śląsk")},
)
def test_unicode_arg(self):
val = u(
"Alors vous imaginez ma surprise, au lever du jour, quand "
"une drôle de petite voix m’a réveillé. Elle disait: "
"« S’il vous plaît… dessine-moi un mouton! »"
)
self._do_memory_test(
"${val}",
u(
"Alors vous imaginez ma surprise, au lever du jour, quand "
"une drôle de petite voix m’a réveillé. Elle disait: "
"« S’il vous plaît… dessine-moi un mouton! »"
),
template_args={"val": val},
)
def test_encoding_errors(self):
self._do_memory_test(
u(
"""KGB (transliteration of "КГБ") is the Russian-language """
"""abbreviation for Committee for State Security, """
"""(Russian: Комит́ет Госуд́арственной Безоп́асности """
"""(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
),
u(
"""KGB (transliteration of "КГБ") is the Russian-language """
"""abbreviation for Committee for State Security, """
"""(Russian: Комит́ет Госуд́арственной Безоп́асности """
"""(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
).encode("iso-8859-1", "replace"),
output_encoding="iso-8859-1",
encoding_errors="replace",
unicode_=False,
)
"Alors vous imaginez ma surprise, au lever du jour, "
"quand une drôle de petite voix m’a réveillé. "
"Elle disait: « S’il vous plaît… dessine-moi un mouton! »"
),
filters=lambda s: s.strip(),
)
else:
self._do_memory_test(
u(
"## -*- coding: utf-8 -*-\n"
'${u"Alors vous imaginez ma surprise, au lever du jour, '
"quand une drôle de petite voix m’a réveillé. "
"Elle disait: « S’il vous plaît… dessine-moi un "
'mouton! »"}'
).encode("utf-8"),
u(
"Alors vous imaginez ma surprise, au lever du jour, "
"quand une drôle de petite voix m’a réveillé. "
"Elle disait: « S’il vous plaît… dessine-moi un mouton! »"
),
filters=lambda s: s.strip(),
)
def test_encoding_errors(self):
self._do_memory_test(
u(
"""KGB (transliteration of "КГБ") is the Russian-language """
"""abbreviation for Committee for State Security, """
"""(Russian: Комит́ет Госуд́арственной Безоп́асности """
"""(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
),
u(
"""KGB (transliteration of "КГБ") is the Russian-language """
"""abbreviation for Committee for State Security, """
"""(Russian: Комит́ет Госуд́арственной Безоп́асности """
"""(help·info); Komitet Gosudarstvennoy Bezopasnosti)"""
).encode("iso-8859-1", "replace"),
output_encoding="iso-8859-1",
encoding_errors="replace",
unicode_=False,
)
def test_unicode_literal_in_controlline(self):
if compat.py3k:
self._do_memory_test(
u(
"""## -*- coding: utf-8 -*-
<%
x = "drôle de petite voix m’a réveillé."
%>
% if x=="drôle de petite voix m’a réveillé.":
hi, ${x}
% endif
"""
).encode("utf-8"),
u("""hi, drôle de petite voix m’a réveillé."""),
filters=lambda s: s.strip(),
)
else:
self._do_memory_test(
u(
"""## -*- coding: utf-8 -*-
def __init__(self, encoding=None, errors="strict", as_unicode=False):
self.data = collections.deque()
self.encoding = encoding
if as_unicode:
self.delim = compat.u("")
else:
self.delim = ""
self.as_unicode = as_unicode
self.errors = errors
self.write = self.data.append
def process_python(self, code, code_lineno, translator_strings):
source = code.getvalue().strip()
if source.endswith(compat.b(':')):
if source in (compat.b('try:'), compat.b('else:')) or source.startswith(compat.b('except')):
source = compat.b('') # Ignore try/except and else
elif source.startswith(compat.b('elif')):
source = source[2:] # Replace "elif" with "if"
source += compat.b('pass')
code = io.BytesIO(source)
for msg in self.python_extractor(
self.filename, self.options, code, code_lineno -1):
if translator_strings:
msg = Message(msg.msgctxt, msg.msgid, msg.msgid_plural,
msg.flags,
compat.u(' ').join(
translator_strings + [msg.comment]),
msg.tcomment, msg.location)
yield msg
def process_python(self, code, code_lineno, translator_strings):
source = code.getvalue().strip()
if source.endswith(compat.b(':')):
if source in (compat.b('try:'), compat.b('else:')) or source.startswith(compat.b('except')):
source = compat.b('') # Ignore try/except and else
elif source.startswith(compat.b('elif')):
source = source[2:] # Replace "elif" with "if"
source += compat.b('pass')
code = io.BytesIO(source)
for msg in self.python_extractor(
self.filename, self.options, code, code_lineno -1):
if translator_strings:
msg = Message(msg.msgctxt, msg.msgid, msg.msgid_plural,
msg.flags,
compat.u(' ').join(
translator_strings + [msg.comment]),
msg.tcomment, msg.location)
yield msg
def process_python(self, code, code_lineno, translator_strings):
source = code.getvalue().strip()
if source.endswith(compat.b(':')):
if source in (compat.b('try:'), compat.b('else:')) or source.startswith(compat.b('except')):
source = compat.b('') # Ignore try/except and else
elif source.startswith(compat.b('elif')):
source = source[2:] # Replace "elif" with "if"
source += compat.b('pass')
code = io.BytesIO(source)
for msg in self.python_extractor(
self.filename, self.options, code, code_lineno -1):
if translator_strings:
msg = Message(msg.msgctxt, msg.msgid, msg.msgid_plural,
msg.flags,
compat.u(' ').join(
translator_strings + [msg.comment]),
msg.tcomment, msg.location)
yield msg