Skip to content

Commit 936d51e

Browse files
authored
Merge pull request #25 from dw/py3-only
Move to Python 3 only
2 parents 9250251 + 8c5c0d1 commit 936d51e

14 files changed

Lines changed: 69 additions & 115 deletions

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ dist: xenial
22
language: "python"
33

44
python:
5-
- "2.7"
65
- "3.4"
76
- "3.5"
87
- "3.6"
98
- "3.7"
10-
- "pypy2.7-6.0"
119
- "pypy3.5-6.0"
1210

1311
install:
@@ -16,6 +14,7 @@ install:
1614

1715
script:
1816
- "coverage run --include='cdblib/*.py' setup.py test"
17+
- "ENABLE_DJB_HASH_CEXT=1 coverage run --include='cdblib/*.py' setup.py test"
1918
- "flake8 ."
2019

2120
notifications:

README.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ python-pure-cdb
88
:target: https://python-pure-cdb.readthedocs.io/en/latest/?badge=latest
99

1010
The `python-pure-cdb` package (`pure-cdb <https://pypi.org/project/pure-cdb/>`_ on PyPI)
11-
is a Python library for working with D.J. Bernstein's "constant datbases."
12-
It supports both Python 2.7 and Python 3.
11+
is a Python library for working with D.J. Bernstein's "constant databases."
1312

1413
In addition to being able to read and write the database files produced by
15-
other `cdb` tools, `python-pure-cdb` can produce and consume "64-bit"
14+
other `cdb` tools, this package can produce and consume "64-bit"
1615
constant databases that don't have the usual 4 GiB restriction.
1716

17+
This package works with Python 3.4 and above.
18+
For a version that works with Python 2, see `this older release <https://github.com/dw/python-pure-cdb/releases/tag/v2.2.0>`_.
19+
1820
For more information on constant databases, see `djb's page <https://cr.yp.to/cdb.html>`_
1921
and `Wikipedia <https://en.wikipedia.org/wiki/Cdb_(software)>`_.
2022

cdblib/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import unicode_literals
2-
31
from .djb_hash import djb_hash
42
from .cdblib import Reader, Reader64, Writer, Writer64
53

cdblib/_djb_hash.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#define PY_SSIZE_T_CLEAN
22
#include "Python.h"
33

4-
#if PY_MAJOR_VERSION >= 3
5-
# define MOD_RETURN(mod) return mod;
6-
# define MODINIT_NAME PyInit__djb_hash
7-
# define BUFFERLIKE_FMT "y#"
8-
#else
9-
# define MOD_RETURN(mod) return;
10-
# define MODINIT_NAME init_djb_hash
11-
# define BUFFERLIKE_FMT "t#"
12-
#endif
4+
#define MOD_RETURN(mod) return mod;
5+
#define MODINIT_NAME PyInit__djb_hash
6+
#define BUFFERLIKE_FMT "y#"
137

148
/* Return a Python long instance containing the value of the DJB hash function
159
* for the given string or array. */
@@ -36,7 +30,6 @@ static /*const*/ PyMethodDef module_methods[] = {
3630
{NULL, NULL, 0, NULL}
3731
};
3832

