From f7b5b3dc2c69f2a71120e499949f9435ccdf4126 Mon Sep 17 00:00:00 2001 From: Alex Nicksay Date: Wed, 28 Sep 2016 17:26:00 -0400 Subject: [PATCH] Python: Create native brotli module and move extension to _brotli --- python/{brotlimodule.cc => _brotli.cc} | 14 ++++++-------- python/brotli.py | 26 ++++++++++++++++++++++++++ setup.py | 10 ++++++++-- 3 files changed, 40 insertions(+), 10 deletions(-) rename python/{brotlimodule.cc => _brotli.cc} (96%) create mode 100644 python/brotli.py diff --git a/python/brotlimodule.cc b/python/_brotli.cc similarity index 96% rename from python/brotlimodule.cc rename to python/_brotli.cc index 1d3b208..27e8346 100644 --- a/python/brotlimodule.cc +++ b/python/_brotli.cc @@ -244,7 +244,7 @@ static PyObject* brotli_decompress(PyObject *self, PyObject *args, PyObject *key } else { PyErr_SetString(BrotliError, "BrotliDecompress failed"); } - + BrotliDecoderDestroyInstance(state); delete[] buffer; @@ -257,18 +257,16 @@ static PyMethodDef brotli_methods[] = { {NULL, NULL, 0, NULL} }; -PyDoc_STRVAR(brotli__doc__, -"The functions in this module allow compression and decompression using the\n" -"Brotli library.\n\n"); +PyDoc_STRVAR(brotli__doc__, "Implementation module for the Brotli library."); #if PY_MAJOR_VERSION >= 3 -#define INIT_BROTLI PyInit_brotli +#define INIT_BROTLI PyInit__brotli #define CREATE_BROTLI PyModule_Create(&brotli_module) #define RETURN_BROTLI return m static struct PyModuleDef brotli_module = { PyModuleDef_HEAD_INIT, - "brotli", + "_brotli", brotli__doc__, 0, brotli_methods, @@ -277,8 +275,8 @@ static struct PyModuleDef brotli_module = { NULL }; #else -#define INIT_BROTLI initbrotli -#define CREATE_BROTLI Py_InitModule3("brotli", brotli_methods, brotli__doc__) +#define INIT_BROTLI init_brotli +#define CREATE_BROTLI Py_InitModule3("_brotli", brotli_methods, brotli__doc__) #define RETURN_BROTLI return #endif diff --git a/python/brotli.py b/python/brotli.py new file mode 100644 index 0000000..b63e218 --- /dev/null +++ b/python/brotli.py @@ -0,0 +1,26 @@ +# Copyright 2016 The Brotli Authors. All rights reserved. +# +# Distributed under MIT license. +# See file LICENSE for detail or copy at https://opensource.org/licenses/MIT + +"""Functions to compress and decompress data using the Brotli library.""" + +import _brotli + + +# The library version. +__version__ = _brotli.__version__ + +# The compression mode. +MODE_GENERIC = _brotli.MODE_GENERIC +MODE_TEXT = _brotli.MODE_TEXT +MODE_FONT = _brotli.MODE_FONT + +# Compress a byte string. +compress = _brotli.compress + +# Decompress a compressed byte string. +decompress = _brotli.decompress + +# Raised if compression or decompression fails. +error = _brotli.error diff --git a/setup.py b/setup.py index 4f6782e..df6a521 100644 --- a/setup.py +++ b/setup.py @@ -174,11 +174,15 @@ CLASSIFIERS = [ 'Topic :: Utilities', ] +PACKAGE_DIR = {'': 'python'} + +PY_MODULES = ['brotli'] + EXT_MODULES = [ Extension( - 'brotli', + '_brotli', sources=[ - 'python/brotlimodule.cc', + 'python/_brotli.cc', 'common/dictionary.c', 'dec/bit_reader.c', 'dec/decode.c', @@ -268,5 +272,7 @@ setup( license=LICENSE, platforms=PLATFORMS, classifiers=CLASSIFIERS, + package_dir=PACKAGE_DIR, + py_modules=PY_MODULES, ext_modules=EXT_MODULES, cmdclass=CMD_CLASS)