bskinn / stdio-mgr

Context manager for mocking/wrapping stdin/stdout/stderr
MIT License
14 stars 4 forks source link

MicroPython implementation and tests #96

Open bskinn opened 5 years ago

bskinn commented 5 years ago

MicroPython types appear not to implement .mro(), in either the Python 2 or Python 3 forms:

>>> str.__bases__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'str' has no attribute '__bases__'
>>> import io
>>> io.StringIO.__bases__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'StringIO' has no attribute '__bases__'
>>> io.StringIO.__mro__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'StringIO' has no attribute '__mro__'
>>> dir(io.StringIO)
['__class__', '__enter__', '__exit__', '__name__', 'close', 'read', 'readinto', 'readline', 'write', 'flush', 'getvalue', 'seek']

Is there a way to introspect the inheritance hierarchy in MP?

Also: Yikes, the MRO itself is not CPython-compliant.

Adapted from original post by @bskinn in https://github.com/bskinn/stdio-mgr/issues/81#issuecomment-531810997

bskinn commented 5 years ago

MicroPython implements a significantly reduced class hierarchy in its io module. Presumably the sys.stdfoo also live on this reduced hierarchy?

At the very least, despite being uio.TextIOWrapper, they appear to have no concept of an internal buffer than can be detached, in addition to a much reduced set of attributes:

$ micropython 
MicroPython v1.11-303-gc8c37ca40 on 2019-09-10; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import sys
>>> type(sys.stdout)
<class 'TextIOWrapper'>
>>> dir(sys.stdout)
['__class__', '__enter__', '__exit__', '__next__', 'close', 'read', 'readinto', 'readline', 'write', 'fileno', 'flush', 'readlines', 'seek', 'tell']

$ python3.7
Python 3.7.4 (default, Sep 12 2019, 13:06:26) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> dir(sys.stdout)
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']
bskinn commented 4 years ago

Might be worth just testing MP at stdio-mgr v1.0.1, modded to remove attrs.

bskinn commented 4 years ago

MicroPython appears to have crippled sys, not allowing its members to be set. Other modules are not crippled in this way:

>>> import io
>>> io.foo = "bar"
>>> io
<module 'io' from '/home/pydev/.micropython/lib/io.py'>
>>> dir(io)
['__class__', '__name__', 'open', '__file__', 'BytesIO', 'FileIO', 'IOBase', 'StringIO', 'TextIOWrapper', 'SEEK_SET', 'SEEK_CUR', 'SEEK_END', 'foo']
>>> import sys
>>> sys.foo = "bar"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'foo'
>>> import unittest
>>> unittest.foo = "bar"
>>> dir(unittest)
['__class__', '__name__', 'main', '__file__', 'sys', 'skip', 'foo', 'SkipTest', 'AssertRaisesContext', 'TestCase', 'skipIf', 'skipUnless', 'TestSuite', 'TestRunner', 'TestResult', 'run_class']
bskinn commented 4 years ago

Given the above, @jayvdb, I'm at a loss as to how to implement even basic stream wraps/mocks. I think MicroPython may be a nonstarter.