python 3.2 works now

This commit is contained in:
Michael Greene
2014-08-10 01:54:12 -07:00
parent ca886fc049
commit 5ba1095ec0
10 changed files with 77 additions and 118 deletions

View File

@@ -67,6 +67,10 @@ class TypedProperty(property):
return type(self)(self.fget, self.fset, self.fdel, self.__doc__, **cur_setters)
# this fixes some python 2.7/3.2 shenanigans
if item == '__isabstractmethod__':
raise AttributeError(item)
if item in self.__dict__ or item in ['fset', 'fget', 'fdel', '__doc__']:
return self.__dict__[item]

View File

@@ -9,6 +9,8 @@ import hashlib
import itertools
import math
import six
from cryptography.exceptions import UnsupportedAlgorithm
from cryptography.hazmat.backends import default_backend
@@ -141,7 +143,7 @@ class UserAttributeSubPackets(SubPackets):
class Signature(MPIs):
def __bytes__(self):
return b''.join(bytes(i) for i in self)
return b''.join(i.to_mpibytes() for i in self)
class RSASignature(Signature):
@@ -175,7 +177,7 @@ class PubKey(MPIs):
return None
def __bytes__(self):
return b''.join(bytes(i) for i in self)
return b''.join(i.to_mpibytes() for i in self)
def publen(self):
return len(self)
@@ -515,7 +517,7 @@ class PrivKey(PubKey):
_bytes += self.encbytes
break
_bytes += bytes(i)
_bytes += i.to_mpibytes()
if self.s2k.usage == 0:
_bytes += self.chksum
@@ -558,7 +560,7 @@ class PrivKey(PubKey):
decryptor = cipher.decryptor()
except UnsupportedAlgorithm as e:
raise PGPOpenSSLCipherNotSupported from e
six.reraise(PGPOpenSSLCipherNotSupported, e)
pt = decryptor.update(bytes(self.encbytes)) + decryptor.finalize()

View File

@@ -538,7 +538,7 @@ class Issuer(Signature):
def __bytes__(self):
_bytes = super(Issuer, self).__bytes__()
_bytes += binascii.unhexlify(self._issuer)
_bytes += binascii.unhexlify(self._issuer.encode())
return _bytes
def parse(self, packet):

View File

@@ -48,7 +48,7 @@ class Header(_Header):
return _bytes
class SubPacket(Dispatchable, metaclass=abc.ABCMeta): ##TODO: is this metaclass declaration necessary?
class SubPacket(Dispatchable):
__headercls__ = Header
def __init__(self):

View File

