[Python-checkins] r70033 - in python/branches/io-c: Lib/_pyio.py Lib/io.py Lib/test/test_io.py Modules/_bufferedio.c Modules/_bytesio.c Modules/_fileio.c Modules/_iobase.c Modules/_stringio.c Modules/_textio.c Modules/io.c

antoine.pitrou python-checkins at python.org
Fri Feb 27 22:49:51 CET 2009


Author: antoine.pitrou
Date: Fri Feb 27 22:49:50 2009
New Revision: 70033
Log:
The base classes now are ABCs.
Modified:
 python/branches/io-c/Lib/_pyio.py
 python/branches/io-c/Lib/io.py
 python/branches/io-c/Lib/test/test_io.py
 python/branches/io-c/Modules/_bufferedio.c
 python/branches/io-c/Modules/_bytesio.c
 python/branches/io-c/Modules/_fileio.c
 python/branches/io-c/Modules/_iobase.c
 python/branches/io-c/Modules/_stringio.c
 python/branches/io-c/Modules/_textio.c
 python/branches/io-c/Modules/io.c
Modified: python/branches/io-c/Lib/_pyio.py
==============================================================================
--- python/branches/io-c/Lib/_pyio.py	(original)
+++ python/branches/io-c/Lib/_pyio.py	Fri Feb 27 22:49:50 2009
@@ -5,18 +5,22 @@
 import os
 import abc
 import codecs
-#import _fileio
 # Import _thread instead of threading to reduce startup cost
 try:
 from _thread import allocate_lock as Lock
 except ImportError:
 from _dummy_thread import allocate_lock as Lock
 
+import io
 from io import __all__
 
 # open() uses st_blksize whenever we can
 DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
 
+# NOTE: Base classes defined here are registered with the "official" ABCs
+# defined in io.py. We don't use real inheritance though, because we don't
+# want to inherit the C implementations.
+
 
 class BlockingIOError(IOError):
 
@@ -496,6 +500,8 @@
 for line in lines:
 self.write(line)
 
+io.IOBase.register(IOBase)
+
 
 class RawIOBase(IOBase):
 
@@ -551,7 +557,7 @@
 """
 self._unsupported("write")
 
-
+io.RawIOBase.register(RawIOBase)
 from _io import FileIO
 RawIOBase.register(FileIO)
 
@@ -627,6 +633,8 @@
 """
 self._unsupported("write")
 
+io.BufferedIOBase.register(BufferedIOBase)
+
 
 class _BufferedIOMixin(BufferedIOBase):
 
@@ -1218,6 +1226,8 @@
 """
 return None
 
+io.TextIOBase.register(TextIOBase)
+
 
 class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
 r"""Codec used when reading a file in universal newlines mode. It wraps
Modified: python/branches/io-c/Lib/io.py
==============================================================================
--- python/branches/io-c/Lib/io.py	(original)
+++ python/branches/io-c/Lib/io.py	Fri Feb 27 22:49:50 2009
@@ -54,24 +54,47 @@
 
 
 import _io
+import abc
 
+# open() uses st_blksize whenever we can
 DEFAULT_BUFFER_SIZE = _io.DEFAULT_BUFFER_SIZE
 BlockingIOError = _io.BlockingIOError
 UnsupportedOperation = _io.UnsupportedOperation
 open = _io.open
 OpenWrapper = _io.open
-IOBase = _io.IOBase
-RawIOBase = _io.RawIOBase
+
+# Declaring ABCs in C is tricky so we do it here.
+# Method descriptions and default implementations are inherited from the C
+# version however.
+class IOBase(_io._IOBase, metaclass=abc.ABCMeta):
+ pass
+
+class RawIOBase(_io._RawIOBase, IOBase):
+ pass
+
+class BufferedIOBase(_io._BufferedIOBase, IOBase):
+ pass
+
+class TextIOBase(_io._TextIOBase, IOBase):
+ pass
+
 FileIO = _io.FileIO
-BufferedIOBase = _io.BufferedIOBase
 BytesIO = _io.BytesIO
 StringIO = _io.StringIO
 BufferedReader = _io.BufferedReader
 BufferedWriter = _io.BufferedWriter
 BufferedRWPair = _io.BufferedRWPair
 BufferedRandom = _io.BufferedRandom
-TextIOBase = _io.TextIOBase
 IncrementalNewlineDecoder = _io.IncrementalNewlineDecoder
 TextIOWrapper = _io.TextIOWrapper
-# open() uses st_blksize whenever we can
-DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
+
+RawIOBase.register(FileIO)
+
+BufferedIOBase.register(BytesIO)
+BufferedIOBase.register(BufferedReader)
+BufferedIOBase.register(BufferedWriter)
+BufferedIOBase.register(BufferedRandom)
+BufferedIOBase.register(BufferedRWPair)
+
+TextIOBase.register(StringIO)
+TextIOBase.register(TextIOWrapper)
Modified: python/branches/io-c/Lib/test/test_io.py
==============================================================================
--- python/branches/io-c/Lib/test/test_io.py	(original)
+++ python/branches/io-c/Lib/test/test_io.py	Fri Feb 27 22:49:50 2009
@@ -28,6 +28,7 @@
 import unittest
 import weakref
 import gc
