【发布时间】:2011-11-02 03:31:29
【问题描述】:
我正在编写一些代码,但遇到一个奇怪的错误:当条件语句变为 false 时,我的 for 循环似乎没有退出。代码如下:
static void wstrcpy_from_Py_UNICODE(Py_UNICODE *inBuf, Py_ssize_t strLength, wchar_t **outBuf)
{
if (strLength == 0) *outBuf = NULL;
else
{
Py_ssize_t i;
wprintf(L"String Length: %d\n", strLength);
*outBuf = (wchar_t *)malloc (sizeof (wchar_t) * (strLength +1));
for (i=0; i < strLength; i++)
{
wprintf("i:%d, strLength:%d\n", i, strLength);
(*outBuf)[i] = (wchar_t)(inBuf[i]);
wprintf(L"i < strLength: %d\n\n", i < strLength);
}
/* Make sure new string is zero terminated */
(*outBuf)[i] = L'\0';
}
}
当使用示例输入运行此代码时,(Py_UNICODE * 缓冲区指向使用 u"example" 生成的内部 unicode python 对象)我得到以下输出:
String Length: 7
i:0, strLength: 7
i < strLength: 1
i:1, strLength: 7
i < strLength: 1
i:2, strLength: 7
i < strLength: 1
i:3, strLength: 7
i < strLength: 1
i:4, strLength: 7
i < strLength: 1
i:5, strLength: 7
i < strLength: 1
i:6, strLength: 7
i < strLength: 1
i:7, strLength: 7
i < strLength: 1
i:8, strLength: 7
i < strLength: 1
...
在运行代码的python解释器(我正在为python包装一个c模块)崩溃之前,循环不会退出。
已将 printf 用于调试。
我在 Mac OSX 10.6 上编译这个,这里是我用来编译的命令:
gcc -c source.c -I/usr/include/python2.6 -I/usr/lib/python2.6
ld -bundle -flat_namespace -undefined suppress -o out.so source.o -F./ -framework some_framework -macosx_version_min 10.6 -rpath ./
如您所见,我正在为其制作 python 包装器的框架中进行链接。这不是问题,因为我可以很好地调用使用链接框架的函数,只是当我调用使用上面显示的辅助函数的函数时,我会遇到问题。
我在这里是不是很愚蠢并且做了一些非常基本的错误,还是编译器有问题?任何帮助将不胜感激!
【问题讨论】:
-
真的很奇怪的错误!我想我会看一下汇编代码,看看它对循环做了什么。我认为编译器搞砸了一些东西。也许 Py_ssize_t 的类型定义有些混乱?