@@ -171,7 +171,14 @@ class Opaque(Packet):
del packet[:self.header.length if not hasattr(self.header, 'version') else (self.header.length - 1)]
class MPI(int):
# Python 2.7 shenanigans
try: # pragma: no cover
long
except NameError:
long = int
class MPI(long):
def __new__(cls, num):
mpi = num
if isinstance(num, (bytes, bytearray)):
@@ -186,11 +193,8 @@ class MPI(int):
def byte_length(self):
return ((self.bit_length() + 7) // 8)
def __bytes__(self):
_bytes = bytearray()
_bytes += MPIs.int_to_bytes(self.bit_length(), 2)
_bytes += MPIs.int_to_bytes(self, self.byte_length())
return bytes(_bytes)
def to_mpibytes(self):
return MPIs.int_to_bytes(self.bit_length(), 2) + MPIs.int_to_bytes(self, self.byte_length())
def __len__(self):
return self.byte_length() + 2

View File

@@ -15,6 +15,8 @@ from enum import IntEnum
import requests
import six
from ._author import __version__
from .decorators import TypedProperty
@@ -103,9 +105,7 @@ class FileLoader(object):
fp.write(self.__bytes__() if binary else str(self))
class Exportable(FileLoader, metaclass=abc.ABCMeta):
__metaclass__ = abc.ABCMeta
class Exportable(six.with_metaclass(abc.ABCMeta, FileLoader)):
__crc24_init__ = 0x0B704CE
__crc24_poly__ = 0x1864CFB
@@ -219,7 +219,7 @@ class Exportable(FileLoader, metaclass=abc.ABCMeta):
return crc & 0xFFFFFF
class PGPObject(object, metaclass=abc.ABCMeta):
class PGPObject(six.with_metaclass(abc.ABCMeta, object)):
__metaclass__ = abc.ABCMeta
@staticmethod
@@ -460,7 +460,7 @@ class MetaDispatchable(abc.ABCMeta):
return obj
class Dispatchable(PGPObject, metaclass=MetaDispatchable):
class Dispatchable(six.with_metaclass(MetaDispatchable, PGPObject)):
__metaclass__ = MetaDispatchable
@abc.abstractproperty
@@ -516,7 +516,7 @@ class FlagEnumMeta(EnumMeta):
return set([f for f in self._member_map_.values() if f.value & other])
class FlagEnum(IntEnum, metaclass=FlagEnumMeta):
class FlagEnum(six.with_metaclass(FlagEnumMeta, IntEnum)):
pass

View File

@@ -6,6 +6,9 @@ import itertools
import os
import re
# compatibility shenanigans for Python 2.7
if not hasattr(re, 'ASCII'):
re.ASCII = 0
def is_ascii(text):
if not isinstance(text, (str, bytes, bytearray)):

View File

@@ -1,3 +1,4 @@
cryptography==0.4
requests
enum34
six

View File

@@ -102,8 +102,12 @@ def pytest_configure(config):
# pytest_generate_tests
# called when each test method is collected to generate parametrizations
def pytest_generate_tests(metafunc):
if not keyword.iskeyword('nonlocal'):
_outer = locals()
global spdir
global pdir
global params
global argvals
global ids
global tdata
spdir = 'subpackets/'
pdir = 'packets/'
@@ -115,15 +119,10 @@ def pytest_generate_tests(metafunc):
tdata = []
def pheader():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal argvals
nonlocal ids
else:
params = _outer['params']
argvals = _outer['argvals']
ids = _outer['ids']
# in 3.x this can be 'nonlocal' but that causes syntax errors in 2.7
global params
global argvals
global ids
params += ['pheader']
argvals += [[
@@ -145,19 +144,13 @@ def pytest_generate_tests(metafunc):
bytearray(b'\x8a' + b'\x00\x01\x00\x00' + (b'\x00' * 65536) + b'\xca\xfe\xba\xbe'),
]]
ids = ['new_1_191', 'new_2_192', 'new_2_8383', 'new_5_8384',
ids += ['new_1_191', 'new_2_192', 'new_2_8383', 'new_5_8384',
'old_1_255', 'old_2_256', 'old_4_65536']
def spheader():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal argvals
nonlocal ids
else:
params = _outer['params']
argvals = _outer['argvals']
ids = _outer['ids']
global params
global argvals
global ids
params += ['spheader']
argvals += [[
@@ -176,15 +169,9 @@ def pytest_generate_tests(metafunc):
ids += ['1_191', '2_192', '2_8383', '5_8384', '5_65535']
def sis2k():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal argvals
nonlocal ids
else:
params = _outer['params']
argvals = _outer['argvals']
ids = _outer['ids']
global params
global argvals
global ids
params += ['sis2k']
argvals += [[ (bytearray(i) +
@@ -197,15 +184,9 @@ def pytest_generate_tests(metafunc):
ids = ['sis2k_' + str(i) for i in range(len(argvals[-1]))]
def sas2k():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal argvals
nonlocal ids
else:
params = _outer['params']
argvals = _outer['argvals']
ids = _outer['ids']
global params
global argvals
global ids
params += ['sas2k']
argvals += [[ (bytearray(i) +
@@ -216,18 +197,12 @@ def pytest_generate_tests(metafunc):
b'\x01', # specifier (simple)
b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B') # hash algorithm
]]
ids = ['sis2k_' + str(i) for i in range(len(argvals[-1]))]
ids += ['sis2k_' + str(i) for i in range(len(argvals[-1]))]
def is2k():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal argvals
nonlocal ids
else:
params = _outer['params']
argvals = _outer['argvals']
ids = _outer['ids']
global params
global argvals
global ids
params += ['is2k']
argvals += [[ (bytearray(i) +
@@ -239,74 +214,50 @@ def pytest_generate_tests(metafunc):
b'\x03', # specifier (simple)
b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B') # hash algorithm
]]
ids = ['is2k_' + str(i) for i in range(len(argvals[-1]))]
ids += ['is2k_' + str(i) for i in range(len(argvals[-1]))]
@CWD_As('tests/testdata')
def sigsubpacket():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal tdata
nonlocal spdir
else:
params = _outer['params']
tdata = _outer['tdata']
global spdir
global params
global tdata
params += ['sigsubpacket']
tdata += [sorted([ spdir + f for f in os.listdir(spdir) if f.endswith('signature') ])]
@CWD_As('tests/testdata')
def uasubpacket():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal tdata
nonlocal spdir
else:
params = _outer['params']
tdata = _outer['tdata']
global spdir
global params
global tdata
params += ['uasubpacket']
tdata += [sorted([ spdir + f for f in os.listdir(spdir) if f.endswith('userattr') ])]
@CWD_As('tests/testdata')
def packet():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal tdata
nonlocal pdir
else:
params = _outer['params']
tdata = _outer['tdata']
global pdir
global params
global tdata
params += ['packet']
tdata += [sorted([ pdir + f for f in os.listdir(pdir) ])]
@CWD_As('tests/testdata')
def ekpacket():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal tdata
nonlocal pdir
else:
params = _outer['params']
tdata = _outer['tdata']
global pdir
global params
global tdata
global pdir
params += ['ekpacket']
tdata += [sorted([ pdir + f for f in os.listdir(pdir) if f.startswith('05.v4.enc') ])]
@CWD_As('tests/testdata')
def ukpacket():
if keyword.iskeyword('nonlocal'):
nonlocal params
nonlocal tdata
nonlocal pdir
else:
params = _outer['params']
tdata = _outer['tdata']
global pdir
global params
global tdata
params += ['ukpacket']
tdata += [sorted([ pdir + f for f in os.listdir(pdir) if f.startswith('05.v4.unc') ])]
@@ -319,15 +270,9 @@ def pytest_generate_tests(metafunc):
@CWD_As('tests/testdata')
def _loadbytearrays():
if keyword.iskeyword('nonlocal'):
nonlocal argvals
nonlocal tdata
nonlocal ids
else:
argvals = _outer['argvals']
ids = _outer['ids']
tdata = _outer['tdata']
global argvals
global tdata
global ids
# quick error checking
if len(set([len(stl) for stl in tdata])) > 1:

View File

@@ -108,7 +108,7 @@ class TestRegressions(object):
sig.signature.md_mod_n = MPI(sig.bytes_to_int(s))
# update header length in sig
sig.header.length += len(sig.__bytes__()[len(sig.header):])
sig.header.length = len(sig.header) + 6 + len(sig.subpackets) + len(sig.signature)
##TODO: verify sig with PGPy