-
Notifications
You must be signed in to change notification settings - Fork 2
/
smilez_python.c
161 lines (126 loc) · 4.71 KB
/
smilez_python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Define PY_SSIZE_T_CLEAN to silence:
* DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats
*
* PY_SSIZE_T_CLEAN was introduced in Python 2.5
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "smilez.h"
#include "smilez.c"
#include <stdlib.h>
#include <stdio.h>
#define FAST_BUFFER_SIZE 8010
static PyObject *
compress_py(PyObject *self, PyObject *args) {
char *smiles;
int smiles_len;
char fast_buffer[FAST_BUFFER_SIZE];
char *compressed_smiles;
int compressed_len;
int dictionary = 0;
PyObject *result;
if (!PyArg_ParseTuple(args, "s#|i:compress", &smiles, &smiles_len, &dictionary)) {
return NULL;
}
if (dictionary < 0 || dictionary >= SMILEZ_NUM_DICTIONARIES) {
PyErr_SetString(PyExc_ValueError, "dictionary value is out of range");
return NULL;
}
/* Won't handle more than 50 MB of SMILES string */
if (smiles_len > 50*1024*1024) {
PyErr_SetString(PyExc_ValueError, "SMILES string is too long");
return NULL;
}
compressed_len = smilez_compress(smiles, smiles_len,
fast_buffer, FAST_BUFFER_SIZE, dictionary);
if (compressed_len < FAST_BUFFER_SIZE) {
return PyBytes_FromStringAndSize(fast_buffer, compressed_len);
}
/* Grrr. Need to try again with more space. */
/* Worse case compression is 2x, and only if the encoder is broken. */
/* Real worst-case compression is 50% larger. (escape 1 byte, encode 1 byte => 3 bytes) */
compressed_smiles = PyMem_New(char, 2*smiles_len+1);
compressed_len = smilez_compress(smiles, smiles_len, compressed_smiles, 2*smiles_len+1, dictionary);
if (compressed_len == 2*smiles_len+1) {
PyErr_SetString(PyExc_AssertionError,
"Compression growth beyond expected bounds");
PyMem_Del(compressed_smiles);
return NULL;
}
result = PyBytes_FromStringAndSize(compressed_smiles, compressed_len);
PyMem_Del(compressed_smiles);
return result;
}
static PyObject *
decompress_py(PyObject *self, PyObject *args) {
char *compressed_smiles;
int compressed_smiles_len;
char fast_buffer[FAST_BUFFER_SIZE];
char *uncompressed_smiles;
int uncompressed_len;
PyObject *result;
if (!PyArg_ParseTuple(args, "y*|i:decompress", &compressed_smiles, &compressed_smiles_len)) {
return NULL;
}
/* Won't handle more than 50 MB of compressed SMILES string */
if (compressed_smiles_len > 50*1024*1024) {
PyErr_SetString(PyExc_ValueError, "compressed SMILES string is too long");
return NULL;
}
uncompressed_len = smilez_decompress(compressed_smiles, compressed_smiles_len,
fast_buffer, FAST_BUFFER_SIZE);
if (uncompressed_len < FAST_BUFFER_SIZE) {
return PyUnicode_FromStringAndSize(fast_buffer, uncompressed_len);
}
/* Grrr. Need to try again with more space. */
/* Maximum decompression ratio is 5x */
uncompressed_smiles = PyMem_New(char, 5*compressed_smiles_len+1);
uncompressed_len = smilez_decompress(compressed_smiles, compressed_smiles_len,
uncompressed_smiles, 5*compressed_smiles_len+1);
if (uncompressed_len == 5*compressed_smiles_len+1) {
PyErr_SetString(PyExc_AssertionError,
"Decompression growth beyond expected bounds");
PyMem_Del(uncompressed_smiles);
return NULL;
}
result = PyUnicode_FromStringAndSize(uncompressed_smiles, uncompressed_len);
PyMem_Del(uncompressed_smiles);
return result;
}
static PyMethodDef SmilezMethods[] = {
{"compress", compress_py, METH_VARARGS,
"Compress"},
{"decompress", decompress_py, METH_VARARGS,
"Decompress"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef smilez = {
PyModuleDef_HEAD_INIT,
"smilez", /* m_name */
"smilez doc.", /* m_doc */
-1, /* m_size */
SmilezMethods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
PyMODINIT_FUNC
PyInit_smilez(void) {
PyObject *m;
m = PyModule_Create(&smilez);
if (m == NULL) {
return;
}
PyModule_AddObject(m, "__version__",
Py_BuildValue("s", smilez_get_version()));
PyModule_AddObject(m, "compression_version",
Py_BuildValue("i", smilez_get_compression_version()));
PyModule_AddObject(m, "BYTE_DICTIONARY",
Py_BuildValue("i", SMILEZ_BYTE_DICTIONARY));
PyModule_AddObject(m, "WHITESPACE_DICTIONARY",
Py_BuildValue("i", SMILEZ_WHITESPACE_DICTIONARY));
PyModule_AddObject(m, "NUM_DICTIONARIES",
Py_BuildValue("i", SMILEZ_NUM_DICTIONARIES));
return m;
}