Skip to content

Commit 35933c1

Browse files
authored
Merge pull request #19 from mr-c/futurize
full py3/py2 compat
2 parents ea5b46e + a2e3980 commit 35933c1

20 files changed

Lines changed: 146 additions & 145 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
__pycache__/
77
/test-report.xml
88
/venv/
9+
.pytest_cache/
10+
/venv3/

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ clean_sdist:
6969

7070
.PHONY: test
7171
test: _check_venv
72-
$(python) setup.py test --pytest-args "-vv --assert=plain $(tests)"
72+
$(python) setup.py test --addopts "-vv --assert=plain --continue-on-collection-errors $(tests)"
7373

7474

7575
.PHONY: pypi

jenkins.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
virtualenv venv
22
. venv/bin/activate
33
make develop
4-
export PYTEST_ADDOPTS="--junitxml=test-report.xml"
4+
export PYTEST_ADDOPTS="--junitxml=test-report.xml --junit-prefix=py2"
55
make $make_targets
6+
7+
virtualenv -p python3 venv3
8+
. venv3/bin/activate
9+
make python=python3 develop
10+
export PYTEST_ADDOPTS="--junitxml=test-report.xml --junit-prefix=py3"
11+
make python=python3 $make_targets

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
# Look for any python file, the default of test_*.py wouldn't work for us
3+
python_files=*.py
4+
# Run doctests and start test collection in the src dir
5+
addopts = --doctest-modules

setup.cfg

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
[pytest]
2-
# Look for any python file, the default of test_*.py wouldn't work for us
3-
python_files=*.py
4-
# Run doctests and start test collection in the src dir
5-
addopts = --doctest-modules
1+
[aliases]
2+
test=pytest

setup.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,12 @@
1616
package_dir={ '': 'src' },
1717
packages=find_packages( 'src' ),
1818
install_requires=[ 'future' ],
19+
setup_requires=['pytest-runner'],
1920
tests_require=[
20-
'pytest==2.7.2',
21+
'pytest==3.5.0',
2122
'mock==1.0.1',
2223
'lockfile==0.11.0',
2324
'boto==2.38.0'],
2425
namespace_packages=[ 'bd2k' ] )
2526

26-
from setuptools.command.test import test as TestCommand
27-
28-
29-
class PyTest( TestCommand ):
30-
user_options = [ ('pytest-args=', 'a', "Arguments to pass to py.test") ]
31-
32-
def initialize_options( self ):
33-
TestCommand.initialize_options( self )
34-
self.pytest_args = [ ]
35-
36-
def finalize_options( self ):
37-
TestCommand.finalize_options( self )
38-
self.test_args = [ ]
39-
self.test_suite = True
40-
41-
def run_tests( self ):
42-
import pytest
43-
# Sanitize command line arguments to avoid confusing Toil code attempting to parse them
44-
sys.argv[ 1: ] = [ ]
45-
errno = pytest.main( self.pytest_args )
46-
sys.exit( errno )
47-
48-
49-
kwargs[ 'cmdclass' ] = { 'test': PyTest }
50-
5127
setup( **kwargs )

src/bd2k/util/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def ilen( it ):
109109
"""
110110
Return the number of elements in an iterable
111111
112-
>>> ilen(xrange(0,100))
112+
>>> from builtins import range
113+
>>> ilen(range(0,100))
113114
100
114115
"""
115116
return sum( 1 for _ in it )

