diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py index d111f8bc44..42fbeecd08 100644 --- a/django/contrib/gis/gdal/geometries.py +++ b/django/contrib/gis/gdal/geometries.py @@ -39,7 +39,7 @@ True True """ import sys -from binascii import a2b_hex, b2a_hex +from binascii import b2a_hex from ctypes import byref, c_char_p, c_double, c_ubyte, c_void_p, string_at from django.contrib.gis.gdal.base import GDALBase @@ -67,7 +67,7 @@ class OGRGeometry(GDALBase): # If HEX, unpack input to a binary buffer. if str_instance and hex_regex.match(geom_input): - geom_input = memoryview(a2b_hex(geom_input.upper().encode())) + geom_input = memoryview(bytes.fromhex(geom_input)) str_instance = False # Constructing the geometry, diff --git a/django/db/backends/oracle/schema.py b/django/db/backends/oracle/schema.py index 742ff39f2c..39c3b7e83d 100644 --- a/django/db/backends/oracle/schema.py +++ b/django/db/backends/oracle/schema.py @@ -1,11 +1,9 @@ -import binascii import copy import datetime import re from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.utils import DatabaseError -from django.utils.encoding import force_text class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): @@ -25,7 +23,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): elif isinstance(value, str): return "'%s'" % value.replace("\'", "\'\'") elif isinstance(value, (bytes, bytearray, memoryview)): - return "'%s'" % force_text(binascii.hexlify(value)) + return "'%s'" % value.hex() elif isinstance(value, bool): return "1" if value else "0" else: diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 93cf077dd2..cbb018e9f7 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -1,4 +1,3 @@ -import codecs import contextlib import copy from decimal import Decimal @@ -49,13 +48,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): elif isinstance(value, (bytes, bytearray, memoryview)): # Bytes are only allowed for BLOB fields, encoded as string # literals containing hexadecimal data and preceded by a single "X" - # character: - # value = b'\x01\x02' => value_hex = b'0102' => return X'0102' - value = bytes(value) - hex_encoder = codecs.getencoder('hex_codec') - value_hex, _length = hex_encoder(value) - # Use 'ascii' encoding for b'01' => '01', no need to use force_text here. - return "X'%s'" % value_hex.decode('ascii') + # character. + return "X'%s'" % value.hex() else: raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value))) diff --git a/tests/gis_tests/gdal_tests/test_geom.py b/tests/gis_tests/gdal_tests/test_geom.py index c7a0872de8..d25627221d 100644 --- a/tests/gis_tests/gdal_tests/test_geom.py +++ b/tests/gis_tests/gdal_tests/test_geom.py @@ -1,6 +1,5 @@ import json import pickle -from binascii import b2a_hex from django.contrib.gis.gdal import ( CoordTransform, GDALException, OGRGeometry, OGRGeomType, SpatialReference, @@ -100,7 +99,7 @@ class OGRGeomTest(SimpleTestCase, TestDataMixin): for g in self.geometries.hex_wkt: geom1 = OGRGeometry(g.wkt) wkb = geom1.wkb - self.assertEqual(b2a_hex(wkb).upper(), g.hex.encode()) + self.assertEqual(wkb.hex().upper(), g.hex) # Constructing w/WKB. geom2 = OGRGeometry(wkb) self.assertEqual(geom1, geom2) diff --git a/tests/gis_tests/geos_tests/test_geos.py b/tests/gis_tests/geos_tests/test_geos.py index 0db98285fa..955bb6c5d2 100644 --- a/tests/gis_tests/geos_tests/test_geos.py +++ b/tests/gis_tests/geos_tests/test_geos.py @@ -2,7 +2,7 @@ import ctypes import json import pickle import random -from binascii import a2b_hex, b2a_hex +from binascii import a2b_hex from io import BytesIO from unittest import mock @@ -102,7 +102,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin): for g in self.geometries.hex_wkt: geom = fromstr(g.wkt) wkb = geom.wkb - self.assertEqual(b2a_hex(wkb).decode().upper(), g.hex) + self.assertEqual(wkb.hex().upper(), g.hex) def test_create_hex(self): "Testing creation from HEX." @@ -115,7 +115,7 @@ class GEOSTest(SimpleTestCase, TestDataMixin): def test_create_wkb(self): "Testing creation from WKB." for g in self.geometries.hex_wkt: - wkb = memoryview(a2b_hex(g.hex.encode())) + wkb = memoryview(bytes.fromhex(g.hex)) geom_h = GEOSGeometry(wkb) # we need to do this so decimal places get normalized geom_t = fromstr(g.wkt) diff --git a/tests/utils_tests/test_crypto.py b/tests/utils_tests/test_crypto.py index 8885112095..c2045fc4ab 100644 --- a/tests/utils_tests/test_crypto.py +++ b/tests/utils_tests/test_crypto.py @@ -1,4 +1,3 @@ -import binascii import hashlib import unittest @@ -132,14 +131,12 @@ class TestUtilsCryptoPBKDF2(unittest.TestCase): def test_public_vectors(self): for vector in self.rfc_vectors: result = pbkdf2(**vector['args']) - self.assertEqual(binascii.hexlify(result).decode('ascii'), - vector['result']) + self.assertEqual(result.hex(), vector['result']) def test_regression_vectors(self): for vector in self.regression_vectors: result = pbkdf2(**vector['args']) - self.assertEqual(binascii.hexlify(result).decode('ascii'), - vector['result']) + self.assertEqual(result.hex(), vector['result']) def test_default_hmac_alg(self): kwargs = {'password': b'password', 'salt': b'salt', 'iterations': 1, 'dklen': 20}