python - 重复 os.path.isdir 调用中的巨大内存泄漏?

标签 python memory-leaks python-2.7 system-calls

我一直在编写与扫描目录有关的脚本,并在调用 os.path.isdir 时注意到严重的内存泄漏,因此我尝试了以下代码段:

def func():
    if not os.path.isdir('D:\Downloads'):
        return False
while True:
    func()

在几秒钟内,Python 进程达到了 100MB RAM。

我正试图弄清楚发生了什么。似乎只有当路径确实是有效的目录路径时,巨大的内存泄漏才有效(这意味着不执行“return False”)。 此外,看看相关调用中发生了什么也很有趣,比如 os.path.isfile。

想法?

编辑: 我想我在做某事。 虽然 isfile 和 isdir 是在 genericpath 模块中实现的,但在 Windows 系统上 - isdir 是从内置的 nt 中导入的。 所以我不得不下载 2.7.3 的源代码(我早就应该这样做了……)。

经过一番搜索,我发现\Modules\posixmodule.c中的posix__isdir函数,我认为是从nt导入的'isdir'函数。

这部分功能(和评论)引起了我的注意:

if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
        Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);

        attributes = GetFileAttributesW(wpath);
        if (attributes == INVALID_FILE_ATTRIBUTES)
            Py_RETURN_FALSE;
        goto check;
    }
    /* Drop the argument parsing error as narrow strings
       are also valid. */
    PyErr_Clear();

看来这一切都归结为 Unicode/ASCII 处理错误。

我刚刚用 unicode 中的路径参数尝试了上面的代码片段(即 u'D:\Downloads') - 没有任何内存泄漏。哈哈。

最佳答案

根本原因是在非Unicode路径中的path变量上调用PyMem_Free失败:

    if (!PyArg_ParseTuple(args, "et:_isdir",
                          Py_FileSystemDefaultEncoding, &path))
        return NULL;

    attributes = GetFileAttributesA(path);
    if (attributes == INVALID_FILE_ATTRIBUTES)
        Py_RETURN_FALSE;

check:
    if (attributes & FILE_ATTRIBUTE_DIRECTORY)
        Py_RETURN_TRUE;
    else
        Py_RETURN_FALSE;

根据 PyArg_ParseTuple 上的文档:

  • et: Same as es...
  • es: PyArg_ParseTuple() will allocate a buffer of the needed size, copy the encoded data into this buffer and adjust *buffer to reference the newly allocated storage. The caller is responsible for calling PyMem_Free() to free the allocated buffer after use.

这是 Python 标准库中的一个错误(在 Python 3 中通过直接使用字节对象修复);在 http://bugs.python.org 提交错误报告.

关于python - 重复 os.path.isdir 调用中的巨大内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12648737/

相关文章:

python - 使用 sqlalchemy 进行区分大小写的查询

java - SoftReferences 与 Weakreferences/OutOfMemoryError

Python:包目录不存在

Python:按键的前半部分/部分对字典重新排序

python - 参数为 : [ODBC Microsoft Access Driver] Too few parameters. 的 PypyODBC 预期为 4

python - 使用 webpy 和 urllib2 发送文件

python - 在Python中使用BeautifulSoup处理网页时,如何通过**class**或**div id**值过滤href链接?

c++ - 释放有效的 SDL 表面会导致段错误

c++ valgrind在hello world中显示内存泄漏

python-2.7 - Python 2.7.3 中的 Pandas moving_mean 错误