[Python-3000] Radical idea: remove built-in open (require import io)

Alex Martelli aleaxit at gmail.com
Fri May 18 02:14:49 CEST 2007

On 5/17/07, Guido van Rossum <guido at python.org> wrote:
> Do people think it would be too radical if the built-in open()
> function was removed altogether, requiring all code that opens files
> to import the io module first? This would make it easier to identify
> modules that engage in I/O.

I think it would be an excellent idea.

Among other advantages, it makes it easier/cleaner to "mock things up"
for testing purposes.

Right now, if I want to make very small and lightweight unit-tests for
a module that uses `open', I have to do that by poking a fake 'open'
in the builtins (or in the module under test, but that may be hard to
achieve if the module imports other modules which import other modules
which...).  I do it, but not happily.

If all I/O occurred through the io module, I could mock things up in
an easier and cleaner way by sticking a "mock io module" in
sys.modules['io'] before I import from my unittest the module I'm
testing -- very similar to what I do in order to have small
lightweight tests of modules that interact with the filesystem with
functions such as os.listdir, and the like; I am far more comfortable
with this approach than I am with poking into builtins.


More information about the Python-3000 mailing list