+import abc
 from itertools import chain, cycle, count
 from collections import deque
 from test import support
@@ -2043,23 +2044,38 @@
 gc.collect()
 self.assert_(wr() is None, wr)
 
- def test_abc_inheritance(self):
- # Test implementations inherit (even virtually) from their respective ABCs
+ def test_abcs(self):
+ # Test the visible base classes are ABCs.
+ self.assertTrue(isinstance(self.IOBase, abc.ABCMeta))
+ self.assertTrue(isinstance(self.RawIOBase, abc.ABCMeta))
+ self.assertTrue(isinstance(self.BufferedIOBase, abc.ABCMeta))
+ self.assertTrue(isinstance(self.TextIOBase, abc.ABCMeta))
+
+ def _check_abc_inheritance(self, abcmodule):
 f = self.open(support.TESTFN, "wb", buffering=0)
- self.assertTrue(isinstance(f, self.IOBase))
- self.assertTrue(isinstance(f, self.RawIOBase))
- self.assertFalse(isinstance(f, self.BufferedIOBase))
- self.assertFalse(isinstance(f, self.TextIOBase))
+ self.assertTrue(isinstance(f, abcmodule.IOBase))
+ self.assertTrue(isinstance(f, abcmodule.RawIOBase))
+ self.assertFalse(isinstance(f, abcmodule.BufferedIOBase))
+ self.assertFalse(isinstance(f, abcmodule.TextIOBase))
 f = self.open(support.TESTFN, "wb")
- self.assertTrue(isinstance(f, self.IOBase))
- self.assertFalse(isinstance(f, self.RawIOBase))
- self.assertTrue(isinstance(f, self.BufferedIOBase))
- self.assertFalse(isinstance(f, self.TextIOBase))
+ self.assertTrue(isinstance(f, abcmodule.IOBase))
+ self.assertFalse(isinstance(f, abcmodule.RawIOBase))
+ self.assertTrue(isinstance(f, abcmodule.BufferedIOBase))
+ self.assertFalse(isinstance(f, abcmodule.TextIOBase))
 f = self.open(support.TESTFN, "w")
- self.assertTrue(isinstance(f, self.IOBase))
- self.assertFalse(isinstance(f, self.RawIOBase))
- self.assertFalse(isinstance(f, self.BufferedIOBase))
- self.assertTrue(isinstance(f, self.TextIOBase))
+ self.assertTrue(isinstance(f, abcmodule.IOBase))
+ self.assertFalse(isinstance(f, abcmodule.RawIOBase))
+ self.assertFalse(isinstance(f, abcmodule.BufferedIOBase))
+ self.assertTrue(isinstance(f, abcmodule.TextIOBase))
+
+ def test_abc_inheritance(self):
+ # Test implementations inherit from their respective ABCs
+ self._check_abc_inheritance(self)
+
+ def test_abc_inheritance_official(self):
+ # Test implementations inherit from the official ABCs of the
+ # baseline "io" module.
+ self._check_abc_inheritance(io)
 
 class CMiscIOTest(MiscIOTest):
 io = io
Modified: python/branches/io-c/Modules/_bufferedio.c
==============================================================================
--- python/branches/io-c/Modules/_bufferedio.c	(original)
+++ python/branches/io-c/Modules/_bufferedio.c	Fri Feb 27 22:49:50 2009
@@ -136,7 +136,7 @@
 
 PyTypeObject PyBufferedIOBase_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "BufferedIOBase", /*tp_name*/
+ "_io._BufferedIOBase", /*tp_name*/
 0, /*tp_basicsize*/
 0, /*tp_itemsize*/
 0, /*tp_dealloc*/
@@ -1374,7 +1374,7 @@
 
 PyTypeObject PyBufferedReader_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "BufferedReader", /*tp_name*/
+ "_io.BufferedReader", /*tp_name*/
 sizeof(BufferedObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)BufferedObject_dealloc, /*tp_dealloc*/
@@ -1712,7 +1712,7 @@
 
 PyTypeObject PyBufferedWriter_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "BufferedWriter", /*tp_name*/
+ "_io.BufferedWriter", /*tp_name*/
 sizeof(BufferedObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)BufferedObject_dealloc, /*tp_dealloc*/
@@ -1957,8 +1957,8 @@
 
 PyTypeObject PyBufferedRWPair_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "BufferedRWPair", /*tp_name*/
- sizeof(BufferedRWPairObject), /*tp_basicsize*/
+ "_io.BufferedRWPair", /*tp_name*/
+ sizeof(BufferedRWPairObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)BufferedRWPair_dealloc, /*tp_dealloc*/
 0, /*tp_print*/
@@ -2088,7 +2088,7 @@
 
 PyTypeObject PyBufferedRandom_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "BufferedRandom", /*tp_name*/
+ "_io.BufferedRandom", /*tp_name*/
 sizeof(BufferedObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)BufferedObject_dealloc, /*tp_dealloc*/
