HBNetwork / python-decouple

Strict separation of config from code.
MIT License
2.83k stars 196 forks source link

Infinte recursion on Windows+IPython if caller path on drive different than working path #172

Open jdranczewski opened 2 months ago

jdranczewski commented 2 months ago

When the imported file calling config is for example on the D: drive, the recurrent function that searches its parent folders will eventually reach D:/. Unfortunately, the check that's meant to stop the recursion considers the drive of the working directory (say, C:/) to be the top folder in some circumstances. The offending lines are these: https://github.com/HBNetwork/python-decouple/blob/0573e6f96637f08fb4cb85e0552f0622d36827d4/decouple.py#L220-L223

This results in a silent crash of whatever code is running, but only when the working directory is on a drive different than the drive where the file using config is, I think? It also only happens in IPython, which may be handling working directories differently. This made it quite amusing to diagnose (I was using a different library that imports decouple), and I presume it may make it difficult to reproduce easily.

I suggest replacing the check with:

os.path.normcase(parent) != os.path.normcase(path)

which will catch if the parent is the same as the current path more explicitly.

jdranczewski commented 2 months ago

Ah, the different behaviour under Python and IPython is because on my machine Python correctly raises the RecursionError, which gets caught by the try ... except block in _load, but in IPython the recursion Exception is not raised correctly and the process crashes. This seems to be an issue in IPython: https://github.com/ipython/ipython/issues/12197

So it's both a problem of the recursion happening and IPython handling it badly.