amoffat / sh

Python process launching

Home Page:https://sh.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[bug] Exception when creating instance of ErrorReturnCode

honnix opened this issue · comments

commented

sh Version: 2.0.3
Python version: 3.8.16

>>> from sh import ErrorReturnCode
>>> ErrorReturnCode("", None, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../site-packages/sh.py", line 313, in __init__
    self.exit_code = self.exit_code  # makes pylint happy
AttributeError: 'ErrorReturnCode' object has no attribute 'exit_code'

ErrorReturnCode instances are not meant to be created like that. They are created from the metaclass. See e.g. https://github.com/amoffat/sh/blob/develop/sh.py#L445

Do you have an actual use case where being able to do the above would be useful?

commented

Yeah you are absolutely right, instantiating this class is not needed in production code. I'm doing this in the context of unit test and mocking.

Something like this:

erc = ErrorReturnCode("foo", b"stdout", b"stderr", False)
    erc.exit_code = 42
    mock_run_func.side_effect = erc
commented

Hmm, I think I can do this instead:

from sh import ErrorReturnCode_42

erc = ErrorReturnCode_42("foo", b"stdout", b"stderr", False)

Yes, that would be the recommended solution. Closing, but feel free to reopen if you have additional questions related to this.