Skip to content

tests: fix Python 2 -> 3 portability issues in test suite#30

Open
thegushi wants to merge 13 commits into
jaredmauch:mainfrom
thegushi:fix/test-suite-python3
Open

tests: fix Python 2 -> 3 portability issues in test suite#30
thegushi wants to merge 13 commits into
jaredmauch:mainfrom
thegushi:fix/test-suite-python3

Conversation

@thegushi
Copy link
Copy Markdown
Collaborator

@thegushi thegushi commented May 20, 2026

Several Python 2 idioms in the test suite cause errors or failures under Python 3.

  • TestBase.py: fix raise (exception, value) tuple syntax -> raise exception(value); drop dead Python 2.1 fallback branch
  • test_handlers.py, test_security_mgr.py: hashlib requires bytes in Python 3 — encode plaintext before hashing
  • test_security_mgr.py: use b'...' literals for md5_new() calls
  • test_safedict.py: dict.items() returns a view in Python 3, not a list — use sorted()
  • test_bounces.py: use __file__-relative paths for bounce fixture files so the suite can be invoked from either the prefix root or from within the tests/ directory

thegushi added 13 commits May 11, 2026 23:04
iconv -o is a GNU iconv extension not supported on BSD. Replace the
subprocess call with Python's own open() encoding support, which is
portable and removes the iconv dependency entirely.
Replace file -bi encoding detection with a Python-native UTF-8 open
attempt. If the file opens cleanly as UTF-8, skip it; otherwise
convert from the known locale encoding. No external tools needed.
Detects legacy SHA1 hex digest passwords that need upgrading to
the PBKDF2 format introduced by hash_password().
sha_new() requires bytes in Python 3 but was receiving a str.
Switch to hash_password() so new lists get PBKDF2 hashes from
the start rather than legacy SHA1. Fixes jaredmauch#24.
'Hit enter to notify %(listname)s owner...' was never interpolated.
Replace print()+readline() with input() which handles both the prompt
and waiting for Enter in one call.
check_perms and Mailman/MTA/Postfix.py both had Python 2-style
print C_('...') % locals(), statements where the migration to Python 3
dropped the % locals() substitution, leaving literal %(varname)s in
error output. Also fix two bare print -> print() in Postfix.py.
BSD make does not have a wildcard function -- it treats the argument
as a variable name with spaces, generating warnings. Use $(POFILES)
in messages/ (already defined) and drop the dependency list in
templates/ (stamp file is sufficient for a clean build).
distutils was removed in Python 3.12. The configure check was using
distutils.sysconfig solely to verify Python development headers exist.
Replace with the sysconfig stdlib module (available since Python 3.2)
using sysconfig.get_path('include') in place of get_python_inc().
- TestBase.py: fix `raise (exception, value)` tuple syntax -> `raise exception(value)`;
  drop dead Python 2.1 fallback branch for ndiffAssertEqual
- test_handlers.py, test_security_mgr.py: encode plaintext before hashing —
  Python 3 hashlib requires bytes, not str
- test_security_mgr.py: use b'...' literals for md5_new() calls
- test_safedict.py: dict.items() returns a view in Python 3, use sorted()
- test_bounces.py: fix relative path 'tests/bounces/' -> 'bounces/' since
  the test suite runs from within the tests/ directory
Previously used CWD-relative paths which only worked when testall.py
was invoked from the prefix root (e.g. python3 tests/testall.py).
Using __file__-relative paths works regardless of invocation directory.
email.message_from_file() in Python 3 opens files as text with the
system encoding (UTF-8). Some bounce fixture files contain Latin-1
bytes, causing UnicodeDecodeError. Use binary mode with
email.message_from_binary_file() instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant