Used bytes.hex() and bytes.fromhex() to simplify.
This commit is contained in:
parent
3f237c1a5b
commit
93cdd07e8f
|
@ -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,
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)))
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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}
|
||||
|
|
Loading…
Reference in New Issue