I am having trouble with testing the PaaSIO exercise . I’m obviously missing something important. When I run the tests, I get a whole lot of failures/errors like this:
_________________ PaasioTest.test_meteredfile_context_manager __________________
self = <paasio_test.PaasioTest testMethod=test_meteredfile_context_manager>
super_mock = <SuperMock at 0x74f434541d60 with mock object: <NonCallableMagicMock id='128592190498832'>>
@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_context_manager(self, super_mock):
wrapped = MockFile(ZEN)
mock = NonCallableMagicMock(wraps=wrapped, autospec=True)
mock.__exit__.side_effect = wrapped.__exit__
super_mock.mock_object = mock
with MeteredFile() as file:
self.assertEqual(1, super_mock.init_called)
self.assertFalse(mock.__enter__.called)
> file.read()
paasio_test.py:224:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <MeteredFile>, size = -1
def read(self, size=-1):
if self._closed:
raise ValueError("I/O operation on closed file.")
> data = super.read(size)
E AttributeError: 'SuperMock' object has no attribute 'read'
paasio.py:31: AttributeError
I don’t get why super.read()
(and other super calls) get this error. If I’m not supposed to write super.read()
, how do I call the superclass’s read()
method?
IsaacG
November 19, 2024, 8:40pm
2
Could you share your entire code?
It’s not complete yet…
import io
class MeteredFile(io.BufferedRandom):
"""Implement using a subclassing model."""
def __init__(self, *args, **kwargs):
super.__init__(*args, **kwargs)
self._read_bytes = 0
self._read_ops = 0
self._write_bytes = 0
self._write_ops = 0
self._closed = False
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self._closed = True
def __iter__(self):
pass
def __next__(self):
pass
def read(self, size=-1):
if self._closed:
raise ValueError("I/O operation on closed file.")
data = super.read(size)
self._read_bytes += len(data)
self._read_ops += 1
return data
@property
def read_bytes(self):
return self._read_bytes
@property
def read_ops(self):
return self._read_ops
def write(self, b):
if self._closed:
raise ValueError("I/O operation on closed file.")
super.write(b)
self._write_bytes += len(b)
self._write_ops += 1
@property
def write_bytes(self):
return self._write_bytes
@property
def write_ops(self):
return self._write_ops
class MeteredSocket:
"""Implement using a delegation model."""
def __init__(self, socket):
self._socket = socket
self._recv_size = 0
self._recv_ops = 0
self._send_size = 0
self._send_ops = 0
self._closed = False
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self._closed = True
def recv(self, bufsize, flags=0):
if self._closed:
raise OSError("Bad file descriptor")
if bufsize is None:
raise TypeError("'NoneType' is not valid, must be an integer")
data = self._socket.recv(bufsize, flags=flags)
self._recv_size += bufsize
self._recv_ops += 1
return data
@property
def recv_bytes(self):
return self._recv_size
@property
def recv_ops(self):
return self._recv_ops
def send(self, data, flags=0):
if self._closed:
raise OSError("Bad file descriptor")
self._socket.send(data, flag=flags)
self._send_size += len(data)
self._send_ops += 1
@property
def send_bytes(self):
return self._send_size
@property
def send_ops(self):
return self._send_ops
IsaacG
November 20, 2024, 12:17am
4
super.read()
and super.write()
should be super().read()
and super().write()
.
Thank you! I was going nuts.