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

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


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.