Issue 981530: UnboundLocalError in shutil.rmtree() (original) (raw)

It is possible for the call to

onerror(func, arg, exc)

to be triggered by an os.error raised by the os.listdir() call in _build_cmdtuple() rather than in the "for func, arg in cmdtuples" loop.

In that case, func isn't set yet, and an UnboundLocalError is raised rather than onerror() being called.

A quick fix for this is to set func=None before calling _build_cmdtuple, but the docs guarantee that it is set to a function when onerror is called (and specifically, os.remove and os.rmdir).

I propose to set func = os.listdir before calling _build_cmdtuple() and fixing the docs. I can backport this to 2.3.