Modified: python/branches/io-c/Modules/_bytesio.c
==============================================================================
--- python/branches/io-c/Modules/_bytesio.c	(original)
+++ python/branches/io-c/Modules/_bytesio.c	Fri Feb 27 22:49:50 2009
@@ -716,7 +716,7 @@
 
 PyTypeObject PyBytesIO_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "_bytesio._BytesIO", /*tp_name*/
+ "_io.BytesIO", /*tp_name*/
 sizeof(BytesIOObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)bytesio_dealloc, /*tp_dealloc*/
Modified: python/branches/io-c/Modules/_fileio.c
==============================================================================
--- python/branches/io-c/Modules/_fileio.c	(original)
+++ python/branches/io-c/Modules/_fileio.c	Fri Feb 27 22:49:50 2009
@@ -956,7 +956,7 @@
 
 PyTypeObject PyFileIO_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
-	"FileIO",
+	"_io.FileIO",
 	sizeof(PyFileIOObject),
 	0,
 	(destructor)fileio_dealloc,		/* tp_dealloc */
Modified: python/branches/io-c/Modules/_iobase.c
==============================================================================
--- python/branches/io-c/Modules/_iobase.c	(original)
+++ python/branches/io-c/Modules/_iobase.c	Fri Feb 27 22:49:50 2009
@@ -706,7 +706,7 @@
 
 PyTypeObject PyIOBase_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "IOBase", /*tp_name*/
+ "_io._IOBase", /*tp_name*/
 sizeof(IOBaseObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)IOBase_dealloc, /*tp_dealloc*/
@@ -862,7 +862,7 @@
 
 PyTypeObject PyRawIOBase_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "RawIOBase", /*tp_name*/
+ "_io._RawIOBase", /*tp_name*/
 0, /*tp_basicsize*/
 0, /*tp_itemsize*/
 0, /*tp_dealloc*/
Modified: python/branches/io-c/Modules/_stringio.c
==============================================================================
--- python/branches/io-c/Modules/_stringio.c	(original)
+++ python/branches/io-c/Modules/_stringio.c	Fri Feb 27 22:49:50 2009
@@ -728,7 +728,7 @@
 
 PyTypeObject PyStringIO_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "StringIO", /*tp_name*/
+ "_io.StringIO", /*tp_name*/
 sizeof(StringIOObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)stringio_dealloc, /*tp_dealloc*/
Modified: python/branches/io-c/Modules/_textio.c
==============================================================================
--- python/branches/io-c/Modules/_textio.c	(original)
+++ python/branches/io-c/Modules/_textio.c	Fri Feb 27 22:49:50 2009
@@ -107,7 +107,7 @@
 
 PyTypeObject PyTextIOBase_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "TextIOBase", /*tp_name*/
+ "_io._TextIOBase", /*tp_name*/
 0, /*tp_basicsize*/
 0, /*tp_itemsize*/
 0, /*tp_dealloc*/
@@ -526,7 +526,7 @@
 
 PyTypeObject PyIncrementalNewlineDecoder_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "IncrementalNewlineDecoder", /*tp_name*/
+ "_io.IncrementalNewlineDecoder", /*tp_name*/
 sizeof(PyNewLineDecoderObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)IncrementalNewlineDecoder_dealloc, /*tp_dealloc*/
@@ -2348,7 +2348,7 @@
 
 PyTypeObject PyTextIOWrapper_Type = {
 PyVarObject_HEAD_INIT(NULL, 0)
- "TextIOWrapper", /*tp_name*/
+ "_io.TextIOWrapper", /*tp_name*/
 sizeof(PyTextIOWrapperObject), /*tp_basicsize*/
 0, /*tp_itemsize*/
 (destructor)TextIOWrapper_dealloc, /*tp_dealloc*/
Modified: python/branches/io-c/Modules/io.c
==============================================================================
--- python/branches/io-c/Modules/io.c	(original)
+++ python/branches/io-c/Modules/io.c	Fri Feb 27 22:49:50 2009
@@ -653,18 +653,15 @@
 _PyExc_BlockingIOError.tp_base = (PyTypeObject *) PyExc_IOError;
 ADD_TYPE(&_PyExc_BlockingIOError, "BlockingIOError");
 
- /* IOBase */
- ADD_TYPE(&PyIOBase_Type, "IOBase");
-
- /* RawIOBase */
- ADD_TYPE(&PyRawIOBase_Type, "RawIOBase");
-
- /* BufferedIOBase */
- ADD_TYPE(&PyBufferedIOBase_Type, "BufferedIOBase");
-
- /* TextIOBase */
- ADD_TYPE(&PyTextIOBase_Type,"TextIOBase");
+ /* Concrete base types of the IO ABCs.
+ (the ABCs themselves are declared through inheritance in io.py)
+ */
+ ADD_TYPE(&PyIOBase_Type, "_IOBase");
+ ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase");
+ ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase");
+ ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase");
 
+ /* Implementation of concrete IO objects. */
 /* FileIO */
 PyFileIO_Type.tp_base = &PyRawIOBase_Type;
 ADD_TYPE(&PyFileIO_Type, "FileIO");


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /