]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
Fix encoding issues with python 3
authorngie <ngie@FreeBSD.org>
Tue, 21 May 2019 03:52:48 +0000 (03:52 +0000)
committerngie <ngie@FreeBSD.org>
Tue, 21 May 2019 03:52:48 +0000 (03:52 +0000)
commit1dc65ca0b9b8ed84c79b178d3b9de016f6150919
tree43cdf851938c487d6259b4a4b0b5e232e1135b75
parent1d2d2b6038367336b53d0d9258d302f74781e656
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)
tests/sys/opencrypto/cryptodev.py