This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: infinite recursion from calling builtins.open()
Type: behavior Stage:
Components: Interpreter Core, IO Versions: Python 3.1
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: pitrou Nosy List: georg.brandl, kaizhu, pitrou
Priority: normal Keywords:

Created on 2009-07-17 15:36 by kaizhu, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg90627 - (view) Author: kai zhu (kaizhu) Date: 2009-07-17 15:36
# copy this to test.py
# > touch foo.txt
# > python3.1 -c "import test; import collections"
# > ...
# >   File "test.py", line 5, in find_module
# >     def find_module(self, mname, path = None): open("foo.txt")
# >   File "test.py", line 5, in find_module
# >     def find_module(self, mname, path = None): open("foo.txt")
# >   File "test.py", line 5, in find_module
# >     def find_module(self, mname, path = None): open("foo.txt")
# >   File "test.py", line 5, in find_module
# >     def find_module(self, mname, path = None): open("foo.txt")
# > RuntimeError: maximum recursion depth exceeded while calling a
# > Python object
class importer(object):
  def find_module(self, mname, path = None): open("foo.txt")
import sys; sys.meta_path.append(importer)

***

# note recursion behavior stops if we don't call open()
class importer(object):
  def find_module(self, mname, path = None): pass
import sys; sys.meta_path.append(importer)
msg90649 - (view) Author: kai zhu (kaizhu) Date: 2009-07-17 23:12
recursion also goes away if we open as raw bytes: open("foo.txt", "rb"). modes "r+" & "w" also give infinite recursion, while "rb+" & "wb" do not.

note found another bug:
instance method find_module should raise exception anyway, since its 
class was uninstantiated in sys.meta_path (proper behavior in python2.6)
msg90669 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-07-18 10:17
First, the second bug isn't a bug since that restriction has been lifted
in Python 3.

The original issue occurs because open() for text modes imports the
"locale" module. This is kind of nasty, because calling open() is well
within what a find_module() implementation should be able to do. Antoine?
msg90672 - (view) Author: kai zhu (kaizhu) Date: 2009-07-18 10:48
current hack-around, then is to pre-import locale, which is verified to 
work:

# beg test.py
class importer(object):
  def find_module(self, mname, path = None): open("foo.txt")
import sys, locale; sys.meta_path.append(importer)
import collections # no recursion
# end test.py
msg90708 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-07-19 10:23
Yes, we conditionally import the "locale" module in order to guess the
encoding when it is not specified by the caller. We can't import it at
module initialization time because it would cause bootstrapping issues.

The Python equivalent of the logic expressed in C code in the _io module
can be found here:
http://code.python.org/hg/branches/py3k/file/51117227ac82/Lib/_pyio.py#l1410
msg111969 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-07-29 17:06
Cannot reproduce with current 3.2 trunk, closing.
History
Date User Action Args
2022-04-11 14:56:51adminsetgithub: 50753
2010-07-29 17:06:31georg.brandlsetstatus: open -> closed
resolution: out of date
messages: + msg111969
2009-07-19 10:23:25pitrousetmessages: + msg90708
2009-07-18 10:48:19kaizhusetmessages: + msg90672
2009-07-18 10:17:55georg.brandlsetassignee: pitrou

messages: + msg90669
nosy: + georg.brandl, pitrou
2009-07-17 23:12:33kaizhusetmessages: + msg90649
2009-07-17 15:36:08kaizhucreate