Skip to content

Commit 6d82dd3

Browse files
committed
Modify _io.py to not require any big modules, add some modules to vm/Lib
1 parent eecaaa1 commit 6d82dd3

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

vm/Lib/core_modules/codecs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../Lib/codecs.py

vm/Lib/core_modules/stat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../Lib/stat.py
File renamed without changes.

vm/Lib/python_builtins/_io.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22
Python implementation of the io module.
33
"""
44

5-
import os
6-
import abc
5+
class _UnsupportedModuleWrapper:
6+
def __init__(self, module):
7+
self.__module = module
8+
def __getattr__(self, attr):
9+
raise RuntimeError(f"the {self.__module!r} module isn't available")
10+
11+
import sys
12+
try:
13+
if 'posix' in sys.builtin_module_names:
14+
import posix as os
15+
linesep = '\n'
16+
else:
17+
import nt as os
18+
linesep = '\r\n'
19+
except ImportError:
20+
os = _UnsupportedModuleWrapper("_os")
721
import codecs
822
import errno
923
import stat
10-
import sys
1124
# Import _thread instead of threading to reduce startup cost
1225
try:
1326
from _thread import allocate_lock as Lock
@@ -18,8 +31,12 @@
1831
else:
1932
_setmode = None
2033

21-
import io
22-
from io import (__all__, SEEK_SET, SEEK_CUR, SEEK_END)
34+
del _UnsupportedModuleWrapper
35+
36+
37+
SEEK_SET = 0
38+
SEEK_CUR = 1
39+
SEEK_END = 2
2340

2441
valid_seek_flags = {0, 1, 2} # Hardwired values
2542
if hasattr(os, 'SEEK_HOLE') :
@@ -29,10 +46,6 @@
2946
# open() uses st_blksize whenever we can
3047
DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
3148

32-
# NOTE: Base classes defined here are registered with the "official" ABCs
33-
# defined in io.py. We don't use real inheritance though, because we don't want
34-
# to inherit the C implementations.
35-
3649
# Rebind for compatibility
3750
BlockingIOError = BlockingIOError
3851

@@ -272,16 +285,11 @@ def __new__(cls, *args, **kwargs):
272285
return open(*args, **kwargs)
273286

274287

275-
# In normal operation, both `UnsupportedOperation`s should be bound to the
276-
# same object.
277-
try:
278-
UnsupportedOperation = io.UnsupportedOperation
279-
except AttributeError:
280-
class UnsupportedOperation(OSError, ValueError):
281-
pass
288+
class UnsupportedOperation(OSError, ValueError):
289+
pass
282290

283291

284-
class IOBase(metaclass=abc.ABCMeta):
292+
class _IOBase:
285293

286294
"""The abstract base class for all I/O classes, acting on streams of
287295
bytes. There is no public constructor.
@@ -549,10 +557,8 @@ def writelines(self, lines):
549557
for line in lines:
550558
self.write(line)
551559

552-
io.IOBase.register(IOBase)
553-
554560

555-
class RawIOBase(IOBase):
561+
class _RawIOBase(_IOBase):
556562

557563
"""Base class for raw binary I/O."""
558564

@@ -613,12 +619,8 @@ def write(self, b):
613619
"""
614620
self._unsupported("write")
615621

616-
io.RawIOBase.register(RawIOBase)
617-
from _io import FileIO
618-
RawIOBase.register(FileIO)
619622

620-
621-
class BufferedIOBase(IOBase):
623+
class _BufferedIOBase(_IOBase):
622624

623625
"""Base class for buffered IO objects.
624626
@@ -721,10 +723,8 @@ def detach(self):
721723
"""
722724
self._unsupported("detach")
723725

724-
io.BufferedIOBase.register(BufferedIOBase)
725-
726726

727-
class _BufferedIOMixin(BufferedIOBase):
727+
class _BufferedIOMixin(_BufferedIOBase):
728728

729729
"""A mixin implementation of BufferedIOBase with an underlying raw stream.
730730
@@ -829,7 +829,7 @@ def isatty(self):
829829
return self.raw.isatty()
830830

831831

832-
class BytesIO(BufferedIOBase):
832+
class BytesIO(_BufferedIOBase):
833833

834834
"""Buffered I/O implementation using an in-memory bytes buffer."""
835835

@@ -1240,7 +1240,7 @@ def seek(self, pos, whence=0):
12401240
return _BufferedIOMixin.seek(self, pos, whence)
12411241

12421242

1243-
class BufferedRWPair(BufferedIOBase):
1243+
class BufferedRWPair(_BufferedIOBase):
12441244

12451245
"""A buffered reader and writer object together.
12461246
@@ -1387,7 +1387,7 @@ def write(self, b):
13871387
return BufferedWriter.write(self, b)
13881388

13891389

1390-
class FileIO(RawIOBase):
1390+
class FileIO(_RawIOBase):
13911391
_fd = -1
13921392
_created = False
13931393
_readable = False
@@ -1726,7 +1726,7 @@ def mode(self):
17261726
return 'wb'
17271727

17281728

1729-
class TextIOBase(IOBase):
1729+
class _TextIOBase(_IOBase):
17301730

17311731
"""Base class for text I/O.
17321732
@@ -1791,8 +1791,6 @@ def errors(self):
17911791
Subclasses should override."""
17921792
return None
17931793

1794-
io.TextIOBase.register(TextIOBase)
1795-
17961794

17971795
class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
17981796
r"""Codec used when reading a file in universal newlines mode. It wraps
@@ -1879,7 +1877,7 @@ def newlines(self):
18791877
)[self.seennl]
18801878

18811879

1882-
class TextIOWrapper(TextIOBase):
1880+
class TextIOWrapper(_TextIOBase):
18831881

18841882
r"""Character and line based layer over a BufferedIOBase object, buffer.
18851883
@@ -1950,7 +1948,7 @@ def __init__(self, buffer, encoding=None, errors=None, newline=None,
19501948
self._readtranslate = newline is None
19511949
self._readnl = newline
19521950
self._writetranslate = newline != ''
1953-
self._writenl = newline or os.linesep
1951+
self._writenl = newline or linesep
19541952
self._encoder = None
19551953
self._decoder = None
19561954
self._decoded_chars = '' # buffer for text returned from decoder

0 commit comments

Comments
 (0)