Author pitrou
Recipients amaury.forgeotdarc, benjamin.peterson, christian.heimes, exarkun, giampaolo.rodola, gregory.p.smith, ialbert, pitrou, rhettinger, wplappert
Date 2009-02-25.15:14:59
SpamBayes Score 8.76521e-14
Marked as misclassified No
Message-id <1235574904.03.0.830057342029.issue4565@psf.upfronthosting.co.za>
In-reply-to
Content
I just took a quick look at Lib/abc.py and there's no way *I*'ll
reimplement it in C :)

The only workable approach would be:
1. rename the current would-be ABCs (IOBase, RawIOBase, etc.) with a
leading underscore (_IOBase, _RawIOBase, etc.)
2. call abc.ABCMeta() with the right arguments to create heap-types 
derived from those base types
3. call XXXIOBase.register() with each of the concrete classes
(BufferedReader, etc.) to register them with the ABCs created in 2

That is, do something like the following:

>>> IOBase = abc.ABCMeta("IOBase", (_io.IOBase,), {})
>>> RawIOBase = type("RawIOBase", (_io.RawIOBase, IOBase), {})
>>> RawIOBase.register(_io.FileIO)
>>> TextIOBase = type("TextIOBase", (_io.TextIOBase, IOBase), {})
>>> TextIOBase.register(_io.TextIOWrapper)

Which gives:
>>> f = open('foobar', 'wb', buffering=0)
>>> isinstance(f, RawIOBase)
True
>>> isinstance(f, IOBase)
True
>>> f = open('foobar', 'w')
>>> isinstance(f, IOBase)
True
>>> isinstance(f, TextIOBase)
True
>>> isinstance(f, RawIOBase)
False


As you see, RawIOBase inherits both from IOBase (the ABC, for ABC-ness)
and _RawIOBase (the concrete non-ABC implementation). Implementation
classes like FileIO don't need to explicitly inherit the ABCs, only to
register with them.

Also, writing a Python implementation still inherits the
close-on-destroy behaviour:

>>> class S(RawIOBase):
...   def close(self):
...     print("closing")
... 
>>> s = S()
>>> del s
closing
>>> 

Perhaps we could even do all this in Python in io.py?
History
Date User Action Args
2009-02-25 15:15:04pitrousetrecipients: + pitrou, rhettinger, gregory.p.smith, exarkun, amaury.forgeotdarc, giampaolo.rodola, christian.heimes, benjamin.peterson, wplappert, ialbert
2009-02-25 15:15:04pitrousetmessageid: <1235574904.03.0.830057342029.issue4565@psf.upfronthosting.co.za>
2009-02-25 15:15:01pitroulinkissue4565 messages
2009-02-25 15:15:00pitroucreate