39-
#if PY_MAJOR_VERSION >= 3
4033
static struct PyModuleDef moduledef = {
4134
PyModuleDef_HEAD_INIT,
4235
"_djb_hash",
@@ -48,16 +41,11 @@ static struct PyModuleDef moduledef = {
4841
NULL,
4942
NULL
5043
};
51-
#endif
5244

5345
PyMODINIT_FUNC
5446
MODINIT_NAME(void)
5547
{
56-
#if PY_MAJOR_VERSION >= 3
5748
PyObject *mod = PyModule_Create(&moduledef);
58-
#else
59-
PyObject *mod = Py_InitModule3("_djb_hash", module_methods, "");
60-
#endif
6149

6250
MOD_RETURN(mod);
6351
}

cdblib/cdbdump.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
from __future__ import print_function
2-
31
import argparse
42
import sys
53

6-
import six
7-
84
import cdblib
95

106

117
def cdbdump(parsed_args, **kwargs):
128
# Read binary data from stdin by default
139
stdin = kwargs.get('stdin')
1410
if stdin is None:
15-
stdin = sys.stdin if six.PY2 else sys.stdin.buffer
11+
stdin = sys.stdin.buffer
1612

1713
# Print text data to stdout by default
1814
stdout = kwargs.get('stdout')
1915
if stdout is None:
20-
stdout = sys.stdout if six.PY2 else sys.stdout.buffer
16+
stdout = sys.stdout.buffer
2117

2218
# Consume stdin and parse the cdb file
2319
reader_cls = cdblib.Reader64 if parsed_args['64'] else cdblib.Reader

cdblib/cdblib.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
http://cr.yp.to/cdb.html
66
77
'''
8-
from __future__ import unicode_literals
9-
108
from struct import Struct
119
from itertools import chain
1210

13-
import six
14-
from six.moves import range
15-
1611
from .djb_hash import djb_hash
1712

1813
# Structs for 32-bit databases
@@ -24,9 +19,10 @@
2419
write_2_le8 = Struct('<QQ').pack
2520

2621
# Encoders for keys
27-
DEFAULT_ENCODERS = {six.text_type: lambda x: x.encode('utf-8')}
28-
for t in six.integer_types:
29-
DEFAULT_ENCODERS[t] = lambda x: six.text_type(x).encode('utf-8')
22+
DEFAULT_ENCODERS = {
23+
str: lambda x: x.encode('utf-8'),
24+
int: lambda x: str(x).encode('utf-8'),
25+
}
3026

3127

3228
class _CDBBase(object):
@@ -41,7 +37,7 @@ def __init__(self, hashfn=djb_hash, strict=False, encoders=None):
4137
self.encoders.update(encoders)
4238

4339
def hash_key(self, key):
44-
if not isinstance(key, six.binary_type):
40+
if not isinstance(key, bytes):
4541
try:
4642
encoded_key = self.encoders[type(key)](key)
4743
except KeyError as e:
@@ -58,8 +54,8 @@ def hash_key_strict(self, key):
5854
try:
5955
h = self.hashfn(key)
6056
except TypeError as e:
61-
msg = 'key must be of type {}'
62-
e.args = (msg.format(six.binary_type.__name__),)
57+
msg = 'key must be of type bytes'
58+
e.args = (msg,)
6359
raise
6460

6561
# Truncate to 32 bits and remove sign.
@@ -232,9 +228,8 @@ def __exit__(self, exc_type, exc_value, traceback):
232228
def put(self, key, value=b''):
233229
'''Write a string key/value pair to the output file.'''
234230
# Ensure that the value is binary
235-
if not isinstance(value, six.binary_type):
236-
msg = 'value must be of type {}'
237-
raise TypeError(msg.format(six.binary_type.__name__))
231+
if not isinstance(value, bytes):
232+
raise TypeError('value must be of type bytes')
238233

239234
# Computing the hash for the key also ensures that it's binary
240235
key, h = self.hash_key(key)
@@ -265,12 +260,12 @@ def putints(self, key, values):
265260
def putstring(self, key, value, encoding='utf-8'):
266261
'''Write a unicode string associated with the given key to the output
267262
file after encoding it as UTF-8 or the given encoding.'''
268-
self.put(key, six.text_type.encode(value, encoding))
263+
self.put(key, value.encode(encoding))
269264

270265
def putstrings(self, key, values, encoding='utf-8'):
271266
'''Write zero or more unicode strings to the output file. Equivalent to
272267
calling putstring() in a loop.'''
273-
self.puts(key, (six.text_type.encode(v, encoding) for v in values))
268+
self.puts(key, (v.encode(encoding) for v in values))
274269

275270
def finalize(self):
276271
'''Write the final hash tables to the output file, and write out its

cdblib/cdbmake.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
from __future__ import print_function, unicode_literals
2-
31
import argparse
4-
import io
52
import os
63
import sys
74

8-
import six
9-
105
import cdblib
116

127

@@ -15,7 +10,7 @@ def __init__(self, parsed_args, **kwargs):
1510
# Read binary data from stdin and write errors to stderr (by default)
1611
self.stdin = kwargs.get('stdin')
1712
if self.stdin is None:
18-
self.stdin = sys.stdin if six.PY2 else sys.stdin.buffer
13+
self.stdin = sys.stdin.buffer
1914

2015
self.stderr = kwargs.get('stderr', sys.stderr)
2116

@@ -93,7 +88,7 @@ def get_items(self):
9388
yield key, data
9489

9590
def run(self):
96-
with io.open(self.cdb_temp_path, 'wb') as tmpfile:
91+
with open(self.cdb_temp_path, 'wb') as tmpfile:
9792
with self.writer_cls(tmpfile) as writer:
9893
for key, data in self.get_items():
9994
writer.put(key, data)

cdblib/djb_hash.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
1-
from __future__ import unicode_literals
2-
3-
import six
4-
51
# The cdb hash function is defined at: http://cr.yp.to/cdb/cdb.txt
6-
# We use separate implementations for Python 2 and 3, since they treat
7-
# iterating over byte strings differently.
8-
if six.PY2:
9-
def djb_hash(s):
10-
'''Return the value of DJB's hash function for byte string *s*'''
11-
h = 5381
12-
for c in s:
13-
h = (((h << 5) + h) ^ ord(c)) & 0xffffffff
14-
return h
15-
else:
16-
def djb_hash(s): # noqa
17-
'''Return the value of DJB's hash function for byte string *s*'''
18-
h = 5381
19-
for c in s:
20-
h = (((h << 5) + h) ^ c) & 0xffffffff
21-
return h
2+
def djb_hash(s): # noqa
3+
'''Return the value of DJB's hash function for byte string *s*'''
4+
h = 5381
5+
for c in s:
6+
h = (((h << 5) + h) ^ c) & 0xffffffff
7+
return h
8+
229

23-
# If the C Extensions is available (Python 2 only), use it
10+
# If the C Extension is available, use it
2411
try:
2512
from ._djb_hash import djb_hash # noqa
2613
except ImportError:

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# The short X.Y version
2727
version = ''
2828
# The full version, including alpha/beta/rc tags
29-
release = '2.2.0'
29+
release = '3.0.0'
3030

3131

3232
# -- General configuration ---------------------------------------------------

docs/library.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ Encoding and strict mode
119119
^^^^^^^^^^^^^^^^^^^^^^^^
120120

121121
Database keys are stored as `bytes` objects. By default, `Reader` instances
122-
will attempt to convert text keys (`str` on Python 3, `unicode` on Python 2)
123-
and integer keys (`int` on Python 3, `int` and `long` on Python 2)
124-
automatically.
122+
will attempt to convert `str` keys and `int` keys automatically.
125123

126124
>>> reader.get(b'1') # Binary key
127125
b'value_for_1'

0 commit comments

Comments
 (0)