Fix encoding issues with python 3


Fix encoding issues with python 3

In python 3, the default encoding was switched from ascii character sets to
unicode character sets in order to support internationalization by default.
Some interfaces, like ioctls and packets, however, specify data in terms of
non-unicode encodings formats, either in host endian (fcntl.ioctl) or
network endian (dpkt) byte order/format.

This change alters assumptions made by previous code where it was all
data objects were assumed to be basestrings, when they should have been
treated as byte arrays. In order to achieve this the following are done:

  • str objects with encodings needing to be encoded as ascii byte arrays are done so via .encode("ascii"). In order for this to work on python 3 in a type agnostic way (as it anecdotally varied depending on the caller), call .encode("ascii") only on str objects with python 3 to cast them to ascii byte arrays in a helper function name str_to_ascii(..).
  • dpkt.Packet objects needing to be passed in to fcntl.ioctl(..) are done so by casting them to byte arrays via bytes(), which calls dpkt.Packet__str__ under the covers and does the necessary str to byte array conversion needed for the dpkt APIs and struct module.

In order to accomodate this change, apply the necessary typecasting for the
byte array literal in order to search fop.name for nul bytes.

This resolves all remaining python 2.x and python 3.x compatibility issues on
amd64. More work needs to be done for the tests to function with i386, in
general (this is a legacy issue).

PR: 237403
MFC after: 1 week
Tested with: python 2.7.16 (amd64), python 3.6.8 (amd64)


ngieMay 21 2019, 3:52 AM
rS348041: Leverage `str_to_ascii(..)` in `_findop(..)` when encoding `fop.name`