├── MANIFEST.in ├── tests ├── __init__.py └── leven_test.py ├── leven ├── __init__.py ├── levenshtein_types.h ├── levenshtein_impl.h ├── _levenshtein.pyx └── _levenshtein.cpp ├── benchmark.py ├── .gitignore ├── setup.py ├── README.rst └── LICENSE /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include leven/*.h 2 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # feeling zen? 2 | -------------------------------------------------------------------------------- /leven/__init__.py: -------------------------------------------------------------------------------- 1 | from ._levenshtein import levenshtein 2 | 3 | __version__ = '1.0.4' 4 | -------------------------------------------------------------------------------- /benchmark.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from timeit import timeit 3 | 4 | from leven import levenshtein 5 | 6 | n_runs = 30 7 | 8 | with open("/usr/share/dict/words") as f: 9 | strs = [ln.strip() for ln in f] 10 | print("Running %d times on %d reference strings" % (n_runs, len(strs))) 11 | 12 | arg = sys.argv[1] 13 | t = timeit(stmt='for s in strs: levenshtein(arg, s)', 14 | setup='from __main__ import arg, levenshtein, strs', 15 | number=n_runs) 16 | print("%.3f seconds" % t) 17 | -------------------------------------------------------------------------------- /leven/levenshtein_types.h: -------------------------------------------------------------------------------- 1 | // Work around lack of template functions in Cython. 2 | 3 | #include "levenshtein_impl.h" 4 | 5 | static unsigned levenshtein_char(char const *a, size_t m, 6 | char const *b, size_t n) 7 | { 8 | return levenshtein(a, m, b, n); 9 | } 10 | 11 | static unsigned levenshtein_Py_UNICODE(Py_UNICODE const *a, size_t m, 12 | Py_UNICODE const *b, size_t n) 13 | { 14 | return levenshtein(a, m, b, n); 15 | } 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Editor temporaries 2 | *.sw[op] 3 | *~ 4 | 5 | *.py[cod] 6 | 7 | # C extensions 8 | *.so 9 | 10 | MANIFEST 11 | 12 | # Packages 13 | *.egg 14 | *.egg-info 15 | dist 16 | build 17 | eggs 18 | parts 19 | bin 20 | var 21 | sdist 22 | develop-eggs 23 | .installed.cfg 24 | lib 25 | lib64 26 | __pycache__ 27 | 28 | # Installer logs 29 | pip-log.txt 30 | 31 | # Unit test / coverage reports 32 | .coverage 33 | .tox 34 | nosetests.xml 35 | 36 | # Translations 37 | *.mo 38 | 39 | # Mr Developer 40 | .mr.developer.cfg 41 | .project 42 | .pydevproject 43 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, Extension 2 | 3 | setup( 4 | name='leven', 5 | version='1.0.4', 6 | description='Levenshtein edit distance library', 7 | maintainer='Lars Buitinck', 8 | maintainer_email='l.j.buitinck@esciencecenter.nl', 9 | packages=['leven'], 10 | ext_modules=[ 11 | Extension("leven._levenshtein", ["leven/_levenshtein.cpp"], 12 | include_dirs=["leven"]) 13 | ], 14 | install_requires=["six", "nose"], 15 | url='https://github.com/semanticize/leven', 16 | test_suite='nose.collector' 17 | ) 18 | -------------------------------------------------------------------------------- /tests/leven_test.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | from nose.tools import assert_equal, assert_raises, assert_true 4 | 5 | from leven import levenshtein 6 | import numbers 7 | from six import b 8 | 9 | 10 | S1 = b("kitten") 11 | S2 = b("sitting") 12 | 13 | U1 = S1.decode("utf-8") 14 | U2 = S2.decode("utf-8") 15 | 16 | 17 | def test_distance(): 18 | assert_equal(levenshtein(S1, S2), 3) 19 | assert_equal(levenshtein(U1, U2), 3) 20 | assert_equal(levenshtein(S2, S1), 3) 21 | assert_equal(levenshtein(U2, U1), 3) 22 | 23 | for x in (S1, S2, U1, U2): 24 | assert_equal(levenshtein(x, x), 0) 25 | 26 | 27 | def test_normalize(): 28 | assert_true(isinstance(levenshtein(S2, S1), numbers.Integral)) 29 | assert_equal(levenshtein("", "", normalize=True), 0) 30 | assert_equal(levenshtein(S1, S2, normalize=True), 3 / 7) 31 | 32 | 33 | def test_types(): 34 | assert_raises(TypeError, levenshtein, S1, U2) 35 | assert_raises(TypeError, levenshtein, U1, S2) 36 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Leven 2 | ===== 3 | 4 | Levenshtein edit distance library for Python, Apache-licensed. 5 | Written by Lars Buitinck, Netherlands eScience Center, with contributions 6 | from Isaac Sijaranamual, University of Amsterdam. 7 | 8 | Performs distance computations on either byte strings or Unicode codepoints. 9 | 10 | Installation 11 | ------------ 12 | 13 | Make sure you have Cython and a C++ compiler installed:: 14 | 15 | pip install cython 16 | 17 | Installing a C++ compiler is so platform-dependent that I won't show 18 | instructions. Consult your package manager. 19 | 20 | Then:: 21 | 22 | python setup.py install 23 | 24 | To run the tests, but not to actually use leven, you need six and Nose. 25 | 26 | Usage 27 | ----- 28 | 29 | >>> from leven import levenshtein 30 | >>> levenshtein("hello, world!", "goodbye, cruel world!") 31 | 13 32 | 33 | About the implementation 34 | ------------------------ 35 | 36 | The core algorithms have been implemented in C++. I used this instead of C 37 | to get templates, easier memory management and a better standard library, 38 | so the C++ code probably looks C-ish. 39 | 40 | Todo 41 | ---- 42 | 43 | * Implement Ukkonen's algorithm for bounded Levenshtein distance 44 | * Implement Levenshtein automata for fast neighbor search in string spaces 45 | * Implement weighted Levenshtein distance 46 | -------------------------------------------------------------------------------- /leven/levenshtein_impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Levenshtein distance using the classic DP algorithm. 3 | * 4 | * Copyright 2013 Netherlands eScience Center 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #ifndef levenshtein_impl_h 19 | #define levenshtein_impl_h 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | static unsigned min(unsigned a, unsigned b, unsigned c) 27 | { 28 | return std::min(a, std::min(b, c)); 29 | } 30 | 31 | template 32 | static unsigned levenshtein(Char const *a, size_t m, Char const *b, size_t n) 33 | { 34 | // Swap a and b if necessary to ensure m <= n. 35 | if (m > n) { 36 | std::swap(a, b); 37 | std::swap(m, n); 38 | } 39 | 40 | // Skip common prefix. 41 | while (m > 0 && n > 0 && *a == *b) { 42 | a++, b++; 43 | m--, n--; 44 | } 45 | 46 | // Skip common suffix. 47 | while (m > 0 && n > 0 && a[m - 1] == b[n - 1]) { 48 | m--, n--; 49 | } 50 | 51 | // To save memory, we use unsigned as the type in the DP table. 52 | if (m > UINT_MAX || n > UINT_MAX) { 53 | throw std::length_error("string too long in Levenshtein distance."); 54 | } 55 | 56 | if (m == 0) { 57 | return n; 58 | } 59 | if (n == 0) { 60 | return m; 61 | } 62 | 63 | std::vector tab((m + 1) * 2); 64 | unsigned *cur = tab.data(), *prev = tab.data() + m + 1; 65 | 66 | for (size_t i = 0; i <= m; i++) { 67 | cur[i] = i; 68 | } 69 | for (size_t j = 1; j <= n; j++) { 70 | std::swap(cur, prev); 71 | 72 | cur[0] = j; 73 | 74 | for (size_t i = 1; i <= m; i++) { 75 | if (a[i - 1] == b[j - 1]) { 76 | cur[i] = prev[i - 1]; 77 | } 78 | else { 79 | cur[i] = min(cur[i - 1], prev[i], prev[i - 1]) + 1; 80 | } 81 | } 82 | } 83 | 84 | return cur[m]; 85 | } 86 | 87 | #endif // levenshtein_impl_h 88 | -------------------------------------------------------------------------------- /leven/_levenshtein.pyx: -------------------------------------------------------------------------------- 1 | # Copyright 2013 Netherlands eScience Center 2 | # Written by Lars Buitinck 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | # distutils: language = c++ 17 | 18 | from cpython cimport PyUnicode_AS_UNICODE, PyUnicode_GET_SIZE 19 | 20 | 21 | cdef extern from "levenshtein_types.h": 22 | unsigned levenshtein_char(const char *, size_t, 23 | const char *, size_t) except + 24 | unsigned levenshtein_Py_UNICODE(const Py_UNICODE *, size_t, 25 | const Py_UNICODE *, size_t) except + 26 | 27 | 28 | def levenshtein(a, b, normalize=False): 29 | """Returns the Levenshtein (edit) distance between a and b. 30 | 31 | When a and b are both byte strings (type bytes), the distance is computed 32 | bytewise. When a and b are both Unicode strings (type unicode in Python 2, 33 | str in Python 3), the distance is computed codepoint-wise. 34 | 35 | Other combinations of types will cause a TypeError to be raised. 36 | 37 | When passing Unicode strings, make sure they use the same normalized form 38 | (use the function unicodedata.normalize in the standard library). 39 | 40 | The normalize parameter can be used to normalize the distance by the length 41 | of the longest input. 42 | 43 | Example 44 | ------- 45 | >>> levenshtein("Python", "Schmython") 46 | 4 47 | """ 48 | cdef unsigned d 49 | cdef size_t m = len(a), n = len(b) 50 | 51 | if isinstance(a, bytes) and isinstance(b, bytes): 52 | d = levenshtein_char(a, m, b, n) 53 | elif isinstance(a, unicode) and isinstance(b, unicode): 54 | d = levenshtein_Py_UNICODE(PyUnicode_AS_UNICODE(a), m, 55 | PyUnicode_AS_UNICODE(b), n) 56 | else: 57 | raise TypeError("Type mismatch: expected (bytes, bytes) or ({0}, {0})," 58 | " got ({1}, {2})".format(unicode, type(a), type(b))) 59 | 60 | if normalize: 61 | return (d) / (max(m, n, 1U)) 62 | else: 63 | return d 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /leven/_levenshtein.cpp: -------------------------------------------------------------------------------- 1 | /* Generated by Cython 0.19.2 on Mon Nov 18 16:13:29 2013 */ 2 | 3 | #define PY_SSIZE_T_CLEAN 4 | #ifndef CYTHON_USE_PYLONG_INTERNALS 5 | #ifdef PYLONG_BITS_IN_DIGIT 6 | #define CYTHON_USE_PYLONG_INTERNALS 0 7 | #else 8 | #include "pyconfig.h" 9 | #ifdef PYLONG_BITS_IN_DIGIT 10 | #define CYTHON_USE_PYLONG_INTERNALS 1 11 | #else 12 | #define CYTHON_USE_PYLONG_INTERNALS 0 13 | #endif 14 | #endif 15 | #endif 16 | #include "Python.h" 17 | #ifndef Py_PYTHON_H 18 | #error Python headers needed to compile C extensions, please install development version of Python. 19 | #elif PY_VERSION_HEX < 0x02040000 20 | #error Cython requires Python 2.4+. 21 | #else 22 | #include /* For offsetof */ 23 | #ifndef offsetof 24 | #define offsetof(type, member) ( (size_t) & ((type*)0) -> member ) 25 | #endif 26 | #if !defined(WIN32) && !defined(MS_WINDOWS) 27 | #ifndef __stdcall 28 | #define __stdcall 29 | #endif 30 | #ifndef __cdecl 31 | #define __cdecl 32 | #endif 33 | #ifndef __fastcall 34 | #define __fastcall 35 | #endif 36 | #endif 37 | #ifndef DL_IMPORT 38 | #define DL_IMPORT(t) t 39 | #endif 40 | #ifndef DL_EXPORT 41 | #define DL_EXPORT(t) t 42 | #endif 43 | #ifndef PY_LONG_LONG 44 | #define PY_LONG_LONG LONG_LONG 45 | #endif 46 | #ifndef Py_HUGE_VAL 47 | #define Py_HUGE_VAL HUGE_VAL 48 | #endif 49 | #ifdef PYPY_VERSION 50 | #define CYTHON_COMPILING_IN_PYPY 1 51 | #define CYTHON_COMPILING_IN_CPYTHON 0 52 | #else 53 | #define CYTHON_COMPILING_IN_PYPY 0 54 | #define CYTHON_COMPILING_IN_CPYTHON 1 55 | #endif 56 | #if PY_VERSION_HEX < 0x02050000 57 | typedef int Py_ssize_t; 58 | #define PY_SSIZE_T_MAX INT_MAX 59 | #define PY_SSIZE_T_MIN INT_MIN 60 | #define PY_FORMAT_SIZE_T "" 61 | #define CYTHON_FORMAT_SSIZE_T "" 62 | #define PyInt_FromSsize_t(z) PyInt_FromLong(z) 63 | #define PyInt_AsSsize_t(o) __Pyx_PyInt_AsInt(o) 64 | #define PyNumber_Index(o) ((PyNumber_Check(o) && !PyFloat_Check(o)) ? PyNumber_Int(o) : \ 65 | (PyErr_Format(PyExc_TypeError, \ 66 | "expected index value, got %.200s", Py_TYPE(o)->tp_name), \ 67 | (PyObject*)0)) 68 | #define __Pyx_PyIndex_Check(o) (PyNumber_Check(o) && !PyFloat_Check(o) && \ 69 | !PyComplex_Check(o)) 70 | #define PyIndex_Check __Pyx_PyIndex_Check 71 | #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message) 72 | #define __PYX_BUILD_PY_SSIZE_T "i" 73 | #else 74 | #define __PYX_BUILD_PY_SSIZE_T "n" 75 | #define CYTHON_FORMAT_SSIZE_T "z" 76 | #define __Pyx_PyIndex_Check PyIndex_Check 77 | #endif 78 | #if PY_VERSION_HEX < 0x02060000 79 | #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) 80 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 81 | #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size) 82 | #define PyVarObject_HEAD_INIT(type, size) \ 83 | PyObject_HEAD_INIT(type) size, 84 | #define PyType_Modified(t) 85 | typedef struct { 86 | void *buf; 87 | PyObject *obj; 88 | Py_ssize_t len; 89 | Py_ssize_t itemsize; 90 | int readonly; 91 | int ndim; 92 | char *format; 93 | Py_ssize_t *shape; 94 | Py_ssize_t *strides; 95 | Py_ssize_t *suboffsets; 96 | void *internal; 97 | } Py_buffer; 98 | #define PyBUF_SIMPLE 0 99 | #define PyBUF_WRITABLE 0x0001 100 | #define PyBUF_FORMAT 0x0004 101 | #define PyBUF_ND 0x0008 102 | #define PyBUF_STRIDES (0x0010 | PyBUF_ND) 103 | #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES) 104 | #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES) 105 | #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES) 106 | #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES) 107 | #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_FORMAT | PyBUF_WRITABLE) 108 | #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_FORMAT | PyBUF_WRITABLE) 109 | typedef int (*getbufferproc)(PyObject *, Py_buffer *, int); 110 | typedef void (*releasebufferproc)(PyObject *, Py_buffer *); 111 | #endif 112 | #if PY_MAJOR_VERSION < 3 113 | #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" 114 | #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \ 115 | PyCode_New(a, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) 116 | #else 117 | #define __Pyx_BUILTIN_MODULE_NAME "builtins" 118 | #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \ 119 | PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) 120 | #endif 121 | #if PY_MAJOR_VERSION < 3 && PY_MINOR_VERSION < 6 122 | #define PyUnicode_FromString(s) PyUnicode_Decode(s, strlen(s), "UTF-8", "strict") 123 | #endif 124 | #if PY_MAJOR_VERSION >= 3 125 | #define Py_TPFLAGS_CHECKTYPES 0 126 | #define Py_TPFLAGS_HAVE_INDEX 0 127 | #endif 128 | #if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3) 129 | #define Py_TPFLAGS_HAVE_NEWBUFFER 0 130 | #endif 131 | #if PY_VERSION_HEX < 0x02060000 132 | #define Py_TPFLAGS_HAVE_VERSION_TAG 0 133 | #endif 134 | #if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND) 135 | #define CYTHON_PEP393_ENABLED 1 136 | #define __Pyx_PyUnicode_READY(op) (likely(PyUnicode_IS_READY(op)) ? \ 137 | 0 : _PyUnicode_Ready((PyObject *)(op))) 138 | #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_LENGTH(u) 139 | #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i) 140 | #define __Pyx_PyUnicode_READ(k, d, i) PyUnicode_READ(k, d, i) 141 | #else 142 | #define CYTHON_PEP393_ENABLED 0 143 | #define __Pyx_PyUnicode_READY(op) (0) 144 | #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u) 145 | #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i])) 146 | #define __Pyx_PyUnicode_READ(k, d, i) ((k=k), (Py_UCS4)(((Py_UNICODE*)d)[i])) 147 | #endif 148 | #if PY_MAJOR_VERSION >= 3 149 | #define PyBaseString_Type PyUnicode_Type 150 | #define PyStringObject PyUnicodeObject 151 | #define PyString_Type PyUnicode_Type 152 | #define PyString_Check PyUnicode_Check 153 | #define PyString_CheckExact PyUnicode_CheckExact 154 | #endif 155 | #if PY_VERSION_HEX < 0x02060000 156 | #define PyBytesObject PyStringObject 157 | #define PyBytes_Type PyString_Type 158 | #define PyBytes_Check PyString_Check 159 | #define PyBytes_CheckExact PyString_CheckExact 160 | #define PyBytes_FromString PyString_FromString 161 | #define PyBytes_FromStringAndSize PyString_FromStringAndSize 162 | #define PyBytes_FromFormat PyString_FromFormat 163 | #define PyBytes_DecodeEscape PyString_DecodeEscape 164 | #define PyBytes_AsString PyString_AsString 165 | #define PyBytes_AsStringAndSize PyString_AsStringAndSize 166 | #define PyBytes_Size PyString_Size 167 | #define PyBytes_AS_STRING PyString_AS_STRING 168 | #define PyBytes_GET_SIZE PyString_GET_SIZE 169 | #define PyBytes_Repr PyString_Repr 170 | #define PyBytes_Concat PyString_Concat 171 | #define PyBytes_ConcatAndDel PyString_ConcatAndDel 172 | #endif 173 | #if PY_MAJOR_VERSION >= 3 174 | #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj) 175 | #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj) 176 | #else 177 | #define __Pyx_PyBaseString_Check(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj) || \ 178 | PyString_Check(obj) || PyUnicode_Check(obj)) 179 | #define __Pyx_PyBaseString_CheckExact(obj) (Py_TYPE(obj) == &PyBaseString_Type) 180 | #endif 181 | #if PY_VERSION_HEX < 0x02060000 182 | #define PySet_Check(obj) PyObject_TypeCheck(obj, &PySet_Type) 183 | #define PyFrozenSet_Check(obj) PyObject_TypeCheck(obj, &PyFrozenSet_Type) 184 | #endif 185 | #ifndef PySet_CheckExact 186 | #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type) 187 | #endif 188 | #define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type) 189 | #if PY_MAJOR_VERSION >= 3 190 | #define PyIntObject PyLongObject 191 | #define PyInt_Type PyLong_Type 192 | #define PyInt_Check(op) PyLong_Check(op) 193 | #define PyInt_CheckExact(op) PyLong_CheckExact(op) 194 | #define PyInt_FromString PyLong_FromString 195 | #define PyInt_FromUnicode PyLong_FromUnicode 196 | #define PyInt_FromLong PyLong_FromLong 197 | #define PyInt_FromSize_t PyLong_FromSize_t 198 | #define PyInt_FromSsize_t PyLong_FromSsize_t 199 | #define PyInt_AsLong PyLong_AsLong 200 | #define PyInt_AS_LONG PyLong_AS_LONG 201 | #define PyInt_AsSsize_t PyLong_AsSsize_t 202 | #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask 203 | #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask 204 | #endif 205 | #if PY_MAJOR_VERSION >= 3 206 | #define PyBoolObject PyLongObject 207 | #endif 208 | #if PY_VERSION_HEX < 0x03020000 209 | typedef long Py_hash_t; 210 | #define __Pyx_PyInt_FromHash_t PyInt_FromLong 211 | #define __Pyx_PyInt_AsHash_t PyInt_AsLong 212 | #else 213 | #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t 214 | #define __Pyx_PyInt_AsHash_t PyInt_AsSsize_t 215 | #endif 216 | #if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300) 217 | #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b) 218 | #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value) 219 | #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b) 220 | #else 221 | #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \ 222 | (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), (PyObject*)0) : \ 223 | (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \ 224 | (PyErr_Format(PyExc_TypeError, "'%.200s' object is unsliceable", (obj)->ob_type->tp_name), (PyObject*)0))) 225 | #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \ 226 | (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \ 227 | (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \ 228 | (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice assignment", (obj)->ob_type->tp_name), -1))) 229 | #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \ 230 | (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \ 231 | (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \ 232 | (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice deletion", (obj)->ob_type->tp_name), -1))) 233 | #endif 234 | #if PY_MAJOR_VERSION >= 3 235 | #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func)) 236 | #endif 237 | #if PY_VERSION_HEX < 0x02050000 238 | #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n))) 239 | #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a)) 240 | #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n))) 241 | #else 242 | #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n)) 243 | #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a)) 244 | #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n)) 245 | #endif 246 | #if PY_VERSION_HEX < 0x02050000 247 | #define __Pyx_NAMESTR(n) ((char *)(n)) 248 | #define __Pyx_DOCSTR(n) ((char *)(n)) 249 | #else 250 | #define __Pyx_NAMESTR(n) (n) 251 | #define __Pyx_DOCSTR(n) (n) 252 | #endif 253 | #ifndef CYTHON_INLINE 254 | #if defined(__GNUC__) 255 | #define CYTHON_INLINE __inline__ 256 | #elif defined(_MSC_VER) 257 | #define CYTHON_INLINE __inline 258 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 259 | #define CYTHON_INLINE inline 260 | #else 261 | #define CYTHON_INLINE 262 | #endif 263 | #endif 264 | #ifndef CYTHON_RESTRICT 265 | #if defined(__GNUC__) 266 | #define CYTHON_RESTRICT __restrict__ 267 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 268 | #define CYTHON_RESTRICT __restrict 269 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 270 | #define CYTHON_RESTRICT restrict 271 | #else 272 | #define CYTHON_RESTRICT 273 | #endif 274 | #endif 275 | #ifdef NAN 276 | #define __PYX_NAN() ((float) NAN) 277 | #else 278 | static CYTHON_INLINE float __PYX_NAN() { 279 | /* Initialize NaN. The sign is irrelevant, an exponent with all bits 1 and 280 | a nonzero mantissa means NaN. If the first bit in the mantissa is 1, it is 281 | a quiet NaN. */ 282 | float value; 283 | memset(&value, 0xFF, sizeof(value)); 284 | return value; 285 | } 286 | #endif 287 | 288 | 289 | #if PY_MAJOR_VERSION >= 3 290 | #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) 291 | #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) 292 | #else 293 | #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y) 294 | #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y) 295 | #endif 296 | 297 | #ifndef __PYX_EXTERN_C 298 | #ifdef __cplusplus 299 | #define __PYX_EXTERN_C extern "C" 300 | #else 301 | #define __PYX_EXTERN_C extern 302 | #endif 303 | #endif 304 | 305 | #if defined(WIN32) || defined(MS_WINDOWS) 306 | #define _USE_MATH_DEFINES 307 | #endif 308 | #include 309 | #define __PYX_HAVE__leven___levenshtein 310 | #define __PYX_HAVE_API__leven___levenshtein 311 | #include "string.h" 312 | #include "stdio.h" 313 | #include "pythread.h" 314 | #include "levenshtein_types.h" 315 | #include "ios" 316 | #include "new" 317 | #include "stdexcept" 318 | #include "typeinfo" 319 | #ifdef _OPENMP 320 | #include 321 | #endif /* _OPENMP */ 322 | 323 | #ifdef PYREX_WITHOUT_ASSERTIONS 324 | #define CYTHON_WITHOUT_ASSERTIONS 325 | #endif 326 | 327 | #ifndef CYTHON_UNUSED 328 | # if defined(__GNUC__) 329 | # if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) 330 | # define CYTHON_UNUSED __attribute__ ((__unused__)) 331 | # else 332 | # define CYTHON_UNUSED 333 | # endif 334 | # elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER)) 335 | # define CYTHON_UNUSED __attribute__ ((__unused__)) 336 | # else 337 | # define CYTHON_UNUSED 338 | # endif 339 | #endif 340 | typedef struct {PyObject **p; char *s; const Py_ssize_t n; const char* encoding; 341 | const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/ 342 | 343 | #define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0 344 | #define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 0 345 | #define __PYX_DEFAULT_STRING_ENCODING "" 346 | #define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString 347 | #define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize 348 | static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject*); 349 | static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length); 350 | #define __Pyx_PyBytes_FromString PyBytes_FromString 351 | #define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize 352 | static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char*); 353 | #if PY_MAJOR_VERSION < 3 354 | #define __Pyx_PyStr_FromString __Pyx_PyBytes_FromString 355 | #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize 356 | #else 357 | #define __Pyx_PyStr_FromString __Pyx_PyUnicode_FromString 358 | #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize 359 | #endif 360 | #define __Pyx_PyObject_AsUString(s) ((unsigned char*) __Pyx_PyObject_AsString(s)) 361 | #define __Pyx_PyObject_FromUString(s) __Pyx_PyObject_FromString((char*)s) 362 | #define __Pyx_PyBytes_FromUString(s) __Pyx_PyBytes_FromString((char*)s) 363 | #define __Pyx_PyStr_FromUString(s) __Pyx_PyStr_FromString((char*)s) 364 | #define __Pyx_PyUnicode_FromUString(s) __Pyx_PyUnicode_FromString((char*)s) 365 | #if PY_MAJOR_VERSION < 3 366 | static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u) 367 | { 368 | const Py_UNICODE *u_end = u; 369 | while (*u_end++) ; 370 | return u_end - u - 1; 371 | } 372 | #else 373 | #define __Pyx_Py_UNICODE_strlen Py_UNICODE_strlen 374 | #endif 375 | #define __Pyx_PyUnicode_FromUnicode(u) PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u)) 376 | #define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode 377 | #define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode 378 | #define __Pyx_Owned_Py_None(b) (Py_INCREF(Py_None), Py_None) 379 | #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False)) 380 | static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); 381 | static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x); 382 | static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); 383 | static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); 384 | static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject*); 385 | #if CYTHON_COMPILING_IN_CPYTHON 386 | #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) 387 | #else 388 | #define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x) 389 | #endif 390 | #define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x)) 391 | #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 392 | static int __Pyx_sys_getdefaultencoding_not_ascii; 393 | static int __Pyx_init_sys_getdefaultencoding_params() { 394 | PyObject* sys = NULL; 395 | PyObject* default_encoding = NULL; 396 | PyObject* ascii_chars_u = NULL; 397 | PyObject* ascii_chars_b = NULL; 398 | sys = PyImport_ImportModule("sys"); 399 | if (sys == NULL) goto bad; 400 | default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL); 401 | if (default_encoding == NULL) goto bad; 402 | if (strcmp(PyBytes_AsString(default_encoding), "ascii") == 0) { 403 | __Pyx_sys_getdefaultencoding_not_ascii = 0; 404 | } else { 405 | const char* default_encoding_c = PyBytes_AS_STRING(default_encoding); 406 | char ascii_chars[128]; 407 | int c; 408 | for (c = 0; c < 128; c++) { 409 | ascii_chars[c] = c; 410 | } 411 | __Pyx_sys_getdefaultencoding_not_ascii = 1; 412 | ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL); 413 | if (ascii_chars_u == NULL) goto bad; 414 | ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL); 415 | if (ascii_chars_b == NULL || strncmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) { 416 | PyErr_Format( 417 | PyExc_ValueError, 418 | "This module compiled with c_string_encoding=ascii, but default encoding '%s' is not a superset of ascii.", 419 | default_encoding_c); 420 | goto bad; 421 | } 422 | } 423 | Py_XDECREF(sys); 424 | Py_XDECREF(default_encoding); 425 | Py_XDECREF(ascii_chars_u); 426 | Py_XDECREF(ascii_chars_b); 427 | return 0; 428 | bad: 429 | Py_XDECREF(sys); 430 | Py_XDECREF(default_encoding); 431 | Py_XDECREF(ascii_chars_u); 432 | Py_XDECREF(ascii_chars_b); 433 | return -1; 434 | } 435 | #endif 436 | #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3 437 | #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL) 438 | #else 439 | #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL) 440 | #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 441 | static char* __PYX_DEFAULT_STRING_ENCODING; 442 | static int __Pyx_init_sys_getdefaultencoding_params() { 443 | PyObject* sys = NULL; 444 | PyObject* default_encoding = NULL; 445 | char* default_encoding_c; 446 | sys = PyImport_ImportModule("sys"); 447 | if (sys == NULL) goto bad; 448 | default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL); 449 | if (default_encoding == NULL) goto bad; 450 | default_encoding_c = PyBytes_AS_STRING(default_encoding); 451 | __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c)); 452 | strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c); 453 | Py_DECREF(sys); 454 | Py_DECREF(default_encoding); 455 | return 0; 456 | bad: 457 | Py_XDECREF(sys); 458 | Py_XDECREF(default_encoding); 459 | return -1; 460 | } 461 | #endif 462 | #endif 463 | 464 | 465 | #ifdef __GNUC__ 466 | /* Test for GCC > 2.95 */ 467 | #if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)) 468 | #define likely(x) __builtin_expect(!!(x), 1) 469 | #define unlikely(x) __builtin_expect(!!(x), 0) 470 | #else /* __GNUC__ > 2 ... */ 471 | #define likely(x) (x) 472 | #define unlikely(x) (x) 473 | #endif /* __GNUC__ > 2 ... */ 474 | #else /* __GNUC__ */ 475 | #define likely(x) (x) 476 | #define unlikely(x) (x) 477 | #endif /* __GNUC__ */ 478 | 479 | static PyObject *__pyx_m; 480 | static PyObject *__pyx_d; 481 | static PyObject *__pyx_b; 482 | static PyObject *__pyx_empty_tuple; 483 | static PyObject *__pyx_empty_bytes; 484 | static int __pyx_lineno; 485 | static int __pyx_clineno = 0; 486 | static const char * __pyx_cfilenm= __FILE__; 487 | static const char *__pyx_filename; 488 | 489 | 490 | static const char *__pyx_f[] = { 491 | "_levenshtein.pyx", 492 | "type.pxd", 493 | "bool.pxd", 494 | "complex.pxd", 495 | }; 496 | 497 | /*--- Type declarations ---*/ 498 | #ifndef CYTHON_REFNANNY 499 | #define CYTHON_REFNANNY 0 500 | #endif 501 | #if CYTHON_REFNANNY 502 | typedef struct { 503 | void (*INCREF)(void*, PyObject*, int); 504 | void (*DECREF)(void*, PyObject*, int); 505 | void (*GOTREF)(void*, PyObject*, int); 506 | void (*GIVEREF)(void*, PyObject*, int); 507 | void* (*SetupContext)(const char*, int, const char*); 508 | void (*FinishContext)(void**); 509 | } __Pyx_RefNannyAPIStruct; 510 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL; 511 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); /*proto*/ 512 | #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL; 513 | #ifdef WITH_THREAD 514 | #define __Pyx_RefNannySetupContext(name, acquire_gil) \ 515 | if (acquire_gil) { \ 516 | PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); \ 517 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \ 518 | PyGILState_Release(__pyx_gilstate_save); \ 519 | } else { \ 520 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \ 521 | } 522 | #else 523 | #define __Pyx_RefNannySetupContext(name, acquire_gil) \ 524 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__) 525 | #endif 526 | #define __Pyx_RefNannyFinishContext() \ 527 | __Pyx_RefNanny->FinishContext(&__pyx_refnanny) 528 | #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 529 | #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 530 | #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 531 | #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 532 | #define __Pyx_XINCREF(r) do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0) 533 | #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0) 534 | #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0) 535 | #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0) 536 | #else 537 | #define __Pyx_RefNannyDeclarations 538 | #define __Pyx_RefNannySetupContext(name, acquire_gil) 539 | #define __Pyx_RefNannyFinishContext() 540 | #define __Pyx_INCREF(r) Py_INCREF(r) 541 | #define __Pyx_DECREF(r) Py_DECREF(r) 542 | #define __Pyx_GOTREF(r) 543 | #define __Pyx_GIVEREF(r) 544 | #define __Pyx_XINCREF(r) Py_XINCREF(r) 545 | #define __Pyx_XDECREF(r) Py_XDECREF(r) 546 | #define __Pyx_XGOTREF(r) 547 | #define __Pyx_XGIVEREF(r) 548 | #endif /* CYTHON_REFNANNY */ 549 | #define __Pyx_XDECREF_SET(r, v) do { \ 550 | PyObject *tmp = (PyObject *) r; \ 551 | r = v; __Pyx_XDECREF(tmp); \ 552 | } while (0) 553 | #define __Pyx_DECREF_SET(r, v) do { \ 554 | PyObject *tmp = (PyObject *) r; \ 555 | r = v; __Pyx_DECREF(tmp); \ 556 | } while (0) 557 | #define __Pyx_CLEAR(r) do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0) 558 | #define __Pyx_XCLEAR(r) do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0) 559 | 560 | #if CYTHON_COMPILING_IN_CPYTHON 561 | static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) { 562 | PyTypeObject* tp = Py_TYPE(obj); 563 | if (likely(tp->tp_getattro)) 564 | return tp->tp_getattro(obj, attr_name); 565 | #if PY_MAJOR_VERSION < 3 566 | if (likely(tp->tp_getattr)) 567 | return tp->tp_getattr(obj, PyString_AS_STRING(attr_name)); 568 | #endif 569 | return PyObject_GetAttr(obj, attr_name); 570 | } 571 | #else 572 | #define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n) 573 | #endif 574 | 575 | static PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/ 576 | 577 | static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact, 578 | Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/ 579 | 580 | static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/ 581 | 582 | static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \ 583 | PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \ 584 | const char* function_name); /*proto*/ 585 | 586 | static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ 587 | static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ 588 | 589 | static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/ 590 | 591 | #ifndef __PYX_FORCE_INIT_THREADS 592 | #define __PYX_FORCE_INIT_THREADS 0 593 | #endif 594 | 595 | #ifndef __Pyx_CppExn2PyErr 596 | #include 597 | #include 598 | #include 599 | #include 600 | static void __Pyx_CppExn2PyErr() { 601 | try { 602 | if (PyErr_Occurred()) 603 | ; // let the latest Python exn pass through and ignore the current one 604 | else 605 | throw; 606 | } catch (const std::bad_alloc& exn) { 607 | PyErr_SetString(PyExc_MemoryError, exn.what()); 608 | } catch (const std::bad_cast& exn) { 609 | PyErr_SetString(PyExc_TypeError, exn.what()); 610 | } catch (const std::domain_error& exn) { 611 | PyErr_SetString(PyExc_ValueError, exn.what()); 612 | } catch (const std::invalid_argument& exn) { 613 | PyErr_SetString(PyExc_ValueError, exn.what()); 614 | } catch (const std::ios_base::failure& exn) { 615 | PyErr_SetString(PyExc_IOError, exn.what()); 616 | } catch (const std::out_of_range& exn) { 617 | PyErr_SetString(PyExc_IndexError, exn.what()); 618 | } catch (const std::overflow_error& exn) { 619 | PyErr_SetString(PyExc_OverflowError, exn.what()); 620 | } catch (const std::range_error& exn) { 621 | PyErr_SetString(PyExc_ArithmeticError, exn.what()); 622 | } catch (const std::underflow_error& exn) { 623 | PyErr_SetString(PyExc_ArithmeticError, exn.what()); 624 | } catch (const std::exception& exn) { 625 | PyErr_SetString(PyExc_RuntimeError, exn.what()); 626 | } 627 | catch (...) 628 | { 629 | PyErr_SetString(PyExc_RuntimeError, "Unknown exception"); 630 | } 631 | } 632 | #endif 633 | 634 | static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject *); 635 | 636 | static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject *); 637 | 638 | static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject *); 639 | 640 | static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject *); 641 | 642 | static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject *); 643 | 644 | static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject *); 645 | 646 | static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject *); 647 | 648 | static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject *); 649 | 650 | static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject *); 651 | 652 | static CYTHON_INLINE int __Pyx_PyInt_AsLongDouble(PyObject *); 653 | 654 | static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject *); 655 | 656 | static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject *); 657 | 658 | static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject *); 659 | 660 | static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject *); 661 | 662 | static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject *); 663 | 664 | static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject *); 665 | 666 | static int __Pyx_check_binary_version(void); 667 | 668 | #if !defined(__Pyx_PyIdentifier_FromString) 669 | #if PY_MAJOR_VERSION < 3 670 | #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s) 671 | #else 672 | #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s) 673 | #endif 674 | #endif 675 | 676 | static PyObject *__Pyx_ImportModule(const char *name); /*proto*/ 677 | 678 | static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, size_t size, int strict); /*proto*/ 679 | 680 | typedef struct { 681 | int code_line; 682 | PyCodeObject* code_object; 683 | } __Pyx_CodeObjectCacheEntry; 684 | struct __Pyx_CodeObjectCache { 685 | int count; 686 | int max_count; 687 | __Pyx_CodeObjectCacheEntry* entries; 688 | }; 689 | static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL}; 690 | static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line); 691 | static PyCodeObject *__pyx_find_code_object(int code_line); 692 | static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object); 693 | 694 | static void __Pyx_AddTraceback(const char *funcname, int c_line, 695 | int py_line, const char *filename); /*proto*/ 696 | 697 | static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/ 698 | 699 | 700 | /* Module declarations from 'cpython.version' */ 701 | 702 | /* Module declarations from 'cpython.ref' */ 703 | 704 | /* Module declarations from 'cpython.exc' */ 705 | 706 | /* Module declarations from 'cpython.module' */ 707 | 708 | /* Module declarations from 'cpython.mem' */ 709 | 710 | /* Module declarations from 'cpython.tuple' */ 711 | 712 | /* Module declarations from 'cpython.list' */ 713 | 714 | /* Module declarations from 'libc.string' */ 715 | 716 | /* Module declarations from 'libc.stdio' */ 717 | 718 | /* Module declarations from 'cpython.object' */ 719 | 720 | /* Module declarations from 'cpython.sequence' */ 721 | 722 | /* Module declarations from 'cpython.mapping' */ 723 | 724 | /* Module declarations from 'cpython.iterator' */ 725 | 726 | /* Module declarations from '__builtin__' */ 727 | 728 | /* Module declarations from 'cpython.type' */ 729 | static PyTypeObject *__pyx_ptype_7cpython_4type_type = 0; 730 | 731 | /* Module declarations from 'cpython.number' */ 732 | 733 | /* Module declarations from 'cpython.int' */ 734 | 735 | /* Module declarations from '__builtin__' */ 736 | 737 | /* Module declarations from 'cpython.bool' */ 738 | static PyTypeObject *__pyx_ptype_7cpython_4bool_bool = 0; 739 | 740 | /* Module declarations from 'cpython.long' */ 741 | 742 | /* Module declarations from 'cpython.float' */ 743 | 744 | /* Module declarations from '__builtin__' */ 745 | 746 | /* Module declarations from 'cpython.complex' */ 747 | static PyTypeObject *__pyx_ptype_7cpython_7complex_complex = 0; 748 | 749 | /* Module declarations from 'cpython.string' */ 750 | 751 | /* Module declarations from 'cpython.unicode' */ 752 | 753 | /* Module declarations from 'cpython.dict' */ 754 | 755 | /* Module declarations from 'cpython.instance' */ 756 | 757 | /* Module declarations from 'cpython.function' */ 758 | 759 | /* Module declarations from 'cpython.method' */ 760 | 761 | /* Module declarations from 'cpython.weakref' */ 762 | 763 | /* Module declarations from 'cpython.getargs' */ 764 | 765 | /* Module declarations from 'cpython.pythread' */ 766 | 767 | /* Module declarations from 'cpython.pystate' */ 768 | 769 | /* Module declarations from 'cpython.cobject' */ 770 | 771 | /* Module declarations from 'cpython.oldbuffer' */ 772 | 773 | /* Module declarations from 'cpython.set' */ 774 | 775 | /* Module declarations from 'cpython.buffer' */ 776 | 777 | /* Module declarations from 'cpython.bytes' */ 778 | 779 | /* Module declarations from 'cpython.pycapsule' */ 780 | 781 | /* Module declarations from 'cpython' */ 782 | 783 | /* Module declarations from 'leven._levenshtein' */ 784 | #define __Pyx_MODULE_NAME "leven._levenshtein" 785 | int __pyx_module_is_main_leven___levenshtein = 0; 786 | 787 | /* Implementation of 'leven._levenshtein' */ 788 | static PyObject *__pyx_builtin_TypeError; 789 | static PyObject *__pyx_pf_5leven_12_levenshtein_levenshtein(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_normalize); /* proto */ 790 | static char __pyx_k_2[] = "Type mismatch: expected (bytes, bytes) or ({0}, {0}), got ({1}, {2})"; 791 | static char __pyx_k_5[] = "/home/larsb/src/leven/leven/_levenshtein.pyx"; 792 | static char __pyx_k_6[] = "leven._levenshtein"; 793 | static char __pyx_k_7[] = "levenshtein (line 28)"; 794 | static char __pyx_k_8[] = "Returns the Levenshtein (edit) distance between a and b.\n\n When a and b are both byte strings (type bytes), the distance is computed\n bytewise. When a and b are both Unicode strings (type unicode in Python 2,\n str in Python 3), the distance is computed codepoint-wise.\n\n Other combinations of types will cause a TypeError to be raised.\n\n When passing Unicode strings, make sure they use the same normalized form\n (use the function unicodedata.normalize in the standard library).\n\n The normalize parameter can be used to normalize the distance by the length\n of the longest input.\n\n Example\n -------\n >>> levenshtein(\"Python\", \"Schmython\")\n 4\n "; 795 | static char __pyx_k__a[] = "a"; 796 | static char __pyx_k__b[] = "b"; 797 | static char __pyx_k__d[] = "d"; 798 | static char __pyx_k__m[] = "m"; 799 | static char __pyx_k__n[] = "n"; 800 | static char __pyx_k__format[] = "format"; 801 | static char __pyx_k____main__[] = "__main__"; 802 | static char __pyx_k____test__[] = "__test__"; 803 | static char __pyx_k__TypeError[] = "TypeError"; 804 | static char __pyx_k__normalize[] = "normalize"; 805 | static char __pyx_k__levenshtein[] = "levenshtein"; 806 | static PyObject *__pyx_kp_s_2; 807 | static PyObject *__pyx_kp_s_5; 808 | static PyObject *__pyx_n_s_6; 809 | static PyObject *__pyx_kp_u_7; 810 | static PyObject *__pyx_kp_u_8; 811 | static PyObject *__pyx_n_s__TypeError; 812 | static PyObject *__pyx_n_s____main__; 813 | static PyObject *__pyx_n_s____test__; 814 | static PyObject *__pyx_n_s__a; 815 | static PyObject *__pyx_n_s__b; 816 | static PyObject *__pyx_n_s__d; 817 | static PyObject *__pyx_n_s__format; 818 | static PyObject *__pyx_n_s__levenshtein; 819 | static PyObject *__pyx_n_s__m; 820 | static PyObject *__pyx_n_s__n; 821 | static PyObject *__pyx_n_s__normalize; 822 | static PyObject *__pyx_k_1; 823 | static PyObject *__pyx_k_tuple_3; 824 | static PyObject *__pyx_k_codeobj_4; 825 | 826 | /* Python wrapper */ 827 | static PyObject *__pyx_pw_5leven_12_levenshtein_1levenshtein(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ 828 | static char __pyx_doc_5leven_12_levenshtein_levenshtein[] = "Returns the Levenshtein (edit) distance between a and b.\n\n When a and b are both byte strings (type bytes), the distance is computed\n bytewise. When a and b are both Unicode strings (type unicode in Python 2,\n str in Python 3), the distance is computed codepoint-wise.\n\n Other combinations of types will cause a TypeError to be raised.\n\n When passing Unicode strings, make sure they use the same normalized form\n (use the function unicodedata.normalize in the standard library).\n\n The normalize parameter can be used to normalize the distance by the length\n of the longest input.\n\n Example\n -------\n >>> levenshtein(\"Python\", \"Schmython\")\n 4\n "; 829 | static PyMethodDef __pyx_mdef_5leven_12_levenshtein_1levenshtein = {__Pyx_NAMESTR("levenshtein"), (PyCFunction)__pyx_pw_5leven_12_levenshtein_1levenshtein, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(__pyx_doc_5leven_12_levenshtein_levenshtein)}; 830 | static PyObject *__pyx_pw_5leven_12_levenshtein_1levenshtein(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { 831 | PyObject *__pyx_v_a = 0; 832 | PyObject *__pyx_v_b = 0; 833 | PyObject *__pyx_v_normalize = 0; 834 | int __pyx_lineno = 0; 835 | const char *__pyx_filename = NULL; 836 | int __pyx_clineno = 0; 837 | PyObject *__pyx_r = 0; 838 | __Pyx_RefNannyDeclarations 839 | __Pyx_RefNannySetupContext("levenshtein (wrapper)", 0); 840 | { 841 | static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__a,&__pyx_n_s__b,&__pyx_n_s__normalize,0}; 842 | PyObject* values[3] = {0,0,0}; 843 | values[2] = __pyx_k_1; 844 | if (unlikely(__pyx_kwds)) { 845 | Py_ssize_t kw_args; 846 | const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); 847 | switch (pos_args) { 848 | case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2); 849 | case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); 850 | case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); 851 | case 0: break; 852 | default: goto __pyx_L5_argtuple_error; 853 | } 854 | kw_args = PyDict_Size(__pyx_kwds); 855 | switch (pos_args) { 856 | case 0: 857 | if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__a)) != 0)) kw_args--; 858 | else goto __pyx_L5_argtuple_error; 859 | case 1: 860 | if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__b)) != 0)) kw_args--; 861 | else { 862 | __Pyx_RaiseArgtupleInvalid("levenshtein", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L3_error;} 863 | } 864 | case 2: 865 | if (kw_args > 0) { 866 | PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__normalize); 867 | if (value) { values[2] = value; kw_args--; } 868 | } 869 | } 870 | if (unlikely(kw_args > 0)) { 871 | if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "levenshtein") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L3_error;} 872 | } 873 | } else { 874 | switch (PyTuple_GET_SIZE(__pyx_args)) { 875 | case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2); 876 | case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); 877 | values[0] = PyTuple_GET_ITEM(__pyx_args, 0); 878 | break; 879 | default: goto __pyx_L5_argtuple_error; 880 | } 881 | } 882 | __pyx_v_a = values[0]; 883 | __pyx_v_b = values[1]; 884 | __pyx_v_normalize = values[2]; 885 | } 886 | goto __pyx_L4_argument_unpacking_done; 887 | __pyx_L5_argtuple_error:; 888 | __Pyx_RaiseArgtupleInvalid("levenshtein", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L3_error;} 889 | __pyx_L3_error:; 890 | __Pyx_AddTraceback("leven._levenshtein.levenshtein", __pyx_clineno, __pyx_lineno, __pyx_filename); 891 | __Pyx_RefNannyFinishContext(); 892 | return NULL; 893 | __pyx_L4_argument_unpacking_done:; 894 | __pyx_r = __pyx_pf_5leven_12_levenshtein_levenshtein(__pyx_self, __pyx_v_a, __pyx_v_b, __pyx_v_normalize); 895 | __Pyx_RefNannyFinishContext(); 896 | return __pyx_r; 897 | } 898 | 899 | /* "leven/_levenshtein.pyx":28 900 | * 901 | * 902 | * def levenshtein(a, b, normalize=False): # <<<<<<<<<<<<<< 903 | * """Returns the Levenshtein (edit) distance between a and b. 904 | * 905 | */ 906 | 907 | static PyObject *__pyx_pf_5leven_12_levenshtein_levenshtein(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_normalize) { 908 | unsigned int __pyx_v_d; 909 | size_t __pyx_v_m; 910 | size_t __pyx_v_n; 911 | PyObject *__pyx_r = NULL; 912 | __Pyx_RefNannyDeclarations 913 | Py_ssize_t __pyx_t_1; 914 | int __pyx_t_2; 915 | int __pyx_t_3; 916 | int __pyx_t_4; 917 | char const *__pyx_t_5; 918 | char const *__pyx_t_6; 919 | unsigned int __pyx_t_7; 920 | PyObject *__pyx_t_8 = NULL; 921 | PyObject *__pyx_t_9 = NULL; 922 | PyObject *__pyx_t_10 = NULL; 923 | size_t __pyx_t_11; 924 | unsigned long __pyx_t_12; 925 | size_t __pyx_t_13; 926 | size_t __pyx_t_14; 927 | double __pyx_t_15; 928 | int __pyx_lineno = 0; 929 | const char *__pyx_filename = NULL; 930 | int __pyx_clineno = 0; 931 | __Pyx_RefNannySetupContext("levenshtein", 0); 932 | 933 | /* "leven/_levenshtein.pyx":49 934 | * """ 935 | * cdef unsigned d 936 | * cdef size_t m = len(a), n = len(b) # <<<<<<<<<<<<<< 937 | * 938 | * if isinstance(a, bytes) and isinstance(b, bytes): 939 | */ 940 | __pyx_t_1 = PyObject_Length(__pyx_v_a); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 941 | __pyx_v_m = __pyx_t_1; 942 | __pyx_t_1 = PyObject_Length(__pyx_v_b); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 943 | __pyx_v_n = __pyx_t_1; 944 | 945 | /* "leven/_levenshtein.pyx":51 946 | * cdef size_t m = len(a), n = len(b) 947 | * 948 | * if isinstance(a, bytes) and isinstance(b, bytes): # <<<<<<<<<<<<<< 949 | * d = levenshtein_char(a, m, b, n) 950 | * elif isinstance(a, unicode) and isinstance(b, unicode): 951 | */ 952 | __pyx_t_2 = PyBytes_Check(__pyx_v_a); 953 | if ((__pyx_t_2 != 0)) { 954 | __pyx_t_3 = PyBytes_Check(__pyx_v_b); 955 | __pyx_t_4 = (__pyx_t_3 != 0); 956 | } else { 957 | __pyx_t_4 = (__pyx_t_2 != 0); 958 | } 959 | if (__pyx_t_4) { 960 | 961 | /* "leven/_levenshtein.pyx":52 962 | * 963 | * if isinstance(a, bytes) and isinstance(b, bytes): 964 | * d = levenshtein_char(a, m, b, n) # <<<<<<<<<<<<<< 965 | * elif isinstance(a, unicode) and isinstance(b, unicode): 966 | * d = levenshtein_Py_UNICODE(PyUnicode_AS_UNICODE(a), m, 967 | */ 968 | __pyx_t_5 = __Pyx_PyObject_AsString(__pyx_v_a); if (unlikely((!__pyx_t_5) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 969 | __pyx_t_6 = __Pyx_PyObject_AsString(__pyx_v_b); if (unlikely((!__pyx_t_6) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 970 | try { 971 | __pyx_t_7 = levenshtein_char(__pyx_t_5, __pyx_v_m, __pyx_t_6, __pyx_v_n); 972 | } catch(...) { 973 | __Pyx_CppExn2PyErr(); 974 | {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 975 | } 976 | __pyx_v_d = __pyx_t_7; 977 | goto __pyx_L3; 978 | } 979 | 980 | /* "leven/_levenshtein.pyx":53 981 | * if isinstance(a, bytes) and isinstance(b, bytes): 982 | * d = levenshtein_char(a, m, b, n) 983 | * elif isinstance(a, unicode) and isinstance(b, unicode): # <<<<<<<<<<<<<< 984 | * d = levenshtein_Py_UNICODE(PyUnicode_AS_UNICODE(a), m, 985 | * PyUnicode_AS_UNICODE(b), n) 986 | */ 987 | __pyx_t_4 = PyUnicode_Check(__pyx_v_a); 988 | if ((__pyx_t_4 != 0)) { 989 | __pyx_t_2 = PyUnicode_Check(__pyx_v_b); 990 | __pyx_t_3 = (__pyx_t_2 != 0); 991 | } else { 992 | __pyx_t_3 = (__pyx_t_4 != 0); 993 | } 994 | if (__pyx_t_3) { 995 | 996 | /* "leven/_levenshtein.pyx":55 997 | * elif isinstance(a, unicode) and isinstance(b, unicode): 998 | * d = levenshtein_Py_UNICODE(PyUnicode_AS_UNICODE(a), m, 999 | * PyUnicode_AS_UNICODE(b), n) # <<<<<<<<<<<<<< 1000 | * else: 1001 | * raise TypeError("Type mismatch: expected (bytes, bytes) or ({0}, {0})," 1002 | */ 1003 | try { 1004 | __pyx_t_7 = levenshtein_Py_UNICODE(PyUnicode_AS_UNICODE(__pyx_v_a), __pyx_v_m, PyUnicode_AS_UNICODE(__pyx_v_b), __pyx_v_n); 1005 | } catch(...) { 1006 | __Pyx_CppExn2PyErr(); 1007 | {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1008 | } 1009 | __pyx_v_d = __pyx_t_7; 1010 | goto __pyx_L3; 1011 | } 1012 | /*else*/ { 1013 | 1014 | /* "leven/_levenshtein.pyx":58 1015 | * else: 1016 | * raise TypeError("Type mismatch: expected (bytes, bytes) or ({0}, {0})," 1017 | * " got ({1}, {2})".format(unicode, type(a), type(b))) # <<<<<<<<<<<<<< 1018 | * 1019 | * if normalize: 1020 | */ 1021 | __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_kp_s_2), __pyx_n_s__format); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1022 | __Pyx_GOTREF(__pyx_t_8); 1023 | __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1024 | __Pyx_GOTREF(__pyx_t_9); 1025 | __Pyx_INCREF(((PyObject *)((PyObject*)(&PyUnicode_Type)))); 1026 | PyTuple_SET_ITEM(__pyx_t_9, 0, ((PyObject *)((PyObject*)(&PyUnicode_Type)))); 1027 | __Pyx_GIVEREF(((PyObject *)((PyObject*)(&PyUnicode_Type)))); 1028 | __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_a))); 1029 | PyTuple_SET_ITEM(__pyx_t_9, 1, ((PyObject *)Py_TYPE(__pyx_v_a))); 1030 | __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_v_a))); 1031 | __Pyx_INCREF(((PyObject *)Py_TYPE(__pyx_v_b))); 1032 | PyTuple_SET_ITEM(__pyx_t_9, 2, ((PyObject *)Py_TYPE(__pyx_v_b))); 1033 | __Pyx_GIVEREF(((PyObject *)Py_TYPE(__pyx_v_b))); 1034 | __pyx_t_10 = PyObject_Call(__pyx_t_8, ((PyObject *)__pyx_t_9), NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1035 | __Pyx_GOTREF(__pyx_t_10); 1036 | __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; 1037 | __Pyx_DECREF(((PyObject *)__pyx_t_9)); __pyx_t_9 = 0; 1038 | __pyx_t_9 = PyTuple_New(1); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1039 | __Pyx_GOTREF(__pyx_t_9); 1040 | PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_10); 1041 | __Pyx_GIVEREF(__pyx_t_10); 1042 | __pyx_t_10 = 0; 1043 | __pyx_t_10 = PyObject_Call(__pyx_builtin_TypeError, ((PyObject *)__pyx_t_9), NULL); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1044 | __Pyx_GOTREF(__pyx_t_10); 1045 | __Pyx_DECREF(((PyObject *)__pyx_t_9)); __pyx_t_9 = 0; 1046 | __Pyx_Raise(__pyx_t_10, 0, 0, 0); 1047 | __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; 1048 | {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1049 | } 1050 | __pyx_L3:; 1051 | 1052 | /* "leven/_levenshtein.pyx":60 1053 | * " got ({1}, {2})".format(unicode, type(a), type(b))) 1054 | * 1055 | * if normalize: # <<<<<<<<<<<<<< 1056 | * return (d) / (max(m, n, 1U)) 1057 | * else: 1058 | */ 1059 | __pyx_t_3 = __Pyx_PyObject_IsTrue(__pyx_v_normalize); if (unlikely(__pyx_t_3 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1060 | if (__pyx_t_3) { 1061 | 1062 | /* "leven/_levenshtein.pyx":61 1063 | * 1064 | * if normalize: 1065 | * return (d) / (max(m, n, 1U)) # <<<<<<<<<<<<<< 1066 | * else: 1067 | * return d 1068 | */ 1069 | __Pyx_XDECREF(__pyx_r); 1070 | __pyx_t_11 = __pyx_v_n; 1071 | __pyx_t_12 = 1U; 1072 | __pyx_t_13 = __pyx_v_m; 1073 | if (((__pyx_t_11 > __pyx_t_13) != 0)) { 1074 | __pyx_t_14 = __pyx_t_11; 1075 | } else { 1076 | __pyx_t_14 = __pyx_t_13; 1077 | } 1078 | __pyx_t_13 = __pyx_t_14; 1079 | if (((__pyx_t_12 > __pyx_t_13) != 0)) { 1080 | __pyx_t_14 = __pyx_t_12; 1081 | } else { 1082 | __pyx_t_14 = __pyx_t_13; 1083 | } 1084 | __pyx_t_15 = ((double)__pyx_t_14); 1085 | if (unlikely(__pyx_t_15 == 0)) { 1086 | #ifdef WITH_THREAD 1087 | PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); 1088 | #endif 1089 | PyErr_Format(PyExc_ZeroDivisionError, "float division"); 1090 | #ifdef WITH_THREAD 1091 | PyGILState_Release(__pyx_gilstate_save); 1092 | #endif 1093 | {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1094 | } 1095 | __pyx_t_10 = PyFloat_FromDouble((((double)__pyx_v_d) / __pyx_t_15)); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1096 | __Pyx_GOTREF(__pyx_t_10); 1097 | __pyx_r = __pyx_t_10; 1098 | __pyx_t_10 = 0; 1099 | goto __pyx_L0; 1100 | goto __pyx_L4; 1101 | } 1102 | /*else*/ { 1103 | 1104 | /* "leven/_levenshtein.pyx":63 1105 | * return (d) / (max(m, n, 1U)) 1106 | * else: 1107 | * return d # <<<<<<<<<<<<<< 1108 | */ 1109 | __Pyx_XDECREF(__pyx_r); 1110 | __pyx_t_10 = PyLong_FromUnsignedLong(__pyx_v_d); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 63; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1111 | __Pyx_GOTREF(__pyx_t_10); 1112 | __pyx_r = __pyx_t_10; 1113 | __pyx_t_10 = 0; 1114 | goto __pyx_L0; 1115 | } 1116 | __pyx_L4:; 1117 | 1118 | __pyx_r = Py_None; __Pyx_INCREF(Py_None); 1119 | goto __pyx_L0; 1120 | __pyx_L1_error:; 1121 | __Pyx_XDECREF(__pyx_t_8); 1122 | __Pyx_XDECREF(__pyx_t_9); 1123 | __Pyx_XDECREF(__pyx_t_10); 1124 | __Pyx_AddTraceback("leven._levenshtein.levenshtein", __pyx_clineno, __pyx_lineno, __pyx_filename); 1125 | __pyx_r = NULL; 1126 | __pyx_L0:; 1127 | __Pyx_XGIVEREF(__pyx_r); 1128 | __Pyx_RefNannyFinishContext(); 1129 | return __pyx_r; 1130 | } 1131 | 1132 | static PyMethodDef __pyx_methods[] = { 1133 | {0, 0, 0, 0} 1134 | }; 1135 | 1136 | #if PY_MAJOR_VERSION >= 3 1137 | static struct PyModuleDef __pyx_moduledef = { 1138 | #if PY_VERSION_HEX < 0x03020000 1139 | { PyObject_HEAD_INIT(NULL) NULL, 0, NULL }, 1140 | #else 1141 | PyModuleDef_HEAD_INIT, 1142 | #endif 1143 | __Pyx_NAMESTR("_levenshtein"), 1144 | 0, /* m_doc */ 1145 | -1, /* m_size */ 1146 | __pyx_methods /* m_methods */, 1147 | NULL, /* m_reload */ 1148 | NULL, /* m_traverse */ 1149 | NULL, /* m_clear */ 1150 | NULL /* m_free */ 1151 | }; 1152 | #endif 1153 | 1154 | static __Pyx_StringTabEntry __pyx_string_tab[] = { 1155 | {&__pyx_kp_s_2, __pyx_k_2, sizeof(__pyx_k_2), 0, 0, 1, 0}, 1156 | {&__pyx_kp_s_5, __pyx_k_5, sizeof(__pyx_k_5), 0, 0, 1, 0}, 1157 | {&__pyx_n_s_6, __pyx_k_6, sizeof(__pyx_k_6), 0, 0, 1, 1}, 1158 | {&__pyx_kp_u_7, __pyx_k_7, sizeof(__pyx_k_7), 0, 1, 0, 0}, 1159 | {&__pyx_kp_u_8, __pyx_k_8, sizeof(__pyx_k_8), 0, 1, 0, 0}, 1160 | {&__pyx_n_s__TypeError, __pyx_k__TypeError, sizeof(__pyx_k__TypeError), 0, 0, 1, 1}, 1161 | {&__pyx_n_s____main__, __pyx_k____main__, sizeof(__pyx_k____main__), 0, 0, 1, 1}, 1162 | {&__pyx_n_s____test__, __pyx_k____test__, sizeof(__pyx_k____test__), 0, 0, 1, 1}, 1163 | {&__pyx_n_s__a, __pyx_k__a, sizeof(__pyx_k__a), 0, 0, 1, 1}, 1164 | {&__pyx_n_s__b, __pyx_k__b, sizeof(__pyx_k__b), 0, 0, 1, 1}, 1165 | {&__pyx_n_s__d, __pyx_k__d, sizeof(__pyx_k__d), 0, 0, 1, 1}, 1166 | {&__pyx_n_s__format, __pyx_k__format, sizeof(__pyx_k__format), 0, 0, 1, 1}, 1167 | {&__pyx_n_s__levenshtein, __pyx_k__levenshtein, sizeof(__pyx_k__levenshtein), 0, 0, 1, 1}, 1168 | {&__pyx_n_s__m, __pyx_k__m, sizeof(__pyx_k__m), 0, 0, 1, 1}, 1169 | {&__pyx_n_s__n, __pyx_k__n, sizeof(__pyx_k__n), 0, 0, 1, 1}, 1170 | {&__pyx_n_s__normalize, __pyx_k__normalize, sizeof(__pyx_k__normalize), 0, 0, 1, 1}, 1171 | {0, 0, 0, 0, 0, 0, 0} 1172 | }; 1173 | static int __Pyx_InitCachedBuiltins(void) { 1174 | __pyx_builtin_TypeError = __Pyx_GetBuiltinName(__pyx_n_s__TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1175 | return 0; 1176 | __pyx_L1_error:; 1177 | return -1; 1178 | } 1179 | 1180 | static int __Pyx_InitCachedConstants(void) { 1181 | __Pyx_RefNannyDeclarations 1182 | __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0); 1183 | 1184 | /* "leven/_levenshtein.pyx":28 1185 | * 1186 | * 1187 | * def levenshtein(a, b, normalize=False): # <<<<<<<<<<<<<< 1188 | * """Returns the Levenshtein (edit) distance between a and b. 1189 | * 1190 | */ 1191 | __pyx_k_tuple_3 = PyTuple_Pack(6, ((PyObject *)__pyx_n_s__a), ((PyObject *)__pyx_n_s__b), ((PyObject *)__pyx_n_s__normalize), ((PyObject *)__pyx_n_s__d), ((PyObject *)__pyx_n_s__m), ((PyObject *)__pyx_n_s__n)); if (unlikely(!__pyx_k_tuple_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1192 | __Pyx_GOTREF(__pyx_k_tuple_3); 1193 | __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_3)); 1194 | __pyx_k_codeobj_4 = (PyObject*)__Pyx_PyCode_New(3, 0, 6, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_k_tuple_3, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_5, __pyx_n_s__levenshtein, 28, __pyx_empty_bytes); if (unlikely(!__pyx_k_codeobj_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1195 | __Pyx_RefNannyFinishContext(); 1196 | return 0; 1197 | __pyx_L1_error:; 1198 | __Pyx_RefNannyFinishContext(); 1199 | return -1; 1200 | } 1201 | 1202 | static int __Pyx_InitGlobals(void) { 1203 | if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; 1204 | return 0; 1205 | __pyx_L1_error:; 1206 | return -1; 1207 | } 1208 | 1209 | #if PY_MAJOR_VERSION < 3 1210 | PyMODINIT_FUNC init_levenshtein(void); /*proto*/ 1211 | PyMODINIT_FUNC init_levenshtein(void) 1212 | #else 1213 | PyMODINIT_FUNC PyInit__levenshtein(void); /*proto*/ 1214 | PyMODINIT_FUNC PyInit__levenshtein(void) 1215 | #endif 1216 | { 1217 | PyObject *__pyx_t_1 = NULL; 1218 | int __pyx_lineno = 0; 1219 | const char *__pyx_filename = NULL; 1220 | int __pyx_clineno = 0; 1221 | __Pyx_RefNannyDeclarations 1222 | #if CYTHON_REFNANNY 1223 | __Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny"); 1224 | if (!__Pyx_RefNanny) { 1225 | PyErr_Clear(); 1226 | __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny"); 1227 | if (!__Pyx_RefNanny) 1228 | Py_FatalError("failed to import 'refnanny' module"); 1229 | } 1230 | #endif 1231 | __Pyx_RefNannySetupContext("PyMODINIT_FUNC PyInit__levenshtein(void)", 0); 1232 | if ( __Pyx_check_binary_version() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1233 | __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1234 | __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1235 | #ifdef __Pyx_CyFunction_USED 1236 | if (__Pyx_CyFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1237 | #endif 1238 | #ifdef __Pyx_FusedFunction_USED 1239 | if (__pyx_FusedFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1240 | #endif 1241 | #ifdef __Pyx_Generator_USED 1242 | if (__pyx_Generator_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1243 | #endif 1244 | /*--- Library function declarations ---*/ 1245 | /*--- Threads initialization code ---*/ 1246 | #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS 1247 | #ifdef WITH_THREAD /* Python build with threading support? */ 1248 | PyEval_InitThreads(); 1249 | #endif 1250 | #endif 1251 | /*--- Module creation code ---*/ 1252 | #if PY_MAJOR_VERSION < 3 1253 | __pyx_m = Py_InitModule4(__Pyx_NAMESTR("_levenshtein"), __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m); 1254 | #else 1255 | __pyx_m = PyModule_Create(&__pyx_moduledef); 1256 | #endif 1257 | if (unlikely(!__pyx_m)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1258 | __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1259 | Py_INCREF(__pyx_d); 1260 | #if PY_MAJOR_VERSION >= 3 1261 | { 1262 | PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1263 | if (!PyDict_GetItemString(modules, "leven._levenshtein")) { 1264 | if (unlikely(PyDict_SetItemString(modules, "leven._levenshtein", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1265 | } 1266 | } 1267 | #endif 1268 | __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME)); if (unlikely(!__pyx_b)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1269 | #if CYTHON_COMPILING_IN_PYPY 1270 | Py_INCREF(__pyx_b); 1271 | #endif 1272 | if (__Pyx_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; 1273 | /*--- Initialize various global constants etc. ---*/ 1274 | if (unlikely(__Pyx_InitGlobals() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1275 | #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT) 1276 | if (__Pyx_init_sys_getdefaultencoding_params() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1277 | #endif 1278 | if (__pyx_module_is_main_leven___levenshtein) { 1279 | if (__Pyx_SetAttrString(__pyx_m, "__name__", __pyx_n_s____main__) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; 1280 | } 1281 | /*--- Builtin init code ---*/ 1282 | if (unlikely(__Pyx_InitCachedBuiltins() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1283 | /*--- Constants init code ---*/ 1284 | if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1285 | /*--- Global init code ---*/ 1286 | /*--- Variable export code ---*/ 1287 | /*--- Function export code ---*/ 1288 | /*--- Type init code ---*/ 1289 | /*--- Type import code ---*/ 1290 | __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "type", 1291 | #if CYTHON_COMPILING_IN_PYPY 1292 | sizeof(PyTypeObject), 1293 | #else 1294 | sizeof(PyHeapTypeObject), 1295 | #endif 1296 | 0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1297 | __pyx_ptype_7cpython_4bool_bool = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "bool", sizeof(PyBoolObject), 0); if (unlikely(!__pyx_ptype_7cpython_4bool_bool)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1298 | __pyx_ptype_7cpython_7complex_complex = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, "complex", sizeof(PyComplexObject), 0); if (unlikely(!__pyx_ptype_7cpython_7complex_complex)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1299 | /*--- Variable import code ---*/ 1300 | /*--- Function import code ---*/ 1301 | /*--- Execution code ---*/ 1302 | 1303 | /* "leven/_levenshtein.pyx":28 1304 | * 1305 | * 1306 | * def levenshtein(a, b, normalize=False): # <<<<<<<<<<<<<< 1307 | * """Returns the Levenshtein (edit) distance between a and b. 1308 | * 1309 | */ 1310 | __pyx_t_1 = __Pyx_PyBool_FromLong(0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1311 | __Pyx_GOTREF(__pyx_t_1); 1312 | __pyx_k_1 = __pyx_t_1; 1313 | __Pyx_GIVEREF(__pyx_t_1); 1314 | __pyx_t_1 = 0; 1315 | __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_5leven_12_levenshtein_1levenshtein, NULL, __pyx_n_s_6); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1316 | __Pyx_GOTREF(__pyx_t_1); 1317 | if (PyDict_SetItem(__pyx_d, __pyx_n_s__levenshtein, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1318 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1319 | 1320 | /* "leven/_levenshtein.pyx":1 1321 | * # Copyright 2013 Netherlands eScience Center # <<<<<<<<<<<<<< 1322 | * # Written by Lars Buitinck 1323 | * # 1324 | */ 1325 | __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1326 | __Pyx_GOTREF(((PyObject *)__pyx_t_1)); 1327 | if (PyDict_SetItem(__pyx_t_1, ((PyObject *)__pyx_kp_u_7), ((PyObject *)__pyx_kp_u_8)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1328 | if (PyDict_SetItem(__pyx_d, __pyx_n_s____test__, ((PyObject *)__pyx_t_1)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} 1329 | __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; 1330 | goto __pyx_L0; 1331 | __pyx_L1_error:; 1332 | __Pyx_XDECREF(__pyx_t_1); 1333 | if (__pyx_m) { 1334 | __Pyx_AddTraceback("init leven._levenshtein", __pyx_clineno, __pyx_lineno, __pyx_filename); 1335 | Py_DECREF(__pyx_m); __pyx_m = 0; 1336 | } else if (!PyErr_Occurred()) { 1337 | PyErr_SetString(PyExc_ImportError, "init leven._levenshtein"); 1338 | } 1339 | __pyx_L0:; 1340 | __Pyx_RefNannyFinishContext(); 1341 | #if PY_MAJOR_VERSION < 3 1342 | return; 1343 | #else 1344 | return __pyx_m; 1345 | #endif 1346 | } 1347 | 1348 | /* Runtime support code */ 1349 | #if CYTHON_REFNANNY 1350 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) { 1351 | PyObject *m = NULL, *p = NULL; 1352 | void *r = NULL; 1353 | m = PyImport_ImportModule((char *)modname); 1354 | if (!m) goto end; 1355 | p = PyObject_GetAttrString(m, (char *)"RefNannyAPI"); 1356 | if (!p) goto end; 1357 | r = PyLong_AsVoidPtr(p); 1358 | end: 1359 | Py_XDECREF(p); 1360 | Py_XDECREF(m); 1361 | return (__Pyx_RefNannyAPIStruct *)r; 1362 | } 1363 | #endif /* CYTHON_REFNANNY */ 1364 | 1365 | static PyObject *__Pyx_GetBuiltinName(PyObject *name) { 1366 | PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name); 1367 | if (unlikely(!result)) { 1368 | PyErr_Format(PyExc_NameError, 1369 | #if PY_MAJOR_VERSION >= 3 1370 | "name '%U' is not defined", name); 1371 | #else 1372 | "name '%s' is not defined", PyString_AS_STRING(name)); 1373 | #endif 1374 | } 1375 | return result; 1376 | } 1377 | 1378 | static void __Pyx_RaiseArgtupleInvalid( 1379 | const char* func_name, 1380 | int exact, 1381 | Py_ssize_t num_min, 1382 | Py_ssize_t num_max, 1383 | Py_ssize_t num_found) 1384 | { 1385 | Py_ssize_t num_expected; 1386 | const char *more_or_less; 1387 | if (num_found < num_min) { 1388 | num_expected = num_min; 1389 | more_or_less = "at least"; 1390 | } else { 1391 | num_expected = num_max; 1392 | more_or_less = "at most"; 1393 | } 1394 | if (exact) { 1395 | more_or_less = "exactly"; 1396 | } 1397 | PyErr_Format(PyExc_TypeError, 1398 | "%s() takes %s %" CYTHON_FORMAT_SSIZE_T "d positional argument%s (%" CYTHON_FORMAT_SSIZE_T "d given)", 1399 | func_name, more_or_less, num_expected, 1400 | (num_expected == 1) ? "" : "s", num_found); 1401 | } 1402 | 1403 | static void __Pyx_RaiseDoubleKeywordsError( 1404 | const char* func_name, 1405 | PyObject* kw_name) 1406 | { 1407 | PyErr_Format(PyExc_TypeError, 1408 | #if PY_MAJOR_VERSION >= 3 1409 | "%s() got multiple values for keyword argument '%U'", func_name, kw_name); 1410 | #else 1411 | "%s() got multiple values for keyword argument '%s'", func_name, 1412 | PyString_AsString(kw_name)); 1413 | #endif 1414 | } 1415 | 1416 | static int __Pyx_ParseOptionalKeywords( 1417 | PyObject *kwds, 1418 | PyObject **argnames[], 1419 | PyObject *kwds2, 1420 | PyObject *values[], 1421 | Py_ssize_t num_pos_args, 1422 | const char* function_name) 1423 | { 1424 | PyObject *key = 0, *value = 0; 1425 | Py_ssize_t pos = 0; 1426 | PyObject*** name; 1427 | PyObject*** first_kw_arg = argnames + num_pos_args; 1428 | while (PyDict_Next(kwds, &pos, &key, &value)) { 1429 | name = first_kw_arg; 1430 | while (*name && (**name != key)) name++; 1431 | if (*name) { 1432 | values[name-argnames] = value; 1433 | continue; 1434 | } 1435 | name = first_kw_arg; 1436 | #if PY_MAJOR_VERSION < 3 1437 | if (likely(PyString_CheckExact(key)) || likely(PyString_Check(key))) { 1438 | while (*name) { 1439 | if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key)) 1440 | && _PyString_Eq(**name, key)) { 1441 | values[name-argnames] = value; 1442 | break; 1443 | } 1444 | name++; 1445 | } 1446 | if (*name) continue; 1447 | else { 1448 | PyObject*** argname = argnames; 1449 | while (argname != first_kw_arg) { 1450 | if ((**argname == key) || ( 1451 | (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key)) 1452 | && _PyString_Eq(**argname, key))) { 1453 | goto arg_passed_twice; 1454 | } 1455 | argname++; 1456 | } 1457 | } 1458 | } else 1459 | #endif 1460 | if (likely(PyUnicode_Check(key))) { 1461 | while (*name) { 1462 | int cmp = (**name == key) ? 0 : 1463 | #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3 1464 | (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 : 1465 | #endif 1466 | PyUnicode_Compare(**name, key); 1467 | if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad; 1468 | if (cmp == 0) { 1469 | values[name-argnames] = value; 1470 | break; 1471 | } 1472 | name++; 1473 | } 1474 | if (*name) continue; 1475 | else { 1476 | PyObject*** argname = argnames; 1477 | while (argname != first_kw_arg) { 1478 | int cmp = (**argname == key) ? 0 : 1479 | #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3 1480 | (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 : 1481 | #endif 1482 | PyUnicode_Compare(**argname, key); 1483 | if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad; 1484 | if (cmp == 0) goto arg_passed_twice; 1485 | argname++; 1486 | } 1487 | } 1488 | } else 1489 | goto invalid_keyword_type; 1490 | if (kwds2) { 1491 | if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad; 1492 | } else { 1493 | goto invalid_keyword; 1494 | } 1495 | } 1496 | return 0; 1497 | arg_passed_twice: 1498 | __Pyx_RaiseDoubleKeywordsError(function_name, key); 1499 | goto bad; 1500 | invalid_keyword_type: 1501 | PyErr_Format(PyExc_TypeError, 1502 | "%s() keywords must be strings", function_name); 1503 | goto bad; 1504 | invalid_keyword: 1505 | PyErr_Format(PyExc_TypeError, 1506 | #if PY_MAJOR_VERSION < 3 1507 | "%s() got an unexpected keyword argument '%s'", 1508 | function_name, PyString_AsString(key)); 1509 | #else 1510 | "%s() got an unexpected keyword argument '%U'", 1511 | function_name, key); 1512 | #endif 1513 | bad: 1514 | return -1; 1515 | } 1516 | 1517 | static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) { 1518 | #if CYTHON_COMPILING_IN_CPYTHON 1519 | PyObject *tmp_type, *tmp_value, *tmp_tb; 1520 | PyThreadState *tstate = PyThreadState_GET(); 1521 | tmp_type = tstate->curexc_type; 1522 | tmp_value = tstate->curexc_value; 1523 | tmp_tb = tstate->curexc_traceback; 1524 | tstate->curexc_type = type; 1525 | tstate->curexc_value = value; 1526 | tstate->curexc_traceback = tb; 1527 | Py_XDECREF(tmp_type); 1528 | Py_XDECREF(tmp_value); 1529 | Py_XDECREF(tmp_tb); 1530 | #else 1531 | PyErr_Restore(type, value, tb); 1532 | #endif 1533 | } 1534 | static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) { 1535 | #if CYTHON_COMPILING_IN_CPYTHON 1536 | PyThreadState *tstate = PyThreadState_GET(); 1537 | *type = tstate->curexc_type; 1538 | *value = tstate->curexc_value; 1539 | *tb = tstate->curexc_traceback; 1540 | tstate->curexc_type = 0; 1541 | tstate->curexc_value = 0; 1542 | tstate->curexc_traceback = 0; 1543 | #else 1544 | PyErr_Fetch(type, value, tb); 1545 | #endif 1546 | } 1547 | 1548 | #if PY_MAJOR_VERSION < 3 1549 | static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, 1550 | CYTHON_UNUSED PyObject *cause) { 1551 | Py_XINCREF(type); 1552 | if (!value || value == Py_None) 1553 | value = NULL; 1554 | else 1555 | Py_INCREF(value); 1556 | if (!tb || tb == Py_None) 1557 | tb = NULL; 1558 | else { 1559 | Py_INCREF(tb); 1560 | if (!PyTraceBack_Check(tb)) { 1561 | PyErr_SetString(PyExc_TypeError, 1562 | "raise: arg 3 must be a traceback or None"); 1563 | goto raise_error; 1564 | } 1565 | } 1566 | #if PY_VERSION_HEX < 0x02050000 1567 | if (PyClass_Check(type)) { 1568 | #else 1569 | if (PyType_Check(type)) { 1570 | #endif 1571 | #if CYTHON_COMPILING_IN_PYPY 1572 | if (!value) { 1573 | Py_INCREF(Py_None); 1574 | value = Py_None; 1575 | } 1576 | #endif 1577 | PyErr_NormalizeException(&type, &value, &tb); 1578 | } else { 1579 | if (value) { 1580 | PyErr_SetString(PyExc_TypeError, 1581 | "instance exception may not have a separate value"); 1582 | goto raise_error; 1583 | } 1584 | value = type; 1585 | #if PY_VERSION_HEX < 0x02050000 1586 | if (PyInstance_Check(type)) { 1587 | type = (PyObject*) ((PyInstanceObject*)type)->in_class; 1588 | Py_INCREF(type); 1589 | } else { 1590 | type = 0; 1591 | PyErr_SetString(PyExc_TypeError, 1592 | "raise: exception must be an old-style class or instance"); 1593 | goto raise_error; 1594 | } 1595 | #else 1596 | type = (PyObject*) Py_TYPE(type); 1597 | Py_INCREF(type); 1598 | if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) { 1599 | PyErr_SetString(PyExc_TypeError, 1600 | "raise: exception class must be a subclass of BaseException"); 1601 | goto raise_error; 1602 | } 1603 | #endif 1604 | } 1605 | __Pyx_ErrRestore(type, value, tb); 1606 | return; 1607 | raise_error: 1608 | Py_XDECREF(value); 1609 | Py_XDECREF(type); 1610 | Py_XDECREF(tb); 1611 | return; 1612 | } 1613 | #else /* Python 3+ */ 1614 | static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) { 1615 | PyObject* owned_instance = NULL; 1616 | if (tb == Py_None) { 1617 | tb = 0; 1618 | } else if (tb && !PyTraceBack_Check(tb)) { 1619 | PyErr_SetString(PyExc_TypeError, 1620 | "raise: arg 3 must be a traceback or None"); 1621 | goto bad; 1622 | } 1623 | if (value == Py_None) 1624 | value = 0; 1625 | if (PyExceptionInstance_Check(type)) { 1626 | if (value) { 1627 | PyErr_SetString(PyExc_TypeError, 1628 | "instance exception may not have a separate value"); 1629 | goto bad; 1630 | } 1631 | value = type; 1632 | type = (PyObject*) Py_TYPE(value); 1633 | } else if (PyExceptionClass_Check(type)) { 1634 | PyObject *args; 1635 | if (!value) 1636 | args = PyTuple_New(0); 1637 | else if (PyTuple_Check(value)) { 1638 | Py_INCREF(value); 1639 | args = value; 1640 | } else 1641 | args = PyTuple_Pack(1, value); 1642 | if (!args) 1643 | goto bad; 1644 | owned_instance = PyEval_CallObject(type, args); 1645 | Py_DECREF(args); 1646 | if (!owned_instance) 1647 | goto bad; 1648 | value = owned_instance; 1649 | if (!PyExceptionInstance_Check(value)) { 1650 | PyErr_Format(PyExc_TypeError, 1651 | "calling %R should have returned an instance of " 1652 | "BaseException, not %R", 1653 | type, Py_TYPE(value)); 1654 | goto bad; 1655 | } 1656 | } else { 1657 | PyErr_SetString(PyExc_TypeError, 1658 | "raise: exception class must be a subclass of BaseException"); 1659 | goto bad; 1660 | } 1661 | #if PY_VERSION_HEX >= 0x03030000 1662 | if (cause) { 1663 | #else 1664 | if (cause && cause != Py_None) { 1665 | #endif 1666 | PyObject *fixed_cause; 1667 | if (cause == Py_None) { 1668 | fixed_cause = NULL; 1669 | } else if (PyExceptionClass_Check(cause)) { 1670 | fixed_cause = PyObject_CallObject(cause, NULL); 1671 | if (fixed_cause == NULL) 1672 | goto bad; 1673 | } else if (PyExceptionInstance_Check(cause)) { 1674 | fixed_cause = cause; 1675 | Py_INCREF(fixed_cause); 1676 | } else { 1677 | PyErr_SetString(PyExc_TypeError, 1678 | "exception causes must derive from " 1679 | "BaseException"); 1680 | goto bad; 1681 | } 1682 | PyException_SetCause(value, fixed_cause); 1683 | } 1684 | PyErr_SetObject(type, value); 1685 | if (tb) { 1686 | PyThreadState *tstate = PyThreadState_GET(); 1687 | PyObject* tmp_tb = tstate->curexc_traceback; 1688 | if (tb != tmp_tb) { 1689 | Py_INCREF(tb); 1690 | tstate->curexc_traceback = tb; 1691 | Py_XDECREF(tmp_tb); 1692 | } 1693 | } 1694 | bad: 1695 | Py_XDECREF(owned_instance); 1696 | return; 1697 | } 1698 | #endif 1699 | 1700 | static CYTHON_INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject* x) { 1701 | const unsigned char neg_one = (unsigned char)-1, const_zero = 0; 1702 | const int is_unsigned = neg_one > const_zero; 1703 | if (sizeof(unsigned char) < sizeof(long)) { 1704 | long val = __Pyx_PyInt_AsLong(x); 1705 | if (unlikely(val != (long)(unsigned char)val)) { 1706 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1707 | PyErr_SetString(PyExc_OverflowError, 1708 | (is_unsigned && unlikely(val < 0)) ? 1709 | "can't convert negative value to unsigned char" : 1710 | "value too large to convert to unsigned char"); 1711 | } 1712 | return (unsigned char)-1; 1713 | } 1714 | return (unsigned char)val; 1715 | } 1716 | return (unsigned char)__Pyx_PyInt_AsUnsignedLong(x); 1717 | } 1718 | 1719 | static CYTHON_INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject* x) { 1720 | const unsigned short neg_one = (unsigned short)-1, const_zero = 0; 1721 | const int is_unsigned = neg_one > const_zero; 1722 | if (sizeof(unsigned short) < sizeof(long)) { 1723 | long val = __Pyx_PyInt_AsLong(x); 1724 | if (unlikely(val != (long)(unsigned short)val)) { 1725 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1726 | PyErr_SetString(PyExc_OverflowError, 1727 | (is_unsigned && unlikely(val < 0)) ? 1728 | "can't convert negative value to unsigned short" : 1729 | "value too large to convert to unsigned short"); 1730 | } 1731 | return (unsigned short)-1; 1732 | } 1733 | return (unsigned short)val; 1734 | } 1735 | return (unsigned short)__Pyx_PyInt_AsUnsignedLong(x); 1736 | } 1737 | 1738 | static CYTHON_INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject* x) { 1739 | const unsigned int neg_one = (unsigned int)-1, const_zero = 0; 1740 | const int is_unsigned = neg_one > const_zero; 1741 | if (sizeof(unsigned int) < sizeof(long)) { 1742 | long val = __Pyx_PyInt_AsLong(x); 1743 | if (unlikely(val != (long)(unsigned int)val)) { 1744 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1745 | PyErr_SetString(PyExc_OverflowError, 1746 | (is_unsigned && unlikely(val < 0)) ? 1747 | "can't convert negative value to unsigned int" : 1748 | "value too large to convert to unsigned int"); 1749 | } 1750 | return (unsigned int)-1; 1751 | } 1752 | return (unsigned int)val; 1753 | } 1754 | return (unsigned int)__Pyx_PyInt_AsUnsignedLong(x); 1755 | } 1756 | 1757 | static CYTHON_INLINE char __Pyx_PyInt_AsChar(PyObject* x) { 1758 | const char neg_one = (char)-1, const_zero = 0; 1759 | const int is_unsigned = neg_one > const_zero; 1760 | if (sizeof(char) < sizeof(long)) { 1761 | long val = __Pyx_PyInt_AsLong(x); 1762 | if (unlikely(val != (long)(char)val)) { 1763 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1764 | PyErr_SetString(PyExc_OverflowError, 1765 | (is_unsigned && unlikely(val < 0)) ? 1766 | "can't convert negative value to char" : 1767 | "value too large to convert to char"); 1768 | } 1769 | return (char)-1; 1770 | } 1771 | return (char)val; 1772 | } 1773 | return (char)__Pyx_PyInt_AsLong(x); 1774 | } 1775 | 1776 | static CYTHON_INLINE short __Pyx_PyInt_AsShort(PyObject* x) { 1777 | const short neg_one = (short)-1, const_zero = 0; 1778 | const int is_unsigned = neg_one > const_zero; 1779 | if (sizeof(short) < sizeof(long)) { 1780 | long val = __Pyx_PyInt_AsLong(x); 1781 | if (unlikely(val != (long)(short)val)) { 1782 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1783 | PyErr_SetString(PyExc_OverflowError, 1784 | (is_unsigned && unlikely(val < 0)) ? 1785 | "can't convert negative value to short" : 1786 | "value too large to convert to short"); 1787 | } 1788 | return (short)-1; 1789 | } 1790 | return (short)val; 1791 | } 1792 | return (short)__Pyx_PyInt_AsLong(x); 1793 | } 1794 | 1795 | static CYTHON_INLINE int __Pyx_PyInt_AsInt(PyObject* x) { 1796 | const int neg_one = (int)-1, const_zero = 0; 1797 | const int is_unsigned = neg_one > const_zero; 1798 | if (sizeof(int) < sizeof(long)) { 1799 | long val = __Pyx_PyInt_AsLong(x); 1800 | if (unlikely(val != (long)(int)val)) { 1801 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1802 | PyErr_SetString(PyExc_OverflowError, 1803 | (is_unsigned && unlikely(val < 0)) ? 1804 | "can't convert negative value to int" : 1805 | "value too large to convert to int"); 1806 | } 1807 | return (int)-1; 1808 | } 1809 | return (int)val; 1810 | } 1811 | return (int)__Pyx_PyInt_AsLong(x); 1812 | } 1813 | 1814 | static CYTHON_INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject* x) { 1815 | const signed char neg_one = (signed char)-1, const_zero = 0; 1816 | const int is_unsigned = neg_one > const_zero; 1817 | if (sizeof(signed char) < sizeof(long)) { 1818 | long val = __Pyx_PyInt_AsLong(x); 1819 | if (unlikely(val != (long)(signed char)val)) { 1820 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1821 | PyErr_SetString(PyExc_OverflowError, 1822 | (is_unsigned && unlikely(val < 0)) ? 1823 | "can't convert negative value to signed char" : 1824 | "value too large to convert to signed char"); 1825 | } 1826 | return (signed char)-1; 1827 | } 1828 | return (signed char)val; 1829 | } 1830 | return (signed char)__Pyx_PyInt_AsSignedLong(x); 1831 | } 1832 | 1833 | static CYTHON_INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject* x) { 1834 | const signed short neg_one = (signed short)-1, const_zero = 0; 1835 | const int is_unsigned = neg_one > const_zero; 1836 | if (sizeof(signed short) < sizeof(long)) { 1837 | long val = __Pyx_PyInt_AsLong(x); 1838 | if (unlikely(val != (long)(signed short)val)) { 1839 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1840 | PyErr_SetString(PyExc_OverflowError, 1841 | (is_unsigned && unlikely(val < 0)) ? 1842 | "can't convert negative value to signed short" : 1843 | "value too large to convert to signed short"); 1844 | } 1845 | return (signed short)-1; 1846 | } 1847 | return (signed short)val; 1848 | } 1849 | return (signed short)__Pyx_PyInt_AsSignedLong(x); 1850 | } 1851 | 1852 | static CYTHON_INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject* x) { 1853 | const signed int neg_one = (signed int)-1, const_zero = 0; 1854 | const int is_unsigned = neg_one > const_zero; 1855 | if (sizeof(signed int) < sizeof(long)) { 1856 | long val = __Pyx_PyInt_AsLong(x); 1857 | if (unlikely(val != (long)(signed int)val)) { 1858 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1859 | PyErr_SetString(PyExc_OverflowError, 1860 | (is_unsigned && unlikely(val < 0)) ? 1861 | "can't convert negative value to signed int" : 1862 | "value too large to convert to signed int"); 1863 | } 1864 | return (signed int)-1; 1865 | } 1866 | return (signed int)val; 1867 | } 1868 | return (signed int)__Pyx_PyInt_AsSignedLong(x); 1869 | } 1870 | 1871 | static CYTHON_INLINE int __Pyx_PyInt_AsLongDouble(PyObject* x) { 1872 | const int neg_one = (int)-1, const_zero = 0; 1873 | const int is_unsigned = neg_one > const_zero; 1874 | if (sizeof(int) < sizeof(long)) { 1875 | long val = __Pyx_PyInt_AsLong(x); 1876 | if (unlikely(val != (long)(int)val)) { 1877 | if (!unlikely(val == -1 && PyErr_Occurred())) { 1878 | PyErr_SetString(PyExc_OverflowError, 1879 | (is_unsigned && unlikely(val < 0)) ? 1880 | "can't convert negative value to int" : 1881 | "value too large to convert to int"); 1882 | } 1883 | return (int)-1; 1884 | } 1885 | return (int)val; 1886 | } 1887 | return (int)__Pyx_PyInt_AsLong(x); 1888 | } 1889 | 1890 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 1891 | #if CYTHON_USE_PYLONG_INTERNALS 1892 | #include "longintrepr.h" 1893 | #endif 1894 | #endif 1895 | static CYTHON_INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject* x) { 1896 | const unsigned long neg_one = (unsigned long)-1, const_zero = 0; 1897 | const int is_unsigned = neg_one > const_zero; 1898 | #if PY_MAJOR_VERSION < 3 1899 | if (likely(PyInt_Check(x))) { 1900 | long val = PyInt_AS_LONG(x); 1901 | if (is_unsigned && unlikely(val < 0)) { 1902 | PyErr_SetString(PyExc_OverflowError, 1903 | "can't convert negative value to unsigned long"); 1904 | return (unsigned long)-1; 1905 | } 1906 | return (unsigned long)val; 1907 | } else 1908 | #endif 1909 | if (likely(PyLong_Check(x))) { 1910 | if (is_unsigned) { 1911 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 1912 | #if CYTHON_USE_PYLONG_INTERNALS 1913 | if (sizeof(digit) <= sizeof(unsigned long)) { 1914 | switch (Py_SIZE(x)) { 1915 | case 0: return 0; 1916 | case 1: return (unsigned long) ((PyLongObject*)x)->ob_digit[0]; 1917 | } 1918 | } 1919 | #endif 1920 | #endif 1921 | if (unlikely(Py_SIZE(x) < 0)) { 1922 | PyErr_SetString(PyExc_OverflowError, 1923 | "can't convert negative value to unsigned long"); 1924 | return (unsigned long)-1; 1925 | } 1926 | return (unsigned long)PyLong_AsUnsignedLong(x); 1927 | } else { 1928 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 1929 | #if CYTHON_USE_PYLONG_INTERNALS 1930 | if (sizeof(digit) <= sizeof(unsigned long)) { 1931 | switch (Py_SIZE(x)) { 1932 | case 0: return 0; 1933 | case 1: return +(unsigned long) ((PyLongObject*)x)->ob_digit[0]; 1934 | case -1: return -(unsigned long) ((PyLongObject*)x)->ob_digit[0]; 1935 | } 1936 | } 1937 | #endif 1938 | #endif 1939 | return (unsigned long)PyLong_AsLong(x); 1940 | } 1941 | } else { 1942 | unsigned long val; 1943 | PyObject *tmp = __Pyx_PyNumber_Int(x); 1944 | if (!tmp) return (unsigned long)-1; 1945 | val = __Pyx_PyInt_AsUnsignedLong(tmp); 1946 | Py_DECREF(tmp); 1947 | return val; 1948 | } 1949 | } 1950 | 1951 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 1952 | #if CYTHON_USE_PYLONG_INTERNALS 1953 | #include "longintrepr.h" 1954 | #endif 1955 | #endif 1956 | static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject* x) { 1957 | const unsigned PY_LONG_LONG neg_one = (unsigned PY_LONG_LONG)-1, const_zero = 0; 1958 | const int is_unsigned = neg_one > const_zero; 1959 | #if PY_MAJOR_VERSION < 3 1960 | if (likely(PyInt_Check(x))) { 1961 | long val = PyInt_AS_LONG(x); 1962 | if (is_unsigned && unlikely(val < 0)) { 1963 | PyErr_SetString(PyExc_OverflowError, 1964 | "can't convert negative value to unsigned PY_LONG_LONG"); 1965 | return (unsigned PY_LONG_LONG)-1; 1966 | } 1967 | return (unsigned PY_LONG_LONG)val; 1968 | } else 1969 | #endif 1970 | if (likely(PyLong_Check(x))) { 1971 | if (is_unsigned) { 1972 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 1973 | #if CYTHON_USE_PYLONG_INTERNALS 1974 | if (sizeof(digit) <= sizeof(unsigned PY_LONG_LONG)) { 1975 | switch (Py_SIZE(x)) { 1976 | case 0: return 0; 1977 | case 1: return (unsigned PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 1978 | } 1979 | } 1980 | #endif 1981 | #endif 1982 | if (unlikely(Py_SIZE(x) < 0)) { 1983 | PyErr_SetString(PyExc_OverflowError, 1984 | "can't convert negative value to unsigned PY_LONG_LONG"); 1985 | return (unsigned PY_LONG_LONG)-1; 1986 | } 1987 | return (unsigned PY_LONG_LONG)PyLong_AsUnsignedLongLong(x); 1988 | } else { 1989 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 1990 | #if CYTHON_USE_PYLONG_INTERNALS 1991 | if (sizeof(digit) <= sizeof(unsigned PY_LONG_LONG)) { 1992 | switch (Py_SIZE(x)) { 1993 | case 0: return 0; 1994 | case 1: return +(unsigned PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 1995 | case -1: return -(unsigned PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 1996 | } 1997 | } 1998 | #endif 1999 | #endif 2000 | return (unsigned PY_LONG_LONG)PyLong_AsLongLong(x); 2001 | } 2002 | } else { 2003 | unsigned PY_LONG_LONG val; 2004 | PyObject *tmp = __Pyx_PyNumber_Int(x); 2005 | if (!tmp) return (unsigned PY_LONG_LONG)-1; 2006 | val = __Pyx_PyInt_AsUnsignedLongLong(tmp); 2007 | Py_DECREF(tmp); 2008 | return val; 2009 | } 2010 | } 2011 | 2012 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2013 | #if CYTHON_USE_PYLONG_INTERNALS 2014 | #include "longintrepr.h" 2015 | #endif 2016 | #endif 2017 | static CYTHON_INLINE long __Pyx_PyInt_AsLong(PyObject* x) { 2018 | const long neg_one = (long)-1, const_zero = 0; 2019 | const int is_unsigned = neg_one > const_zero; 2020 | #if PY_MAJOR_VERSION < 3 2021 | if (likely(PyInt_Check(x))) { 2022 | long val = PyInt_AS_LONG(x); 2023 | if (is_unsigned && unlikely(val < 0)) { 2024 | PyErr_SetString(PyExc_OverflowError, 2025 | "can't convert negative value to long"); 2026 | return (long)-1; 2027 | } 2028 | return (long)val; 2029 | } else 2030 | #endif 2031 | if (likely(PyLong_Check(x))) { 2032 | if (is_unsigned) { 2033 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2034 | #if CYTHON_USE_PYLONG_INTERNALS 2035 | if (sizeof(digit) <= sizeof(long)) { 2036 | switch (Py_SIZE(x)) { 2037 | case 0: return 0; 2038 | case 1: return (long) ((PyLongObject*)x)->ob_digit[0]; 2039 | } 2040 | } 2041 | #endif 2042 | #endif 2043 | if (unlikely(Py_SIZE(x) < 0)) { 2044 | PyErr_SetString(PyExc_OverflowError, 2045 | "can't convert negative value to long"); 2046 | return (long)-1; 2047 | } 2048 | return (long)PyLong_AsUnsignedLong(x); 2049 | } else { 2050 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2051 | #if CYTHON_USE_PYLONG_INTERNALS 2052 | if (sizeof(digit) <= sizeof(long)) { 2053 | switch (Py_SIZE(x)) { 2054 | case 0: return 0; 2055 | case 1: return +(long) ((PyLongObject*)x)->ob_digit[0]; 2056 | case -1: return -(long) ((PyLongObject*)x)->ob_digit[0]; 2057 | } 2058 | } 2059 | #endif 2060 | #endif 2061 | return (long)PyLong_AsLong(x); 2062 | } 2063 | } else { 2064 | long val; 2065 | PyObject *tmp = __Pyx_PyNumber_Int(x); 2066 | if (!tmp) return (long)-1; 2067 | val = __Pyx_PyInt_AsLong(tmp); 2068 | Py_DECREF(tmp); 2069 | return val; 2070 | } 2071 | } 2072 | 2073 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2074 | #if CYTHON_USE_PYLONG_INTERNALS 2075 | #include "longintrepr.h" 2076 | #endif 2077 | #endif 2078 | static CYTHON_INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject* x) { 2079 | const PY_LONG_LONG neg_one = (PY_LONG_LONG)-1, const_zero = 0; 2080 | const int is_unsigned = neg_one > const_zero; 2081 | #if PY_MAJOR_VERSION < 3 2082 | if (likely(PyInt_Check(x))) { 2083 | long val = PyInt_AS_LONG(x); 2084 | if (is_unsigned && unlikely(val < 0)) { 2085 | PyErr_SetString(PyExc_OverflowError, 2086 | "can't convert negative value to PY_LONG_LONG"); 2087 | return (PY_LONG_LONG)-1; 2088 | } 2089 | return (PY_LONG_LONG)val; 2090 | } else 2091 | #endif 2092 | if (likely(PyLong_Check(x))) { 2093 | if (is_unsigned) { 2094 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2095 | #if CYTHON_USE_PYLONG_INTERNALS 2096 | if (sizeof(digit) <= sizeof(PY_LONG_LONG)) { 2097 | switch (Py_SIZE(x)) { 2098 | case 0: return 0; 2099 | case 1: return (PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 2100 | } 2101 | } 2102 | #endif 2103 | #endif 2104 | if (unlikely(Py_SIZE(x) < 0)) { 2105 | PyErr_SetString(PyExc_OverflowError, 2106 | "can't convert negative value to PY_LONG_LONG"); 2107 | return (PY_LONG_LONG)-1; 2108 | } 2109 | return (PY_LONG_LONG)PyLong_AsUnsignedLongLong(x); 2110 | } else { 2111 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2112 | #if CYTHON_USE_PYLONG_INTERNALS 2113 | if (sizeof(digit) <= sizeof(PY_LONG_LONG)) { 2114 | switch (Py_SIZE(x)) { 2115 | case 0: return 0; 2116 | case 1: return +(PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 2117 | case -1: return -(PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 2118 | } 2119 | } 2120 | #endif 2121 | #endif 2122 | return (PY_LONG_LONG)PyLong_AsLongLong(x); 2123 | } 2124 | } else { 2125 | PY_LONG_LONG val; 2126 | PyObject *tmp = __Pyx_PyNumber_Int(x); 2127 | if (!tmp) return (PY_LONG_LONG)-1; 2128 | val = __Pyx_PyInt_AsLongLong(tmp); 2129 | Py_DECREF(tmp); 2130 | return val; 2131 | } 2132 | } 2133 | 2134 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2135 | #if CYTHON_USE_PYLONG_INTERNALS 2136 | #include "longintrepr.h" 2137 | #endif 2138 | #endif 2139 | static CYTHON_INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject* x) { 2140 | const signed long neg_one = (signed long)-1, const_zero = 0; 2141 | const int is_unsigned = neg_one > const_zero; 2142 | #if PY_MAJOR_VERSION < 3 2143 | if (likely(PyInt_Check(x))) { 2144 | long val = PyInt_AS_LONG(x); 2145 | if (is_unsigned && unlikely(val < 0)) { 2146 | PyErr_SetString(PyExc_OverflowError, 2147 | "can't convert negative value to signed long"); 2148 | return (signed long)-1; 2149 | } 2150 | return (signed long)val; 2151 | } else 2152 | #endif 2153 | if (likely(PyLong_Check(x))) { 2154 | if (is_unsigned) { 2155 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2156 | #if CYTHON_USE_PYLONG_INTERNALS 2157 | if (sizeof(digit) <= sizeof(signed long)) { 2158 | switch (Py_SIZE(x)) { 2159 | case 0: return 0; 2160 | case 1: return (signed long) ((PyLongObject*)x)->ob_digit[0]; 2161 | } 2162 | } 2163 | #endif 2164 | #endif 2165 | if (unlikely(Py_SIZE(x) < 0)) { 2166 | PyErr_SetString(PyExc_OverflowError, 2167 | "can't convert negative value to signed long"); 2168 | return (signed long)-1; 2169 | } 2170 | return (signed long)PyLong_AsUnsignedLong(x); 2171 | } else { 2172 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2173 | #if CYTHON_USE_PYLONG_INTERNALS 2174 | if (sizeof(digit) <= sizeof(signed long)) { 2175 | switch (Py_SIZE(x)) { 2176 | case 0: return 0; 2177 | case 1: return +(signed long) ((PyLongObject*)x)->ob_digit[0]; 2178 | case -1: return -(signed long) ((PyLongObject*)x)->ob_digit[0]; 2179 | } 2180 | } 2181 | #endif 2182 | #endif 2183 | return (signed long)PyLong_AsLong(x); 2184 | } 2185 | } else { 2186 | signed long val; 2187 | PyObject *tmp = __Pyx_PyNumber_Int(x); 2188 | if (!tmp) return (signed long)-1; 2189 | val = __Pyx_PyInt_AsSignedLong(tmp); 2190 | Py_DECREF(tmp); 2191 | return val; 2192 | } 2193 | } 2194 | 2195 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2196 | #if CYTHON_USE_PYLONG_INTERNALS 2197 | #include "longintrepr.h" 2198 | #endif 2199 | #endif 2200 | static CYTHON_INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject* x) { 2201 | const signed PY_LONG_LONG neg_one = (signed PY_LONG_LONG)-1, const_zero = 0; 2202 | const int is_unsigned = neg_one > const_zero; 2203 | #if PY_MAJOR_VERSION < 3 2204 | if (likely(PyInt_Check(x))) { 2205 | long val = PyInt_AS_LONG(x); 2206 | if (is_unsigned && unlikely(val < 0)) { 2207 | PyErr_SetString(PyExc_OverflowError, 2208 | "can't convert negative value to signed PY_LONG_LONG"); 2209 | return (signed PY_LONG_LONG)-1; 2210 | } 2211 | return (signed PY_LONG_LONG)val; 2212 | } else 2213 | #endif 2214 | if (likely(PyLong_Check(x))) { 2215 | if (is_unsigned) { 2216 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2217 | #if CYTHON_USE_PYLONG_INTERNALS 2218 | if (sizeof(digit) <= sizeof(signed PY_LONG_LONG)) { 2219 | switch (Py_SIZE(x)) { 2220 | case 0: return 0; 2221 | case 1: return (signed PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 2222 | } 2223 | } 2224 | #endif 2225 | #endif 2226 | if (unlikely(Py_SIZE(x) < 0)) { 2227 | PyErr_SetString(PyExc_OverflowError, 2228 | "can't convert negative value to signed PY_LONG_LONG"); 2229 | return (signed PY_LONG_LONG)-1; 2230 | } 2231 | return (signed PY_LONG_LONG)PyLong_AsUnsignedLongLong(x); 2232 | } else { 2233 | #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 2234 | #if CYTHON_USE_PYLONG_INTERNALS 2235 | if (sizeof(digit) <= sizeof(signed PY_LONG_LONG)) { 2236 | switch (Py_SIZE(x)) { 2237 | case 0: return 0; 2238 | case 1: return +(signed PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 2239 | case -1: return -(signed PY_LONG_LONG) ((PyLongObject*)x)->ob_digit[0]; 2240 | } 2241 | } 2242 | #endif 2243 | #endif 2244 | return (signed PY_LONG_LONG)PyLong_AsLongLong(x); 2245 | } 2246 | } else { 2247 | signed PY_LONG_LONG val; 2248 | PyObject *tmp = __Pyx_PyNumber_Int(x); 2249 | if (!tmp) return (signed PY_LONG_LONG)-1; 2250 | val = __Pyx_PyInt_AsSignedLongLong(tmp); 2251 | Py_DECREF(tmp); 2252 | return val; 2253 | } 2254 | } 2255 | 2256 | static int __Pyx_check_binary_version(void) { 2257 | char ctversion[4], rtversion[4]; 2258 | PyOS_snprintf(ctversion, 4, "%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION); 2259 | PyOS_snprintf(rtversion, 4, "%s", Py_GetVersion()); 2260 | if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) { 2261 | char message[200]; 2262 | PyOS_snprintf(message, sizeof(message), 2263 | "compiletime version %s of module '%.100s' " 2264 | "does not match runtime version %s", 2265 | ctversion, __Pyx_MODULE_NAME, rtversion); 2266 | #if PY_VERSION_HEX < 0x02050000 2267 | return PyErr_Warn(NULL, message); 2268 | #else 2269 | return PyErr_WarnEx(NULL, message, 1); 2270 | #endif 2271 | } 2272 | return 0; 2273 | } 2274 | 2275 | #ifndef __PYX_HAVE_RT_ImportModule 2276 | #define __PYX_HAVE_RT_ImportModule 2277 | static PyObject *__Pyx_ImportModule(const char *name) { 2278 | PyObject *py_name = 0; 2279 | PyObject *py_module = 0; 2280 | py_name = __Pyx_PyIdentifier_FromString(name); 2281 | if (!py_name) 2282 | goto bad; 2283 | py_module = PyImport_Import(py_name); 2284 | Py_DECREF(py_name); 2285 | return py_module; 2286 | bad: 2287 | Py_XDECREF(py_name); 2288 | return 0; 2289 | } 2290 | #endif 2291 | 2292 | #ifndef __PYX_HAVE_RT_ImportType 2293 | #define __PYX_HAVE_RT_ImportType 2294 | static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, 2295 | size_t size, int strict) 2296 | { 2297 | PyObject *py_module = 0; 2298 | PyObject *result = 0; 2299 | PyObject *py_name = 0; 2300 | char warning[200]; 2301 | Py_ssize_t basicsize; 2302 | #ifdef Py_LIMITED_API 2303 | PyObject *py_basicsize; 2304 | #endif 2305 | py_module = __Pyx_ImportModule(module_name); 2306 | if (!py_module) 2307 | goto bad; 2308 | py_name = __Pyx_PyIdentifier_FromString(class_name); 2309 | if (!py_name) 2310 | goto bad; 2311 | result = PyObject_GetAttr(py_module, py_name); 2312 | Py_DECREF(py_name); 2313 | py_name = 0; 2314 | Py_DECREF(py_module); 2315 | py_module = 0; 2316 | if (!result) 2317 | goto bad; 2318 | if (!PyType_Check(result)) { 2319 | PyErr_Format(PyExc_TypeError, 2320 | "%s.%s is not a type object", 2321 | module_name, class_name); 2322 | goto bad; 2323 | } 2324 | #ifndef Py_LIMITED_API 2325 | basicsize = ((PyTypeObject *)result)->tp_basicsize; 2326 | #else 2327 | py_basicsize = PyObject_GetAttrString(result, "__basicsize__"); 2328 | if (!py_basicsize) 2329 | goto bad; 2330 | basicsize = PyLong_AsSsize_t(py_basicsize); 2331 | Py_DECREF(py_basicsize); 2332 | py_basicsize = 0; 2333 | if (basicsize == (Py_ssize_t)-1 && PyErr_Occurred()) 2334 | goto bad; 2335 | #endif 2336 | if (!strict && (size_t)basicsize > size) { 2337 | PyOS_snprintf(warning, sizeof(warning), 2338 | "%s.%s size changed, may indicate binary incompatibility", 2339 | module_name, class_name); 2340 | #if PY_VERSION_HEX < 0x02050000 2341 | if (PyErr_Warn(NULL, warning) < 0) goto bad; 2342 | #else 2343 | if (PyErr_WarnEx(NULL, warning, 0) < 0) goto bad; 2344 | #endif 2345 | } 2346 | else if ((size_t)basicsize != size) { 2347 | PyErr_Format(PyExc_ValueError, 2348 | "%s.%s has the wrong size, try recompiling", 2349 | module_name, class_name); 2350 | goto bad; 2351 | } 2352 | return (PyTypeObject *)result; 2353 | bad: 2354 | Py_XDECREF(py_module); 2355 | Py_XDECREF(result); 2356 | return NULL; 2357 | } 2358 | #endif 2359 | 2360 | static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) { 2361 | int start = 0, mid = 0, end = count - 1; 2362 | if (end >= 0 && code_line > entries[end].code_line) { 2363 | return count; 2364 | } 2365 | while (start < end) { 2366 | mid = (start + end) / 2; 2367 | if (code_line < entries[mid].code_line) { 2368 | end = mid; 2369 | } else if (code_line > entries[mid].code_line) { 2370 | start = mid + 1; 2371 | } else { 2372 | return mid; 2373 | } 2374 | } 2375 | if (code_line <= entries[mid].code_line) { 2376 | return mid; 2377 | } else { 2378 | return mid + 1; 2379 | } 2380 | } 2381 | static PyCodeObject *__pyx_find_code_object(int code_line) { 2382 | PyCodeObject* code_object; 2383 | int pos; 2384 | if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) { 2385 | return NULL; 2386 | } 2387 | pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); 2388 | if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) { 2389 | return NULL; 2390 | } 2391 | code_object = __pyx_code_cache.entries[pos].code_object; 2392 | Py_INCREF(code_object); 2393 | return code_object; 2394 | } 2395 | static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) { 2396 | int pos, i; 2397 | __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; 2398 | if (unlikely(!code_line)) { 2399 | return; 2400 | } 2401 | if (unlikely(!entries)) { 2402 | entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry)); 2403 | if (likely(entries)) { 2404 | __pyx_code_cache.entries = entries; 2405 | __pyx_code_cache.max_count = 64; 2406 | __pyx_code_cache.count = 1; 2407 | entries[0].code_line = code_line; 2408 | entries[0].code_object = code_object; 2409 | Py_INCREF(code_object); 2410 | } 2411 | return; 2412 | } 2413 | pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); 2414 | if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) { 2415 | PyCodeObject* tmp = entries[pos].code_object; 2416 | entries[pos].code_object = code_object; 2417 | Py_DECREF(tmp); 2418 | return; 2419 | } 2420 | if (__pyx_code_cache.count == __pyx_code_cache.max_count) { 2421 | int new_max = __pyx_code_cache.max_count + 64; 2422 | entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc( 2423 | __pyx_code_cache.entries, new_max*sizeof(__Pyx_CodeObjectCacheEntry)); 2424 | if (unlikely(!entries)) { 2425 | return; 2426 | } 2427 | __pyx_code_cache.entries = entries; 2428 | __pyx_code_cache.max_count = new_max; 2429 | } 2430 | for (i=__pyx_code_cache.count; i>pos; i--) { 2431 | entries[i] = entries[i-1]; 2432 | } 2433 | entries[pos].code_line = code_line; 2434 | entries[pos].code_object = code_object; 2435 | __pyx_code_cache.count++; 2436 | Py_INCREF(code_object); 2437 | } 2438 | 2439 | #include "compile.h" 2440 | #include "frameobject.h" 2441 | #include "traceback.h" 2442 | static PyCodeObject* __Pyx_CreateCodeObjectForTraceback( 2443 | const char *funcname, int c_line, 2444 | int py_line, const char *filename) { 2445 | PyCodeObject *py_code = 0; 2446 | PyObject *py_srcfile = 0; 2447 | PyObject *py_funcname = 0; 2448 | #if PY_MAJOR_VERSION < 3 2449 | py_srcfile = PyString_FromString(filename); 2450 | #else 2451 | py_srcfile = PyUnicode_FromString(filename); 2452 | #endif 2453 | if (!py_srcfile) goto bad; 2454 | if (c_line) { 2455 | #if PY_MAJOR_VERSION < 3 2456 | py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); 2457 | #else 2458 | py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); 2459 | #endif 2460 | } 2461 | else { 2462 | #if PY_MAJOR_VERSION < 3 2463 | py_funcname = PyString_FromString(funcname); 2464 | #else 2465 | py_funcname = PyUnicode_FromString(funcname); 2466 | #endif 2467 | } 2468 | if (!py_funcname) goto bad; 2469 | py_code = __Pyx_PyCode_New( 2470 | 0, /*int argcount,*/ 2471 | 0, /*int kwonlyargcount,*/ 2472 | 0, /*int nlocals,*/ 2473 | 0, /*int stacksize,*/ 2474 | 0, /*int flags,*/ 2475 | __pyx_empty_bytes, /*PyObject *code,*/ 2476 | __pyx_empty_tuple, /*PyObject *consts,*/ 2477 | __pyx_empty_tuple, /*PyObject *names,*/ 2478 | __pyx_empty_tuple, /*PyObject *varnames,*/ 2479 | __pyx_empty_tuple, /*PyObject *freevars,*/ 2480 | __pyx_empty_tuple, /*PyObject *cellvars,*/ 2481 | py_srcfile, /*PyObject *filename,*/ 2482 | py_funcname, /*PyObject *name,*/ 2483 | py_line, /*int firstlineno,*/ 2484 | __pyx_empty_bytes /*PyObject *lnotab*/ 2485 | ); 2486 | Py_DECREF(py_srcfile); 2487 | Py_DECREF(py_funcname); 2488 | return py_code; 2489 | bad: 2490 | Py_XDECREF(py_srcfile); 2491 | Py_XDECREF(py_funcname); 2492 | return NULL; 2493 | } 2494 | static void __Pyx_AddTraceback(const char *funcname, int c_line, 2495 | int py_line, const char *filename) { 2496 | PyCodeObject *py_code = 0; 2497 | PyObject *py_globals = 0; 2498 | PyFrameObject *py_frame = 0; 2499 | py_code = __pyx_find_code_object(c_line ? c_line : py_line); 2500 | if (!py_code) { 2501 | py_code = __Pyx_CreateCodeObjectForTraceback( 2502 | funcname, c_line, py_line, filename); 2503 | if (!py_code) goto bad; 2504 | __pyx_insert_code_object(c_line ? c_line : py_line, py_code); 2505 | } 2506 | py_globals = PyModule_GetDict(__pyx_m); 2507 | if (!py_globals) goto bad; 2508 | py_frame = PyFrame_New( 2509 | PyThreadState_GET(), /*PyThreadState *tstate,*/ 2510 | py_code, /*PyCodeObject *code,*/ 2511 | py_globals, /*PyObject *globals,*/ 2512 | 0 /*PyObject *locals*/ 2513 | ); 2514 | if (!py_frame) goto bad; 2515 | py_frame->f_lineno = py_line; 2516 | PyTraceBack_Here(py_frame); 2517 | bad: 2518 | Py_XDECREF(py_code); 2519 | Py_XDECREF(py_frame); 2520 | } 2521 | 2522 | static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) { 2523 | while (t->p) { 2524 | #if PY_MAJOR_VERSION < 3 2525 | if (t->is_unicode) { 2526 | *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL); 2527 | } else if (t->intern) { 2528 | *t->p = PyString_InternFromString(t->s); 2529 | } else { 2530 | *t->p = PyString_FromStringAndSize(t->s, t->n - 1); 2531 | } 2532 | #else /* Python 3+ has unicode identifiers */ 2533 | if (t->is_unicode | t->is_str) { 2534 | if (t->intern) { 2535 | *t->p = PyUnicode_InternFromString(t->s); 2536 | } else if (t->encoding) { 2537 | *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL); 2538 | } else { 2539 | *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1); 2540 | } 2541 | } else { 2542 | *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1); 2543 | } 2544 | #endif 2545 | if (!*t->p) 2546 | return -1; 2547 | ++t; 2548 | } 2549 | return 0; 2550 | } 2551 | 2552 | static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char* c_str) { 2553 | return __Pyx_PyUnicode_FromStringAndSize(c_str, strlen(c_str)); 2554 | } 2555 | static CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) { 2556 | Py_ssize_t ignore; 2557 | return __Pyx_PyObject_AsStringAndSize(o, &ignore); 2558 | } 2559 | static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { 2560 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 2561 | if ( 2562 | #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2563 | __Pyx_sys_getdefaultencoding_not_ascii && 2564 | #endif 2565 | PyUnicode_Check(o)) { 2566 | #if PY_VERSION_HEX < 0x03030000 2567 | char* defenc_c; 2568 | PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); 2569 | if (!defenc) return NULL; 2570 | defenc_c = PyBytes_AS_STRING(defenc); 2571 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2572 | { 2573 | char* end = defenc_c + PyBytes_GET_SIZE(defenc); 2574 | char* c; 2575 | for (c = defenc_c; c < end; c++) { 2576 | if ((unsigned char) (*c) >= 128) { 2577 | PyUnicode_AsASCIIString(o); 2578 | return NULL; 2579 | } 2580 | } 2581 | } 2582 | #endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/ 2583 | *length = PyBytes_GET_SIZE(defenc); 2584 | return defenc_c; 2585 | #else /* PY_VERSION_HEX < 0x03030000 */ 2586 | if (PyUnicode_READY(o) == -1) return NULL; 2587 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2588 | if (PyUnicode_IS_ASCII(o)) { 2589 | *length = PyUnicode_GET_DATA_SIZE(o); 2590 | return PyUnicode_AsUTF8(o); 2591 | } else { 2592 | PyUnicode_AsASCIIString(o); 2593 | return NULL; 2594 | } 2595 | #else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ 2596 | return PyUnicode_AsUTF8AndSize(o, length); 2597 | #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ 2598 | #endif /* PY_VERSION_HEX < 0x03030000 */ 2599 | } else 2600 | #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */ 2601 | { 2602 | char* result; 2603 | int r = PyBytes_AsStringAndSize(o, &result, length); 2604 | if (r < 0) { 2605 | return NULL; 2606 | } else { 2607 | return result; 2608 | } 2609 | } 2610 | } 2611 | static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) { 2612 | int is_true = x == Py_True; 2613 | if (is_true | (x == Py_False) | (x == Py_None)) return is_true; 2614 | else return PyObject_IsTrue(x); 2615 | } 2616 | static CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) { 2617 | PyNumberMethods *m; 2618 | const char *name = NULL; 2619 | PyObject *res = NULL; 2620 | #if PY_MAJOR_VERSION < 3 2621 | if (PyInt_Check(x) || PyLong_Check(x)) 2622 | #else 2623 | if (PyLong_Check(x)) 2624 | #endif 2625 | return Py_INCREF(x), x; 2626 | m = Py_TYPE(x)->tp_as_number; 2627 | #if PY_MAJOR_VERSION < 3 2628 | if (m && m->nb_int) { 2629 | name = "int"; 2630 | res = PyNumber_Int(x); 2631 | } 2632 | else if (m && m->nb_long) { 2633 | name = "long"; 2634 | res = PyNumber_Long(x); 2635 | } 2636 | #else 2637 | if (m && m->nb_int) { 2638 | name = "int"; 2639 | res = PyNumber_Long(x); 2640 | } 2641 | #endif 2642 | if (res) { 2643 | #if PY_MAJOR_VERSION < 3 2644 | if (!PyInt_Check(res) && !PyLong_Check(res)) { 2645 | #else 2646 | if (!PyLong_Check(res)) { 2647 | #endif 2648 | PyErr_Format(PyExc_TypeError, 2649 | "__%s__ returned non-%s (type %.200s)", 2650 | name, name, Py_TYPE(res)->tp_name); 2651 | Py_DECREF(res); 2652 | return NULL; 2653 | } 2654 | } 2655 | else if (!PyErr_Occurred()) { 2656 | PyErr_SetString(PyExc_TypeError, 2657 | "an integer is required"); 2658 | } 2659 | return res; 2660 | } 2661 | static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) { 2662 | Py_ssize_t ival; 2663 | PyObject* x = PyNumber_Index(b); 2664 | if (!x) return -1; 2665 | ival = PyInt_AsSsize_t(x); 2666 | Py_DECREF(x); 2667 | return ival; 2668 | } 2669 | static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) { 2670 | #if PY_VERSION_HEX < 0x02050000 2671 | if (ival <= LONG_MAX) 2672 | return PyInt_FromLong((long)ival); 2673 | else { 2674 | unsigned char *bytes = (unsigned char *) &ival; 2675 | int one = 1; int little = (int)*(unsigned char*)&one; 2676 | return _PyLong_FromByteArray(bytes, sizeof(size_t), little, 0); 2677 | } 2678 | #else 2679 | return PyInt_FromSize_t(ival); 2680 | #endif 2681 | } 2682 | static CYTHON_INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) { 2683 | unsigned PY_LONG_LONG val = __Pyx_PyInt_AsUnsignedLongLong(x); 2684 | if (unlikely(val != (unsigned PY_LONG_LONG)(size_t)val)) { 2685 | if ((val != (unsigned PY_LONG_LONG)-1) || !PyErr_Occurred()) 2686 | PyErr_SetString(PyExc_OverflowError, 2687 | "value too large to convert to size_t"); 2688 | return (size_t)-1; 2689 | } 2690 | return (size_t)val; 2691 | } 2692 | 2693 | 2694 | #endif /* Py_PYTHON_H */ 2695 | --------------------------------------------------------------------------------