我正在使用Anaconda '3.4.1 |Anaconda 2.1.0 (64-bit)| (default, Sep 24 2014, 18:32:42) [MSC v.1600 64 bit (AMD64)]'
Windows 8计算机上的Python 3.4.1。
我安装了包含pip install python-Levenshtein
的软件包,所有内容都没有错误。 (版本0.12.0)
但后来我尝试使用它:
import Levenshtein as lvn
print(lvn.ratio('a', 'A'))
# This application has requested the Runtime to terminate it in an unusual way.
# Please contact the application's support team for more information.
# Assertion failed!
#
# Program: C:\Users\Me\Anaconda-P3.x-64b\python.exe
# File: Levenshtein/_levenshtein.c, Line 726
#
# Expression: PyUnicode_Check(arg1)
#
# Process finished with exit code 3
我尝试了print(lvn.ratio.__doc__)
,它打印文档就好了。
我缺少什么让这项工作?这是我所拥有的特定Python版本,编译器错误或Levenshtein库中的错误的问题
Levenshtein模块源代码可在Github获得;崩溃线726是:
722 else if (PyObject_TypeCheck(arg1, &PyUnicode_Type)
723 && PyObject_TypeCheck(arg2, &PyUnicode_Type)) {
724 Py_UNICODE *string1, *string2;
725
726 len1 = PyUnicode_GET_SIZE(arg1); // <-- assertion failure here
727 len2 = PyUnicode_GET_SIZE(arg2);
728 *lensum = len1 + len2;
729 string1 = PyUnicode_AS_UNICODE(arg1);
730 string2 = PyUnicode_AS_UNICODE(arg2);
<小时/> 我能够在另一台Windows 8机器上使用全新安装的Python 3.4重现此错误(来自Anaconda软件包 - 来自我的帖子的相同软件包) 测试一些其他方法:
distance()
- 完全相同的错误。
hamming()
- 同样的错误,但在第805行。
jaro()
- 同样的错误,但在第848行。
C:\Users\Thomaz\Anaconda32b\Scripts>pip.exe install python-Levenshtein
Collecting python-Levenshtein
Using cached python-Levenshtein-0.12.0.tar.gz
Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\users
\thomaz\anaconda32b\lib\site-packages\setuptools-5.8-py2.7.egg (from python-Leve
nshtein)
Installing collected packages: python-Levenshtein
Running setup.py install for python-Levenshtein
building 'Levenshtein._levenshtein' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Users\Thomaz\Anaconda32b\include -I
C:\Users\Thomaz\Anaconda32b\PC -c Levenshtein/_levenshtein.c -o build\temp.win32
-2.7\Release\levenshtein\_levenshtein.o
Levenshtein/_levenshtein.c: In function 'levenshtein_common':
Levenshtein/_levenshtein.c:711:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string1 = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:712:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string2 = PyString_AS_STRING(arg2);
^
Levenshtein/_levenshtein.c: In function 'hamming_py':
Levenshtein/_levenshtein.c:796:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string1 = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:797:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string2 = PyString_AS_STRING(arg2);
^
Levenshtein/_levenshtein.c: In function 'jaro_py':
Levenshtein/_levenshtein.c:840:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string1 = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:841:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string2 = PyString_AS_STRING(arg2);
^
Levenshtein/_levenshtein.c: In function 'jaro_winkler_py':
Levenshtein/_levenshtein.c:890:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string1 = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:891:13: warning: pointer targets in assignment di
ffer in signedness [-Wpointer-sign]
string2 = PyString_AS_STRING(arg2);
^
Levenshtein/_levenshtein.c: In function 'median_common':
Levenshtein/_levenshtein.c:992:7: warning: pointer targets in passing argume
nt 1 of 'PyString_FromStringAndSize' differ in signedness [-Wpointer-sign]
result = PyString_FromStringAndSize(medstr, len);
^
In file included from C:\Users\Thomaz\Anaconda32b\include/Python.h:94:0,
from Levenshtein/_levenshtein.c:99:
C:\Users\Thomaz\Anaconda32b\include/stringobject.h:62:24: note: expected 'co
nst char *' but argument is of type 'lev_byte *'
PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t)
;
^
Levenshtein/_levenshtein.c: In function 'median_improve_common':
C:\Users\Thomaz\Anaconda32b\include/stringobject.h:91:32: warning: pointer t
argets in initialization differ in signedness [-Wpointer-sign]
#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
^
Levenshtein/_levenshtein.c:1071:19: note: in expansion of macro 'PyString_AS
_STRING'
lev_byte *s = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:1077:7: warning: pointer targets in passing argum
ent 1 of 'PyString_FromStringAndSize' differ in signedness [-Wpointer-sign]
result = PyString_FromStringAndSize(medstr, len);
^
In file included from C:\Users\Thomaz\Anaconda32b\include/Python.h:94:0,
from Levenshtein/_levenshtein.c:99:
C:\Users\Thomaz\Anaconda32b\include/stringobject.h:62:24: note: expected 'co
nst char *' but argument is of type 'lev_byte *'
PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t)
;
^
Levenshtein/_levenshtein.c: In function 'extract_stringlist':
Levenshtein/_levenshtein.c:1201:16: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
strings[0] = PyString_AS_STRING(first);
^
Levenshtein/_levenshtein.c:1213:18: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
strings[i] = PyString_AS_STRING(item);
^
Levenshtein/_levenshtein.c: In function 'editops_py':
Levenshtein/_levenshtein.c:1650:13: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
string1 = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:1651:13: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
string2 = PyString_AS_STRING(arg2);
^
Levenshtein/_levenshtein.c: In function 'opcodes_py':
Levenshtein/_levenshtein.c:1768:13: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
string1 = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:1769:13: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
string2 = PyString_AS_STRING(arg2);
^
Levenshtein/_levenshtein.c: In function 'apply_edit_py':
Levenshtein/_levenshtein.c:1863:13: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
string1 = PyString_AS_STRING(arg1);
^
Levenshtein/_levenshtein.c:1864:13: warning: pointer targets in assignment d
iffer in signedness [-Wpointer-sign]
string2 = PyString_AS_STRING(arg2);
^
Levenshtein/_levenshtein.c:1878:7: warning: pointer targets in passing argum
ent 1 of 'PyString_FromStringAndSize' differ in signedness [-Wpointer-sign]
result = PyString_FromStringAndSize(s, len);
^
In file included from C:\Users\Thomaz\Anaconda32b\include/Python.h:94:0,
from Levenshtein/_levenshtein.c:99:
C:\Users\Thomaz\Anaconda32b\include/stringobject.h:62:24: note: expected 'co
nst char *' but argument is of type 'lev_byte *'
PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t)
;
^
Levenshtein/_levenshtein.c:1894:7: warning: pointer targets in passing argum
ent 1 of 'PyString_FromStringAndSize' differ in signedness [-Wpointer-sign]
result = PyString_FromStringAndSize(s, len);
^
In file included from C:\Users\Thomaz\Anaconda32b\include/Python.h:94:0,
from Levenshtein/_levenshtein.c:99:
C:\Users\Thomaz\Anaconda32b\include/stringobject.h:62:24: note: expected 'co
nst char *' but argument is of type 'lev_byte *'
PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t)
;
^
Levenshtein/_levenshtein.c: At top level:
Levenshtein/_levenshtein.c:6630:1: warning: 'lev_editops_total_cost' defined
but not used [-Wunused-function]
lev_editops_total_cost(size_t n,
^
Levenshtein/_levenshtein.c:6700:1: warning: 'lev_opcodes_total_cost' defined
but not used [-Wunused-function]
lev_opcodes_total_cost(size_t nb,
^
Levenshtein/_levenshtein.c:6655:1: warning: 'lev_editops_normalize' defined
but not used [-Wunused-function]
lev_editops_normalize(size_t n,
^
Levenshtein/_levenshtein.c:2371:1: warning: 'lev_edit_distance_sod' defined
but not used [-Wunused-function]
lev_edit_distance_sod(size_t len, const lev_byte *string,
^
Levenshtein/_levenshtein.c:2550:1: warning: 'lev_u_edit_distance_sod' define
d but not used [-Wunused-function]
lev_u_edit_distance_sod(size_t len, const lev_wchar *string,
^
C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-2.7\Release\levenshtein\_le
venshtein.o build\temp.win32-2.7\Release\levenshtein\_levenshtein.def -LC:\Users
\Thomaz\Anaconda32b\libs -LC:\Users\Thomaz\Anaconda32b\PCbuild -lpython27 -lmsvc
r90 -o build\lib.win32-2.7\Levenshtein\_levenshtein.pyd
Successfully installed python-Levenshtein-0.12.0
有趣......在Python 2.7中,即使有上述警告,一切也都有效。
答案 0 :(得分:1)
这个答案只是部分答案,我自己没有Windows,所以我无法调试bug。然而,仅仅查看源代码就可以得出结论:“这个bug是不可能的” - 可能是一个编译器错误,或者是UB的真正令人讨厌的情况,或者其他什么。
扩展程序的编译方式可能有问题。在the line 726之前,对第722行和第723行进行检查,明确检查参数是Unicode对象还是其子类(如果不是,则会跳过整个if
并且不会运行第726行);
722 else if (PyObject_TypeCheck(arg1, &PyUnicode_Type)
723 && PyObject_TypeCheck(arg2, &PyUnicode_Type)) {
724 Py_UNICODE *string1, *string2;
725
726 len1 = PyUnicode_GET_SIZE(arg1);
727 len2 = PyUnicode_GET_SIZE(arg2);
728 *lensum = len1 + len2;
729 string1 = PyUnicode_AS_UNICODE(arg1);
730 string2 = PyUnicode_AS_UNICODE(arg2);
PyUnicode_GET_SIZE
有两个断言语句,并且它们都被内联到行726
:
#define PyUnicode_GET_SIZE(op) \
(assert(PyUnicode_Check(op)), \
(((PyASCIIObject *)(op))->wstr) ? \
PyUnicode_WSTR_LENGTH(op) : \
((void)PyUnicode_AsUnicode((PyObject *)(op)), \
assert(((PyASCIIObject *)(op))->wstr), \
PyUnicode_WSTR_LENGTH(op)))
第一个通过检查PyUnicode_Check
的返回值来断言该对象确实是一个Unicode对象或其子类:
#define PyUnicode_Check(op) \
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS)
第二个断言PyUnicode_AsUnicode(op)
正确设置了wstr
成员。错误输出中的PyUnicode_Check(arg1)
指向第一个失败的事实。
现在有趣的是,它不应该失败; PyObject_TypeCheck
成功, 是一个Unicode对象(Python 3 str
),因此断言也应该是无操作。