src/bd2k/util/collections.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ def rindex( l, v ):
152152
2
153153
>>> rindex( (0,1,0,1), 0 )
154154
2
155-
>>> rindex( xrange(3), 2 )
155+
>>> from builtins import range
156+
>>> rindex( range(3), 2 )
156157
2
157158
"""
158159
try:

src/bd2k/util/d32.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1818

1919
# Inspired by Dominic Tarr's JavaScript at https://github.com/dominictarr/d64
20-
21-
from builtins import str
20+
import codecs
21+
import sys
2222
from builtins import range
2323
from builtins import object
2424
from past.utils import old_div
@@ -29,23 +29,23 @@ class D32( object ):
2929

3030
def __init__( self, alphabet ):
3131
super( D32, self ).__init__( )
32-
self.alphabet = bytearray( alphabet )
32+
self.alphabet = bytearray( alphabet.encode('utf-8') )
3333
self.lookup = bytearray( 255 )
3434
for i in range( 32 ):
3535
self.lookup[ self.alphabet[ i ] ] = i
3636

3737
def encode( self, d ):
3838
"""
3939
>>> encode = standard.encode
40-
>>> encode('')
40+
>>> encode(b'') # doctest: +ALLOW_UNICODE
4141
''
42-
>>> encode('\\0')
42+
>>> encode(b'\\0') # doctest: +ALLOW_UNICODE
4343
'22'
44-
>>> encode('\\xff')
44+
>>> encode(b'\\xff') # doctest: +ALLOW_UNICODE
4545
'zw'
46-
>>> encode('\\0\\1\\2\\3\\4')
46+
>>> encode(b'\\0\\1\\2\\3\\4') # doctest: +ALLOW_UNICODE
4747
'222k62s6'
48-
>>> encode('\\0\\1\\2\\3\\4\\5')
48+
>>> encode(b'\\0\\1\\2\\3\\4\\5') # doctest: +ALLOW_UNICODE
4949
'222k62s62o'
5050
"""
5151
m = len( d )
@@ -57,7 +57,7 @@ def encode( self, d ):
5757

5858
while i < m:
5959
if m - i < 5:
60-
g = bytearray( d[ i: ] + '\0' * (5 - (m - i)) )
60+
g = bytearray( d[ i: ] + b'\0' * (5 - (m - i)))
6161
else:
6262
g = bytearray( d[ i:i + 5 ] )
6363
# bit 1 2 3
@@ -74,17 +74,18 @@ def encode( self, d ):
7474
e[ j + 7 ] = a[ g[ 4 ] & 31 ]
7575
j += 8
7676
i += 5
77-
return str( e[ :-padding ] )
77+
return codecs.decode( e[ :-padding ], 'ASCII' )
7878

7979
def decode( self, e ):
8080
"""
81+
>>> import codecs
8182
>>> decode = standard.decode
8283
8384
# >>> decode('222k62s62o')
8485
# '\\x00\\x01\\x02\\x03\\x04\\x05'
8586
# >>> decode('222k62s6')
8687
# '\\x00\\x01\\x02\\x03\\x04'
87-
>>> decode('zw')
88+
>>> codecs.decode(decode('zw'), 'unicode-escape') # # doctest: +ALLOW_UNICODE
8889
'\\xff'
8990
"""
9091
n = len( e )
@@ -109,7 +110,8 @@ def decode( self, e ):
109110
d[ i + 4 ] = g[ 6 ] << 5 & 255 | g[ 7 ]
110111
j += 8
111112
i += 5
112-
return bytes( d[ :-padding ] )
113+
114+
return bytes(d[ :-padding ])
113115

114116

115117
# A variant of Base64 that maintains the lexicographical ordering such that for any given list of

src/bd2k/util/d64.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919

2020
# Ported from JS found at https://github.com/dominictarr/d64
2121

22-
23-
24-
from builtins import str
22+
import codecs
23+
from builtins import bytes
2524
from builtins import range
2625
from builtins import object
2726
from past.utils import old_div
27+
2828
class D64( object ):
2929
def __init__( self, special_chars ):
3030
super( D64, self ).__init__( )
3131
alphabet = 'PYFGCRLAOEUIDHTNSQJKXBMWVZpyfgcrlaoeuidhtnsqjkxbmwvz1234567890'
32-
self.alphabet = bytearray( sorted( alphabet + special_chars ) )
32+
self.alphabet = bytearray( str(''.join(sorted( alphabet + special_chars))).encode( 'utf-8' ))
3333
self.lookup = bytearray( 255 )
3434
for i in range( 64 ):
3535
code = self.alphabet[ i ]
@@ -38,24 +38,25 @@ def __init__( self, special_chars ):
3838
def encode( self, data ):
3939
"""
4040
>>> encode = standard.encode
41-
>>> encode('')
41+
>>> encode(b'') # doctest: +ALLOW_UNICODE
4242
''
43-
>>> encode('\\x00')
43+
>>> encode(b'\\x00') # doctest: +ALLOW_UNICODE
4444
'..'
45-
>>> encode('\\x00\\x01')
45+
>>> encode(b'\\x00\\x01') # doctest: +ALLOW_UNICODE
4646
'..3'
47-
>>> encode('\\x00\\x01\\x02')
47+
>>> encode(b'\\x00\\x01\\x02') # doctest: +ALLOW_UNICODE
4848
'..31'
49-
>>> encode('\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07')
49+
>>> encode(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07') # doctest: +ALLOW_UNICODE
5050
'..31.kF40VR'
5151
"""
52+
data = bytes( data )
5253
l = len( data )
5354
s = bytearray( old_div((l * 4 + 2), 3) )
5455
hang = 0
5556
j = 0
5657
a = self.alphabet
5758
for i in range( l ):
58-
v = ord( data[ i ] )
59+
v = data[ i ]
5960
r = i % 3
6061
if r == 0:
6162
s[ j ] = a[ v >> 2 ]
@@ -76,20 +77,21 @@ def encode( self, data ):
7677
if l % 3:
7778
s[ j ] = a[ hang ]
7879

79-
return str( s )
80+
return codecs.decode( s )
8081

8182
def decode( self, e ):
8283
"""
84+
>>> import codecs
8385
>>> decode = standard.decode
84-
>>> decode('')
86+
>>> codecs.decode(decode(''), 'unicode-escape') # doctest: +ALLOW_UNICODE
8587
''
86-
>>> decode('..')
88+
>>> codecs.decode(decode('..'), 'unicode-escape') # doctest: +ALLOW_UNICODE
8789
'\\x00'
88-
>>> decode('..3')
90+
>>> codecs.decode(decode('..3'), 'unicode-escape') # doctest: +ALLOW_UNICODE
8991
'\\x00\\x01'
90-
>>> decode('..31')
92+
>>> codecs.decode(decode('..31'), 'unicode-escape') # doctest: +ALLOW_UNICODE
9193
'\\x00\\x01\\x02'
92-
>>> decode('..31.kF40VR')
94+
>>> codecs.decode(decode('..31.kF40VR'), 'unicode-escape') # doctest: +ALLOW_UNICODE
9395
'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
9496
"""
9597
n = len( e )
@@ -116,7 +118,7 @@ def decode( self, e ):
116118
j += 1
117119
else:
118120
assert False
119-
return bytes( b )
121+
return bytes(b)
120122

121123

122124
standard = D64( '._' )

0 commit comments

Comments
 (0)