Python3 Compat Problem: "Cannot use a bytes pattern on a string-like object"

Bug #1782416 reported by Thomas Ward
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
dkimpy
Invalid
Undecided
Unassigned
dkimpy (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

When attempting to call dkim.dkim_sign, and provide a message to the dkim_sign argument as either bytes or str, we get a type error that triggers in rfc822_parse within the __init__ of the DKIM class.

The type error states: "TypeError: cannot use a bytes pattern on a string-like object"

The line that errors is line 310 in the __init__.py:

lines = re.split(b"\r?\n", message)

I think we are attempting to do some type of regex split with bytes on message which is a string. Shouldn't the pattern be r"\r?\n" to indicate a regex pattern instead of a bytes pattern?

Note that this breaks the DKIM signature functions entirely, making signing useless in Python 3 on Ubuntu 18.04.

Revision history for this message
Thomas Ward (teward) wrote :

Testing the package in `python3-dkim` this same problem occurs. Adding as "Affects dkimpy" in Ubuntu.

Revision history for this message
Scott Kitterman (kitterman) wrote :

If you feed it bytes, it works fine:

>>> print(message)
b'Authentication-Results: lists.example.org; arc=none; spf=pass <email address hidden>; dkim=pass (1024-bit key) <email address hidden>; dmarc=pass\nReceived: from localhost\nMessage-ID: <email address hidden>\nDate: Mon, 01 Jan 2011 01:02:03 +0400\nFrom: Test User <email address hidden>\nTo: <email address hidden>\nSubject: Testing\n\nThis is a test message.\n'
>>> sig = dkim.dkim_sign(message, b"test", b"example.com", key)

Changed in dkimpy:
status: New → Invalid
Changed in dkimpy (Ubuntu):
status: New → Invalid
Revision history for this message
Scott Kitterman (kitterman) wrote :

This is true pretty much throughout dkimpy when used with python3. It's not particularly feasible to change it. I'm open to suggestions on improved documentation.

Revision history for this message
Thomas Ward (teward) wrote :

I had tried passing it bytes as well, with a similar failure. I will try again, but in my opinion adding type hinting might help those of us using an ODE to determine whether we are passing things properly by determining datatypes that are acceptable for which argument.

Revision history for this message
Scott Kitterman (kitterman) wrote : Re: [Bug 1782416] Re: Python3 Compat Problem: "Cannot use a bytes pattern on a string-like object"

I'm open to changes for type hinting.

Scott K

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.