Huge memory leak in repeated os.path.isdir calls?

前端 未结 1 771
-上瘾入骨i
-上瘾入骨i 2021-01-03 21:18

I\'ve been scripting something that has to do with scanning directories and noticed a severe memory leak when calling os.path.isdir, so I\'ve tried the following snippet:

相关标签:
1条回答
  • 2021-01-03 21:57

    The root cause is a failure to call PyMem_Free on the path variable in the non-Unicode path:

        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;
    

    As per the documentation on 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.

    It's a bug in Python's standard library (fixed in Python 3 by using bytes objects directly); file a bug report at http://bugs.python.org.

    0 讨论(0)
提交回复
热议问题