Cython 3.2.6__py3-none-any.whl → 3.3.0a2.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of Cython might be problematic. Click here for more details.
- Cython/Build/BuildExecutable.py +1 -14
- Cython/Build/Dependencies.py +6 -44
- Cython/Build/Inline.py +7 -3
- Cython/Build/IpythonMagic.py +0 -1
- Cython/Build/SharedModule.py +1 -2
- Cython/Build/Tests/TestCythonizeArgsParser.py +6 -6
- Cython/Build/Tests/TestDependencies.py +3 -3
- Cython/Build/Tests/TestInline.py +3 -3
- Cython/Build/Tests/TestIpythonMagic.py +35 -19
- Cython/Build/Tests/TestStripLiterals.py +2 -2
- Cython/Compiler/Buffer.py +5 -5
- Cython/Compiler/Builtin.py +57 -46
- Cython/Compiler/Code.pxd +12 -9
- Cython/Compiler/Code.py +220 -94
- Cython/Compiler/Dataclass.py +78 -52
- Cython/Compiler/Errors.py +21 -8
- Cython/Compiler/ExprNodes.py +871 -459
- Cython/Compiler/FlowControl.pxd +8 -6
- Cython/Compiler/FlowControl.py +111 -8
- Cython/Compiler/FusedNode.py +17 -12
- Cython/Compiler/Lexicon.py +123 -122
- Cython/Compiler/Main.py +3 -4
- Cython/Compiler/MatchCaseNodes.py +1948 -10
- Cython/Compiler/MemoryView.py +62 -33
- Cython/Compiler/ModuleNode.py +559 -283
- Cython/Compiler/Naming.py +4 -0
- Cython/Compiler/Nodes.py +333 -197
- Cython/Compiler/Optimize.py +268 -112
- Cython/Compiler/Options.py +0 -5
- Cython/Compiler/ParseTreeTransforms.pxd +2 -1
- Cython/Compiler/ParseTreeTransforms.py +180 -76
- Cython/Compiler/Parsing.py +55 -38
- Cython/Compiler/PyrexTypes.py +364 -128
- Cython/Compiler/Scanning.pxd +27 -5
- Cython/Compiler/Scanning.py +24 -14
- Cython/Compiler/StringEncoding.py +1 -2
- Cython/Compiler/Symtab.py +27 -19
- Cython/Compiler/Tests/TestBuiltin.py +41 -6
- Cython/Compiler/Tests/TestCmdLine.py +3 -2
- Cython/Compiler/Tests/TestCode.py +4 -3
- Cython/Compiler/Tests/TestFlowControl.py +2 -2
- Cython/Compiler/Tests/TestParseTreeTransforms.py +2 -2
- Cython/Compiler/Tests/TestScanning.py +2 -4
- Cython/Compiler/Tests/TestSignatureMatching.py +3 -3
- Cython/Compiler/Tests/TestStringEncoding.py +2 -3
- Cython/Compiler/Tests/TestTypes.py +46 -3
- Cython/Compiler/Tests/TestUtilityLoad.py +4 -4
- Cython/Compiler/TypeInference.py +20 -0
- Cython/Compiler/TypeSlots.py +171 -16
- Cython/Compiler/UtilNodes.py +24 -0
- Cython/Compiler/UtilityCode.py +1 -0
- Cython/Compiler/Visitor.pxd +1 -1
- Cython/Compiler/Visitor.py +3 -2
- Cython/Debugger/Tests/TestLibCython.py +7 -2
- Cython/Debugger/Tests/test_libcython_in_gdb.py +4 -2
- Cython/Debugger/Tests/test_libpython_in_gdb.py +1 -1
- Cython/Includes/cpython/array.pxd +5 -5
- Cython/Includes/cpython/dict.pxd +1 -6
- Cython/Includes/cpython/float.pxd +19 -10
- Cython/Includes/cpython/frozendict.pxd +37 -0
- Cython/Includes/cpython/long.pxd +31 -0
- Cython/Includes/cpython/object.pxd +9 -12
- Cython/Includes/cpython/sentinel.pxd +17 -0
- Cython/Includes/cpython/type.pxd +93 -0
- Cython/Includes/libcpp/exception.pxd +90 -0
- Cython/LZSS.py +170 -0
- Cython/Shadow.py +550 -73
- Cython/StringIOTree.py +1 -2
- Cython/Tempita/_tempita.py +45 -46
- Cython/TestUtils.py +50 -8
- Cython/Tests/TestCythonUtils.py +2 -2
- Cython/Tests/TestJediTyper.py +1 -1
- Cython/Tests/TestShadow.py +7 -5
- Cython/Tests/TestStringIOTree.py +4 -3
- Cython/Tests/TestTestUtils.py +2 -3
- Cython/Utility/AsyncGen.c +59 -19
- Cython/Utility/Buffer.c +4 -3
- Cython/Utility/Builtins.c +139 -17
- Cython/Utility/CConvert.pyx +27 -12
- Cython/Utility/CommonStructures.c +55 -37
- Cython/Utility/Coroutine.c +112 -68
- Cython/Utility/CythonFunction.c +607 -330
- Cython/Utility/Embed.c +8 -0
- Cython/Utility/Exceptions.c +20 -3
- Cython/Utility/ExtensionTypes.c +414 -224
- Cython/Utility/FunctionArguments.c +83 -81
- Cython/Utility/ImportExport.c +72 -61
- Cython/Utility/MatchCase.c +979 -0
- Cython/Utility/MatchCase_Cy.pyx +12 -0
- Cython/Utility/MemoryView.pxd +9 -88
- Cython/Utility/MemoryView.pyx +173 -61
- Cython/Utility/MemoryView_C.c +3 -3
- Cython/Utility/ModuleSetupCode.c +306 -157
- Cython/Utility/ObjectHandling.c +483 -404
- Cython/Utility/Optimize.c +1117 -164
- Cython/Utility/Overflow.c +3 -9
- Cython/Utility/Profile.c +4 -0
- Cython/Utility/StringTools.c +187 -149
- Cython/Utility/Synchronization.c +39 -0
- Cython/Utility/TString.c +24 -13
- Cython/Utility/TypeConversion.c +405 -250
- Cython/Utility/arrayarray.h +24 -22
- Cython/Utils.py +11 -23
- Cython/__init__.py +3 -1
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/METADATA +14 -33
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/RECORD +109 -107
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/WHEEL +1 -1
- Cython/Shadow.pyi +0 -521
- Cython/Utility/Printing.c +0 -86
- Cython/__init__.pyi +0 -7
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/entry_points.txt +0 -0
- {cython-3.2.6.dist-info → cython-3.3.0a2.dev0.dist-info}/top_level.txt +0 -0
|
@@ -399,25 +399,15 @@ cdef extern from "Python.h":
|
|
|
399
399
|
# Added in Py2.6
|
|
400
400
|
|
|
401
401
|
# Type flags (tp_flags of PyTypeObject)
|
|
402
|
-
const long Py_TPFLAGS_HAVE_GETCHARBUFFER
|
|
403
|
-
const long Py_TPFLAGS_HAVE_SEQUENCE_IN
|
|
404
|
-
const long Py_TPFLAGS_HAVE_INPLACEOPS
|
|
405
|
-
const long Py_TPFLAGS_CHECKTYPES
|
|
406
|
-
const long Py_TPFLAGS_HAVE_RICHCOMPARE
|
|
407
|
-
const long Py_TPFLAGS_HAVE_WEAKREFS
|
|
408
|
-
const long Py_TPFLAGS_HAVE_ITER
|
|
409
|
-
const long Py_TPFLAGS_HAVE_CLASS
|
|
410
402
|
const long Py_TPFLAGS_HEAPTYPE
|
|
411
403
|
const long Py_TPFLAGS_BASETYPE
|
|
412
404
|
const long Py_TPFLAGS_READY
|
|
413
405
|
const long Py_TPFLAGS_READYING
|
|
414
406
|
const long Py_TPFLAGS_HAVE_GC
|
|
415
407
|
const long Py_TPFLAGS_HAVE_STACKLESS_EXTENSION
|
|
416
|
-
const long Py_TPFLAGS_HAVE_INDEX
|
|
417
408
|
const long Py_TPFLAGS_HAVE_VERSION_TAG # unused since Py3.11
|
|
418
409
|
const long Py_TPFLAGS_VALID_VERSION_TAG
|
|
419
410
|
const long Py_TPFLAGS_IS_ABSTRACT
|
|
420
|
-
const long Py_TPFLAGS_HAVE_NEWBUFFER
|
|
421
411
|
const long Py_TPFLAGS_INT_SUBCLASS
|
|
422
412
|
const long Py_TPFLAGS_LONG_SUBCLASS
|
|
423
413
|
const long Py_TPFLAGS_LIST_SUBCLASS
|
|
@@ -427,7 +417,14 @@ cdef extern from "Python.h":
|
|
|
427
417
|
const long Py_TPFLAGS_DICT_SUBCLASS
|
|
428
418
|
const long Py_TPFLAGS_BASE_EXC_SUBCLASS
|
|
429
419
|
const long Py_TPFLAGS_TYPE_SUBCLASS
|
|
430
|
-
const long Py_TPFLAGS_DEFAULT_EXTERNAL
|
|
431
|
-
const long Py_TPFLAGS_DEFAULT_CORE
|
|
432
420
|
const long Py_TPFLAGS_DEFAULT
|
|
433
421
|
const long Py_TPFLAGS_HAVE_FINALIZE # unused since CPython 3.8
|
|
422
|
+
const long Py_TPFLAGS_METHOD_DESCRIPTOR
|
|
423
|
+
const long Py_TPFLAGS_IMMUTABLETYPE
|
|
424
|
+
const long Py_TPFLAGS_DISALLOW_INSTANTIATION
|
|
425
|
+
const long Py_TPFLAGS_SEQUENCE
|
|
426
|
+
const long Py_TPFLAGS_MAPPING
|
|
427
|
+
const long Py_TPFLAGS_MANAGED_DICT
|
|
428
|
+
const long Py_TPFLAGS_MANAGED_WEAKREF
|
|
429
|
+
const long Py_TPFLAGS_ITEMS_AT_END
|
|
430
|
+
const long Py_TPFLAGS_HAVE_VECTORCALL
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
############################################################################
|
|
2
|
+
# Sentinel Objects (Python 3.15+)
|
|
3
|
+
############################################################################
|
|
4
|
+
|
|
5
|
+
cdef extern from "Python.h":
|
|
6
|
+
# PyTypeObject PySentinel_Type
|
|
7
|
+
# This instance of PyTypeObject represents the Python 'sentinel' type.
|
|
8
|
+
# This is the same object as 'sentinel'.
|
|
9
|
+
|
|
10
|
+
bint PySentinel_Check(object o)
|
|
11
|
+
# Return true if o is a sentinel object.
|
|
12
|
+
# The sentinel type does not allow subclasses, so this check is exact.
|
|
13
|
+
|
|
14
|
+
object PySentinel_New(const char *name, const char *module_name)
|
|
15
|
+
# Return value: New reference.
|
|
16
|
+
# Return a new sentinel object with __name__ set to name and __module__ set to module_name.
|
|
17
|
+
# name must not be NULL. If module_name is NULL, __module__ is set to None.
|
Cython/Includes/cpython/type.pxd
CHANGED
|
@@ -51,3 +51,96 @@ cdef extern from "Python.h":
|
|
|
51
51
|
# responsible for adding inherited slots from a type's base
|
|
52
52
|
# class. Return 0 on success, or return -1 and sets an exception
|
|
53
53
|
# on error.
|
|
54
|
+
|
|
55
|
+
enum:
|
|
56
|
+
# Constants for PyType_GetSlot() slot IDs.
|
|
57
|
+
Py_bf_getbuffer = 1
|
|
58
|
+
Py_bf_releasebuffer = 2
|
|
59
|
+
Py_mp_ass_subscript = 3
|
|
60
|
+
Py_mp_length = 4
|
|
61
|
+
Py_mp_subscript = 5
|
|
62
|
+
Py_nb_absolute = 6
|
|
63
|
+
Py_nb_add = 7
|
|
64
|
+
Py_nb_and = 8
|
|
65
|
+
Py_nb_bool = 9
|
|
66
|
+
Py_nb_divmod = 10
|
|
67
|
+
Py_nb_float = 11
|
|
68
|
+
Py_nb_floor_divide = 12
|
|
69
|
+
Py_nb_index = 13
|
|
70
|
+
Py_nb_inplace_add = 14
|
|
71
|
+
Py_nb_inplace_and = 15
|
|
72
|
+
Py_nb_inplace_floor_divide = 16
|
|
73
|
+
Py_nb_inplace_lshift = 17
|
|
74
|
+
Py_nb_inplace_multiply = 18
|
|
75
|
+
Py_nb_inplace_or = 19
|
|
76
|
+
Py_nb_inplace_power = 20
|
|
77
|
+
Py_nb_inplace_remainder = 21
|
|
78
|
+
Py_nb_inplace_rshift = 22
|
|
79
|
+
Py_nb_inplace_subtract = 23
|
|
80
|
+
Py_nb_inplace_true_divide = 24
|
|
81
|
+
Py_nb_inplace_xor = 25
|
|
82
|
+
Py_nb_int = 26
|
|
83
|
+
Py_nb_invert = 27
|
|
84
|
+
Py_nb_lshift = 28
|
|
85
|
+
Py_nb_multiply = 29
|
|
86
|
+
Py_nb_negative = 30
|
|
87
|
+
Py_nb_or = 31
|
|
88
|
+
Py_nb_positive = 32
|
|
89
|
+
Py_nb_power = 33
|
|
90
|
+
Py_nb_remainder = 34
|
|
91
|
+
Py_nb_rshift = 35
|
|
92
|
+
Py_nb_subtract = 36
|
|
93
|
+
Py_nb_true_divide = 37
|
|
94
|
+
Py_nb_xor = 38
|
|
95
|
+
Py_sq_ass_item = 39
|
|
96
|
+
Py_sq_concat = 40
|
|
97
|
+
Py_sq_contains = 41
|
|
98
|
+
Py_sq_inplace_concat = 42
|
|
99
|
+
Py_sq_inplace_repeat = 43
|
|
100
|
+
Py_sq_item = 44
|
|
101
|
+
Py_sq_length = 45
|
|
102
|
+
Py_sq_repeat = 46
|
|
103
|
+
Py_tp_alloc = 47
|
|
104
|
+
Py_tp_base = 48
|
|
105
|
+
Py_tp_bases = 49
|
|
106
|
+
Py_tp_call = 50
|
|
107
|
+
Py_tp_clear = 51
|
|
108
|
+
Py_tp_dealloc = 52
|
|
109
|
+
Py_tp_del = 53
|
|
110
|
+
Py_tp_descr_get = 54
|
|
111
|
+
Py_tp_descr_set = 55
|
|
112
|
+
Py_tp_doc = 56
|
|
113
|
+
Py_tp_getattr = 57
|
|
114
|
+
Py_tp_getattro = 58
|
|
115
|
+
Py_tp_hash = 59
|
|
116
|
+
Py_tp_init = 60
|
|
117
|
+
Py_tp_is_gc = 61
|
|
118
|
+
Py_tp_iter = 62
|
|
119
|
+
Py_tp_iternext = 63
|
|
120
|
+
Py_tp_methods = 64
|
|
121
|
+
Py_tp_new = 65
|
|
122
|
+
Py_tp_repr = 66
|
|
123
|
+
Py_tp_richcompare = 67
|
|
124
|
+
Py_tp_setattr = 68
|
|
125
|
+
Py_tp_setattro = 69
|
|
126
|
+
Py_tp_str = 70
|
|
127
|
+
Py_tp_traverse = 71
|
|
128
|
+
Py_tp_members = 72
|
|
129
|
+
Py_tp_getset = 73
|
|
130
|
+
Py_tp_free = 74
|
|
131
|
+
Py_nb_matrix_multiply = 75
|
|
132
|
+
Py_nb_inplace_matrix_multiply = 76
|
|
133
|
+
Py_am_await = 77
|
|
134
|
+
Py_am_aiter = 78
|
|
135
|
+
Py_am_anext = 79
|
|
136
|
+
Py_tp_finalize = 80
|
|
137
|
+
Py_am_send = 81 # New in 3.10
|
|
138
|
+
Py_tp_vectorcall = 82 # New in 3.14
|
|
139
|
+
Py_tp_token = 83 # New in 3.14
|
|
140
|
+
|
|
141
|
+
void* PyType_GetSlot(type t, int slot) except? NULL
|
|
142
|
+
# Return the function pointer stored in the given slot.
|
|
143
|
+
# If the result is NULL, this indicates that either the slot is NULL,
|
|
144
|
+
# or that the function was called with invalid parameters and raised an exception.
|
|
145
|
+
# In Python 3.9, this function only supports heap types and raises otherwise.
|
|
146
|
+
# Callers will typically cast the result pointer into the appropriate function type.
|
|
@@ -124,3 +124,93 @@ cdef extern from *:
|
|
|
124
124
|
# Dummy inline function to force __Pyx_CppExn2PyErr to be included for utility code above
|
|
125
125
|
cdef inline void __pyx_call_rethrow_exception "__pyx_call_rethrow_exception"(exception_ptr e) except*:
|
|
126
126
|
rethrow_exception(e)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
cdef extern from *:
|
|
130
|
+
"""
|
|
131
|
+
namespace {
|
|
132
|
+
#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
|
|
133
|
+
template <typename ExceptionT>
|
|
134
|
+
bool __pyx_call_exception_handler(void (*handler)(ExceptionT&)) {
|
|
135
|
+
try {
|
|
136
|
+
throw;
|
|
137
|
+
} catch(ExceptionT &ex) {
|
|
138
|
+
handler(ex);
|
|
139
|
+
return static_cast<bool>(PyErr_Occurred());
|
|
140
|
+
} catch (...) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
bool __pyx_call_exception_handler(void(*handler)()) {
|
|
146
|
+
try {
|
|
147
|
+
throw;
|
|
148
|
+
} catch (...) {
|
|
149
|
+
handler();
|
|
150
|
+
return static_cast<bool>(PyErr_Occurred());
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
template <typename ...HandlerTs>
|
|
155
|
+
void __pyx_delegate_to_exception_handlers(HandlerTs&& ... handlers) {
|
|
156
|
+
if (!std::current_exception()) {
|
|
157
|
+
PyErr_SetString(PyExc_SystemError, "__pyx_delegrate_to_exception_handlers called without a C++ exception");
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
bool handled = (__pyx_call_exception_handler(std::forward<HandlerTs>(handlers)) || ... );
|
|
161
|
+
if (!handled)
|
|
162
|
+
throw;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#elif __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
|
|
166
|
+
|
|
167
|
+
inline bool __pyx_call_exception_handler_recursive() {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
template <typename ExceptionT, typename ...HandlerTs>
|
|
172
|
+
bool __pyx_call_exception_handler_recursive(void (*first_handler)(ExceptionT&), HandlerTs&& ...handlers);
|
|
173
|
+
|
|
174
|
+
template <typename ...HandlerTs>
|
|
175
|
+
bool __pyx_call_exception_handler_recursive(void (*first_handler)(), HandlerTs&& ...handlers) {
|
|
176
|
+
try {
|
|
177
|
+
throw;
|
|
178
|
+
} catch (...) {
|
|
179
|
+
first_handler();
|
|
180
|
+
if (static_cast<bool>(PyErr_Occurred())) return true;
|
|
181
|
+
}
|
|
182
|
+
return __pyx_call_exception_handler_recursive(std::forward<HandlerTs>(handlers)...);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
template <typename ExceptionT, typename ...HandlerTs>
|
|
186
|
+
bool __pyx_call_exception_handler_recursive(void (*first_handler)(ExceptionT&), HandlerTs&& ...handlers) {
|
|
187
|
+
try {
|
|
188
|
+
throw;
|
|
189
|
+
} catch (ExceptionT& e) {
|
|
190
|
+
first_handler(e);
|
|
191
|
+
if (static_cast<bool>(PyErr_Occurred())) return true;
|
|
192
|
+
} catch (...) {}
|
|
193
|
+
return __pyx_call_exception_handler_recursive(std::forward<HandlerTs>(handlers)...);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
template <typename ...HandlerTs>
|
|
197
|
+
void __pyx_delegate_to_exception_handlers(HandlerTs&& ... handlers) {
|
|
198
|
+
if (!std::current_exception()) {
|
|
199
|
+
PyErr_SetString(PyExc_SystemError, "__pyx_delegrate_to_exception_handlers called without a C++ exception");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
bool handled = __pyx_call_exception_handler_recursive(std::forward<HandlerTs>(handlers)...);
|
|
203
|
+
if (!handled)
|
|
204
|
+
throw;
|
|
205
|
+
}
|
|
206
|
+
#endif
|
|
207
|
+
} // anonymous namespace
|
|
208
|
+
"""
|
|
209
|
+
# Convenience function for writing easy custom C++ exception handlers.
|
|
210
|
+
# Pass it a collection of C++ function pointers taking either an exception reference, or no arguments.
|
|
211
|
+
# The function pointers are tried in order. If their argument type matches the current C++ exception (or if
|
|
212
|
+
# their argument type is empty) then they are called.
|
|
213
|
+
# If they raise a Python exception then the exception handling finishes and that exception is raised.
|
|
214
|
+
# Otherwise the next handler will be called.
|
|
215
|
+
# If no handlers raise a Python exception then the default Cython mechanism will be used.
|
|
216
|
+
void delegate_to_exception_handlers "__pyx_delegate_to_exception_handlers"(...) except+*
|
Cython/LZSS.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""
|
|
2
|
+
LZSS-like compressor.
|
|
3
|
+
|
|
4
|
+
See decompression function in StringTools.c, "DecompressString_LZSS".
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections import defaultdict
|
|
10
|
+
|
|
11
|
+
import cython
|
|
12
|
+
|
|
13
|
+
PRINT_STATS = False
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def lzss_compress(data: bytes) -> bytes:
|
|
17
|
+
"""
|
|
18
|
+
LZSS-like compressor.
|
|
19
|
+
|
|
20
|
+
Uses a byte triplet dict for fast match finding.
|
|
21
|
+
Outputs a bit stream with small overhead.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
data: Input bytes to compress
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
Compressed bytes (no header/metadata, decompressor must know output size)
|
|
28
|
+
"""
|
|
29
|
+
if not data:
|
|
30
|
+
return b''
|
|
31
|
+
|
|
32
|
+
input_size = len(data)
|
|
33
|
+
|
|
34
|
+
# Hash table: maps 3-byte sequences to a list of positions starting with that sequence.
|
|
35
|
+
hash_table = defaultdict(list)
|
|
36
|
+
|
|
37
|
+
def find_longest_match(pos: cython.Py_ssize_t) -> tuple[cython.Py_ssize_t, cython.Py_ssize_t]:
|
|
38
|
+
"""Find longest match in sliding window. Returns (offset, length)."""
|
|
39
|
+
if pos + 3 > input_size:
|
|
40
|
+
return (0, 0)
|
|
41
|
+
|
|
42
|
+
# 14-bit offset (max 16KiB lookback)
|
|
43
|
+
WINDOW_SIZE: cython.long = 1 << 14
|
|
44
|
+
# 8-bit length + 3, allowing up to 258 bytes.
|
|
45
|
+
MAX_MATCH: cython.long = min(255 + 3, input_size - pos)
|
|
46
|
+
|
|
47
|
+
best_len: cython.Py_ssize_t = 0
|
|
48
|
+
best_offset: cython.Py_ssize_t = 0
|
|
49
|
+
window_start: cython.Py_ssize_t = max(0, pos - WINDOW_SIZE - MAX_MATCH)
|
|
50
|
+
|
|
51
|
+
prev_pos: cython.Py_ssize_t
|
|
52
|
+
match_len: cython.long
|
|
53
|
+
max_len: cython.long
|
|
54
|
+
|
|
55
|
+
# Check all positions starting with the same 3-byte sequence.
|
|
56
|
+
key = data[pos:pos+3]
|
|
57
|
+
if key not in hash_table:
|
|
58
|
+
return (0, 0)
|
|
59
|
+
|
|
60
|
+
for prev_pos in hash_table[key]:
|
|
61
|
+
if prev_pos < window_start or prev_pos >= pos:
|
|
62
|
+
continue
|
|
63
|
+
|
|
64
|
+
# Extend match as far as possible.
|
|
65
|
+
match_len = 3
|
|
66
|
+
max_len = min(MAX_MATCH, pos - prev_pos)
|
|
67
|
+
while match_len < max_len and data[prev_pos + match_len] == data[pos + match_len]:
|
|
68
|
+
match_len += 1
|
|
69
|
+
|
|
70
|
+
# Prefer longer matches, break ties by preferring closer offsets.
|
|
71
|
+
if match_len > best_len:
|
|
72
|
+
best_len = match_len
|
|
73
|
+
best_offset = pos - prev_pos
|
|
74
|
+
|
|
75
|
+
# Lazy matching: check if next position has a better match.
|
|
76
|
+
next_key = data[pos + 1:pos + 4]
|
|
77
|
+
if 0 < best_len < MAX_MATCH and next_key in hash_table and pos + best_len + 1 < input_size:
|
|
78
|
+
next_best_len: cython.Py_ssize_t = 0
|
|
79
|
+
window_start = max(0, pos + 1 - WINDOW_SIZE - MAX_MATCH)
|
|
80
|
+
|
|
81
|
+
for prev_pos in hash_table[next_key]:
|
|
82
|
+
if prev_pos < window_start:
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
match_len = 3
|
|
86
|
+
max_len = min(MAX_MATCH, pos - prev_pos, input_size - pos - 1)
|
|
87
|
+
while match_len < max_len and data[prev_pos + match_len] == data[pos + 1 + match_len]:
|
|
88
|
+
match_len += 1
|
|
89
|
+
|
|
90
|
+
if match_len > next_best_len:
|
|
91
|
+
next_best_len = match_len
|
|
92
|
+
|
|
93
|
+
# If the next position has a significantly better match, ignore this one.
|
|
94
|
+
if next_best_len > best_len + 1:
|
|
95
|
+
best_offset = best_len = 0
|
|
96
|
+
|
|
97
|
+
return (best_offset, best_len)
|
|
98
|
+
|
|
99
|
+
# Build output as bit stream.
|
|
100
|
+
output = bytearray(b'\0')
|
|
101
|
+
pos: cython.Py_ssize_t = 0
|
|
102
|
+
flags_pos: cython.Py_ssize_t = 0
|
|
103
|
+
flags: cython.int = 0xFF0000
|
|
104
|
+
|
|
105
|
+
stats = [0, 0, 0, 0]
|
|
106
|
+
|
|
107
|
+
while pos < input_size:
|
|
108
|
+
offset, length = find_longest_match(pos)
|
|
109
|
+
|
|
110
|
+
#if offset > 0:
|
|
111
|
+
# assert data[pos:pos+length] == data[pos-offset:pos-offset+length], (
|
|
112
|
+
# data[pos:pos+3], data[pos-offset:pos-offset+length])
|
|
113
|
+
|
|
114
|
+
# Update hash table for current position.
|
|
115
|
+
key = data[pos:pos+3]
|
|
116
|
+
hash_table[key].append(pos)
|
|
117
|
+
|
|
118
|
+
flag = 0
|
|
119
|
+
offset -= length # offset >= length if interesting, so remove redundancy.
|
|
120
|
+
|
|
121
|
+
# See if the match is worth it and store a single literal byte otherwise.
|
|
122
|
+
|
|
123
|
+
if length > 2 and 0 <= offset <= 0x7F:
|
|
124
|
+
# Store 7 bit offset and length as two bytes.
|
|
125
|
+
output.append(offset)
|
|
126
|
+
#assert output[-1] & 0x80 == 0
|
|
127
|
+
output.append(length - 3)
|
|
128
|
+
stats[1] += 1
|
|
129
|
+
elif length > 2 and length - 3 <= 0x1F and 0 <= offset <= 0x1FF:
|
|
130
|
+
# Store a longer 7+2 bit offset at the cost of a shorter 5 bit length.
|
|
131
|
+
output.append((offset & 0x7F) | 0x80)
|
|
132
|
+
output.append(((offset & 0x180) >> 2) | (length - 3))
|
|
133
|
+
#assert output[-1] & 0x80 == 0
|
|
134
|
+
stats[2] += 1
|
|
135
|
+
elif length > 3 and 0 <= offset < (1 << 14):
|
|
136
|
+
# Store a 7+7 bit offset with a separate 8 bit length.
|
|
137
|
+
output.append(offset & 0x7F | 0x80)
|
|
138
|
+
output.append((offset >> 7) & 0x7F | 0x80)
|
|
139
|
+
output.append(length - 3)
|
|
140
|
+
stats[3] += 1
|
|
141
|
+
else:
|
|
142
|
+
# Encode 8 bit literal + 1 bit flag.
|
|
143
|
+
flag = 1
|
|
144
|
+
length = 1
|
|
145
|
+
output.append(data[pos])
|
|
146
|
+
stats[0] += 1
|
|
147
|
+
|
|
148
|
+
pos += length
|
|
149
|
+
|
|
150
|
+
flags = (flag << 7) | (flags >> 1)
|
|
151
|
+
if flags < 0x10000:
|
|
152
|
+
output[flags_pos] = flags & 0xFF
|
|
153
|
+
flags_pos = len(output)
|
|
154
|
+
output.append(0) # next flags
|
|
155
|
+
flags = 0xFF0000
|
|
156
|
+
|
|
157
|
+
if flags_pos == len(output) - 1:
|
|
158
|
+
output.pop()
|
|
159
|
+
else:
|
|
160
|
+
# Pad flags with empty non-literals
|
|
161
|
+
# - decompressor must stop at the end of the output buffer !
|
|
162
|
+
while flags >= 0x10000:
|
|
163
|
+
flags >>= 1
|
|
164
|
+
output[flags_pos] = flags & 0xFF
|
|
165
|
+
|
|
166
|
+
if PRINT_STATS:
|
|
167
|
+
print("BYTES:", input_size, "->", len(output))
|
|
168
|
+
print("ENCODINGS:", stats)
|
|
169
|
+
|
|
170
|
+
return bytes(output)
|