]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
MFC r347996, r348024, r348031, r348042 (by ngie)
authorLi-Wen Hsu <lwhsu@FreeBSD.org>
Wed, 23 Sep 2020 12:11:14 +0000 (12:11 +0000)
committerLi-Wen Hsu <lwhsu@FreeBSD.org>
Wed, 23 Sep 2020 12:11:14 +0000 (12:11 +0000)
commit0772076ce4a309d426f6537b8528538ce08a9c53
treebfa4eb57f46e6cd542684a60e3f5732bdf081472
parentd59a31cb86572dd4d3a04e6b4dfe4f6c17e90c8d
MFC r347996, r348024, r348031, r348042 (by ngie)

r347996:
Replace uses of `foo.(de|en)code('hex')` with `binascii.(un)?hexlify(foo)`

Python 3 no longer doesn't support encoding/decoding hexadecimal numbers using
the `str.format` method. The backwards compatible new method (using the
binascii module/methods) is a comparable means of converting to/from
hexadecimal format.

In short, the functional change is the following:
* `foo.decode('hex')` -> `binascii.unhexlify(foo)`
* `foo.encode('hex')` -> `binascii.hexlify(foo)`

While here, move the dpkt import in `cryptodev.py` down per PEP8, so it comes
after the standard library provided imports.

PR: 237403

r348024:
Followup to r347996

Replace uses of `foo.encode("hex")` with `binascii.hexlify(foo)` for forwards
compatibility between python 2.x and python 3.

PR: 237403

r348031:
Squash deprecation warning related to array.array(..).tostring()

In version 3.2+, `array.array(..).tostring()` was renamed to
`array.array(..).tobytes()`. Conditionally call `array.array(..).tobytes()` if
the python version is 3.2+.

PR: 237403

r348042:
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
Tested with: python 2.7.16 (amd64), python 3.6.8 (amd64)
tests/sys/opencrypto/cryptodev.py
tests/sys/opencrypto/cryptotest.py