Skip to content

quopri pure-Python encoder splits hexadecimal escapes at line boundaries #158740

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Python standard-library documentation, quopri.encodestring: “Encode the contents of s, a bytes object which contains data, and return the encoded data as a bytes object.” The quopri module documentation identifies the encoding as quoted-printable.

Expected: 74 ASCII A bytes followed by b'=\n=FFB', preserving the contiguous =FF escape.

Actual: 74 ASCII A bytes followed by b'==\nFFB', splitting the =FF escape across a soft line break.

import quopri

s = b'A' * 74 + b'\xffB'
if not isinstance(s, bytes):
    print('REFUTATION REJECTED: input is not bytes')
else:
    lines, line = [], b''
    for c in s:
        token = bytes([c]) if 33 <= c <= 126 and c != 61 else ('=%02X' % c).encode()
        if len(line + token) > 75:
            lines.append(line + b'=\n')
            line = b''
        line += token
    expected = b''.join(lines) + line
    quopri.b2a_qp = None  # Exercise the supported pure-Python fallback.
    actual = quopri.encodestring(s, quotetabs=True, header=False)
    if actual != expected:
        print('REFUTATION CONFIRMED:', s, actual, expected)
    else:
        print('REFUTATION REJECTED: output matches independently encoded expectation')

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library quopri:

REFUTATION CONFIRMED: b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\xffB' b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\nFFB' b'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\n=FFB'

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/quopri-20261003-042505-c3

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions