Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull latest zopfli as git submodule + Python 3 support #9

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "zopfli"]
path = zopfli
url = https://github.com/google/zopfli
12 changes: 1 addition & 11 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
zopfli/blocksplitter.h
zopfli/cache.h
zopfli/deflate.h
zopfli/gzip_container.h
zopfli/hash.h
zopfli/katajainen.h
zopfli/lz77.h
zopfli/squeeze.h
zopfli/tree.h
zopfli/util.h
zopfli/zlib_container.h
recursive-include zopfli/src/zopfli *.h
41 changes: 16 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,28 @@

setup(
name='zopfli',
version='0.0.3',
version='0.0.4',
author='Adam DePrince',
author_email='[email protected]',
description='Zopfli module for python',
long_description=__doc__,
py_modules = [
'zopfli',
'zopfli.gzip',
'zopfli.zlib',
],
ext_modules = [Extension('zopfli.zopfli',
opts = "-O2 -W -Wall -Wextra -ansi -pedantic -lm",
sources = [
'zopfli/blocksplitter.c',
'zopfli/cache.c',
'zopfli/deflate.c',
'zopfli/gzip_container.c',
'zopfli/squeeze.c',
'zopfli/hash.c',
'zopfli/katajainen.c',
'zopfli/lz77.c',
'zopfli/tree.c',
'zopfli/util.c',
'zopfli/zlib_container.c',
'zopfli/zopflimodule.c',
'zopfli/src/zopfli/blocksplitter.c',
'zopfli/src/zopfli/cache.c',
'zopfli/src/zopfli/deflate.c',
'zopfli/src/zopfli/gzip_container.c',
'zopfli/src/zopfli/squeeze.c',
'zopfli/src/zopfli/hash.c',
'zopfli/src/zopfli/katajainen.c',
'zopfli/src/zopfli/lz77.c',
'zopfli/src/zopfli/tree.c',
'zopfli/src/zopfli/util.c',
'zopfli/src/zopfli/zlib_container.c',
'src/zopflimodule.c',
],
libraries = ['c']
)],
package_dir={"": "src"},
packages = ["zopfli"],
zip_safe=True,
license='ASL',
Expand All @@ -48,10 +42,7 @@
'Programming Language :: Python :: Implementation :: CPython',
'Topic :: System :: Archiving :: Compression',
],
scripts = [
],
url = "https://github.com/wnyc/pyzopfli",
install_requires = [
]
url = "https://github.com/wnyc/py-zopfli",
test_suite="tests",
)

File renamed without changes.
File renamed without changes.
File renamed without changes.
111 changes: 111 additions & 0 deletions src/zopflimodule.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#define PY_SSIZE_T_CLEAN size_t
#include <Python.h>
#include <bytesobject.h>
#include <stdlib.h>
#include "../zopfli/src/zopfli/zlib_container.h"
#include "../zopfli/src/zopfli/gzip_container.h"
#include "../zopfli/src/zopfli/util.h"

#if PY_MAJOR_VERSION >= 3
#define PyInt_Check PyLong_Check
#define PyInt_AsLong PyLong_AsLong
#endif

static PyObject *
zopfli_compress(PyObject *self, PyObject *args, PyObject *keywrds)
{
const unsigned char *in;
unsigned char *in2, *out;
size_t insize=0;
size_t outsize=0;
ZopfliOptions options;
int gzip_mode = 0;
static char *kwlist[] = {"data", "verbose", "numiterations", "blocksplitting", "blocksplittinglast", "blocksplittingmax", "gzip_mode", NULL};
PyObject *returnValue;

ZopfliInitOptions(&options);
options.verbose = 0;
options.numiterations = 15;
options.blocksplitting = 1;
options.blocksplittinglast = 0;
options.blocksplittingmax = 15;

if (!PyArg_ParseTupleAndKeywords(args, keywrds, "s#|iiiiii", kwlist, &in, &insize,
&options.verbose,
&options.numiterations,
&options.blocksplitting,
&options.blocksplittinglast,
&options.blocksplittingmax,
&gzip_mode))
return NULL;

Py_BEGIN_ALLOW_THREADS
in2 = malloc(insize);
memcpy(in2, in, insize);

if (!gzip_mode)
ZopfliZlibCompress(&options, in2, insize, &out, &outsize);
else
ZopfliGzipCompress(&options, in2, insize, &out, &outsize);

free(in2);
Py_END_ALLOW_THREADS

returnValue = PyBytes_FromStringAndSize((char*)out, outsize);
free(out);
return returnValue;
}

PyDoc_STRVAR(compress__doc__,
"zopfli.zopfli.compress applies zopfli zip or gzip compression to an obj."
"" \
"zopfli.zopfli.compress("
" s, **kwargs, verbose=0, numiterations=15, blocksplitting=1, "
" blocksplittinglast=0, blocksplittingmax=15, gzip_mode=0)"
""
"If gzip_mode is set to a non-zero value, a Gzip compatbile container will "
"be generated, otherwise a zlib compatible container will be generated. ");

static PyObject *ZopfliError;

static PyMethodDef ZopfliMethods[] = {
{ "compress", (PyCFunction)zopfli_compress, METH_VARARGS | METH_KEYWORDS, compress__doc__},
{ NULL, NULL, 0, NULL}
};

PyDoc_STRVAR(zopfli__doc__,
"Wrapper around zopfli's ZlibCompress and GzipCompress methods.");

#if PY_MAJOR_VERSION >= 3
#define INIT_ZOPFLI PyInit_zopfli
#define CREATE_ZOPFLI PyModule_Create(&zopfli_module)
#define RETURN_ZOPFLI return m

static struct PyModuleDef zopfli_module = {
PyModuleDef_HEAD_INIT,
"zopfli",
zopfli__doc__,
0,
ZopfliMethods,
NULL,
NULL,
NULL
};
#else
#define INIT_ZOPFLI initzopfli
#define CREATE_ZOPFLI Py_InitModule3("zopfli", ZopfliMethods, zopfli__doc__)
#define RETURN_ZOPFLI return
#endif

PyMODINIT_FUNC INIT_ZOPFLI(void) {
PyObject *m = CREATE_ZOPFLI;

ZopfliError = PyErr_NewException((char*) "zopfli.error", NULL, NULL);
if (ZopfliError != NULL) {
Py_INCREF(ZopfliError);
PyModule_AddObject(m, "error", ZopfliError);
}

RETURN_ZOPFLI;
}

Empty file added tests/__init__.py
Empty file.
8 changes: 4 additions & 4 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import zlib
import zopfli.gzip
import zopfli.zlib
import StringIO
from io import BytesIO

class Tests(object):
data = unittest.__doc__
data = unittest.__doc__.encode('utf-8')
def test_reversible(self):
data = self.data
self.assertEquals(self.decompress(self.compress(data)), data)
self.assertEqual(self.decompress(self.compress(data)), data)

def test_iterations_help(self):
data = self.data
Expand All @@ -26,7 +26,7 @@ class GzipTest(unittest.TestCase, Tests):
compress = staticmethod(zopfli.gzip.compress)

def decompress(self, s):
return gzip.GzipFile(fileobj=StringIO.StringIO(s)).read()
return gzip.GzipFile(fileobj=BytesIO(s)).read()


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions zopfli
Submodule zopfli added at 0aa547
Loading