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:
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 ases
...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 callingPyMem_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.