Skip to content

Commit 4e445e0

Browse files
kevin1024hseg
andcommitted
Fix invalid escape sequence SyntaxWarnings
The two ASCII-art literals and the parse_multi_value_header() regex used unrecognized escape sequences (\_, \ , \s), which emit a SyntaxWarning on Python 3.12+ (DeprecationWarning earlier) and will become a SyntaxError in a future Python. Make them raw strings; the rendered art and regex behaviour are byte-for-byte unchanged (verified against the prior values). Adds a regression test that compiles every module in the httpbin package with escape-sequence warnings escalated to errors. Closes #62. Supersedes #55. Co-Authored-By: gesh <gesh@gesh.uni.cx>
1 parent 0771716 commit 4e445e0

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

httpbin/helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .structures import CaseInsensitiveDict
3030

3131

32-
ASCII_ART = """
32+
ASCII_ART = r'''
3333
-=[ teapot ]=-
3434
3535
_...._
@@ -38,8 +38,8 @@
3838
\_;`"---"`|//
3939
| ;/
4040
\_ _/
41-
`\"\"\"`
42-
"""
41+
`"""`
42+
'''
4343

4444
REDIRECT_LOCATION = '/redirect/1'
4545

@@ -74,10 +74,10 @@
7474
'image/*'
7575
]
7676

77-
ANGRY_ASCII ="""
77+
ANGRY_ASCII = r"""
7878
.-''''''-.
7979
.' _ _ '.
80-
/ O O \\
80+
/ O O \
8181
: :
8282
| |
8383
: __ :
@@ -441,7 +441,7 @@ def parse_multi_value_header(header_str):
441441
if header_str:
442442
parts = header_str.split(',')
443443
for part in parts:
444-
match = re.search('\s*(W/)?\"?([^"]*)\"?\s*', part)
444+
match = re.search(r'\s*(W/)?\"?([^"]*)\"?\s*', part)
445445
if match is not None:
446446
parsed_parts.append(match.group(2))
447447
return parsed_parts

tests/test_httpbin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,27 @@ def test_index_falls_back_to_static_page_without_flasgger(self):
823823
self.assertEqual(result.returncode, 0, result.stderr)
824824
self.assertIn("OK", result.stdout)
825825

826+
def test_sources_have_no_invalid_escape_sequences(self):
827+
"""Every module in the httpbin package compiles without escape-sequence
828+
warnings. Invalid escapes are a SyntaxWarning on 3.12+ (DeprecationWarning
829+
earlier) and will become a SyntaxError in a future Python."""
830+
import glob
831+
import warnings
832+
833+
pkg_dir = os.path.dirname(httpbin.__file__)
834+
py_files = glob.glob(os.path.join(pkg_dir, "**", "*.py"), recursive=True)
835+
self.assertTrue(py_files, "no source files found")
836+
with warnings.catch_warnings():
837+
warnings.simplefilter("error", SyntaxWarning)
838+
warnings.simplefilter("error", DeprecationWarning)
839+
for path in py_files:
840+
with open(path, encoding="utf-8") as f:
841+
src = f.read()
842+
try:
843+
compile(src, path, "exec")
844+
except (SyntaxWarning, DeprecationWarning) as exc:
845+
self.fail("{}: {}".format(path, exc))
846+
826847
def test_parse_multi_value_header(self):
827848
self.assertEqual(parse_multi_value_header('xyzzy'), [ "xyzzy" ])
828849
self.assertEqual(parse_multi_value_header('"xyzzy"'), [ "xyzzy" ])

0 commit comments

Comments
 (0)