TurboGears / TurboGears/backlash
utils.escape() raises TypeError for ASCII bytes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 13
- Forks
- 13
- PR merge metrics
- No merged PRs in 30d
Description
Goal
Make backlash.utils.escape handle bytes input on Python 3.
Done when: escape(b'hello') returns 'hello' instead of raising, and a test
covers both the ASCII and non-ASCII bytes cases.
Reproduction (reproduces on Python 3.9 through 3.14):
from backlash.utils import escape
escape(b'hello')
Current behaviour:
TypeError: a bytes-like object is required, not 'str'
Note that non-ASCII bytes work correctly (escape(b'caf\xc3\xa9') returns
'café'), which is why the failure went unnoticed - only the ASCII path is
broken.
Cause: the bytes branch calls s.decode('ascii') purely as a probe and discards
the result, so ASCII input stays bytes and then reaches
s.replace('&', '&') with str arguments. On Python 2 this was harmless
because bytes was str.
Suggested minimal fix: decode unconditionally with
s = s.decode('utf-8', 'replace') and drop the ASCII probe, since the
replacement decode already handles both cases.
Reachable from the interactive console, where a bogus traceback replaces the
real result:
from backlash.console import Console
Console({}, {}, None).eval("import sys; sys.stdout.write(b'hi')")
Why
escape is the single escaping helper used across traceback rendering and
console output, and HTMLStringO.write routes console writes through it. Any
code under debug that writes ASCII bytes to stdout produces a TypeError
traceback instead of its output, which is confusing precisely when the user is
trying to diagnose something else.
Verified as pre-existing rather than a regression: the same reproduction fails
identically on the commit preceding the recent modernization work.
References
backlash/utils.py(escape, thebytesbranch)backlash/tbtools.py(HTMLStringO.write) is the console path that reaches ittests/test_utils.pymay be a natural home for a regression test
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in backlash/utils.py at escape's bytes branch, then inspect tests/test_utils.py for the existing escaping cases. Add regression coverage for ASCII and non-ASCII bytes and verify the helper returns the decoded text without raising; the console path through backlash/tbtools.py can be checked afterward.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 90/100