mirror of
https://github.com/rpm-software-management/tito.git
synced 2025-02-23 20:22:46 +00:00
Define a small abstraction for mocking builtins
It's better to do the python2 and python3 compatibility magic on one place rather than having having `if PY2` conditions everywhere and figuring whether to use `builtins.foo` or `__builtin__.foo over and over again.
This commit is contained in:
parent
6e1ebf4a3f
commit
ecd5718f62
1 changed files with 11 additions and 5 deletions
|
@ -25,6 +25,16 @@ is_rawhide = sys.version_info[:2] >= (3, 8)
|
||||||
is_epel6 = sys.version_info[:2] == (2, 6)
|
is_epel6 = sys.version_info[:2] == (2, 6)
|
||||||
|
|
||||||
|
|
||||||
|
if PY2:
|
||||||
|
builtins = "__builtin__"
|
||||||
|
builtins_open = "__builtin__.open"
|
||||||
|
builtins_input = "__builtin__.raw_input"
|
||||||
|
else:
|
||||||
|
builtins = "builtins"
|
||||||
|
builtins_open = "builtins.open"
|
||||||
|
builtins_input = "builtins.input"
|
||||||
|
|
||||||
|
|
||||||
file_spec = None
|
file_spec = None
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,11 +102,7 @@ def open_mock(content, **kwargs):
|
||||||
|
|
||||||
content_out = StringIO()
|
content_out = StringIO()
|
||||||
|
|
||||||
if PY2:
|
with patch(builtins_open, m, create=True, **kwargs) as mo:
|
||||||
patch_module = "__builtin__.open"
|
|
||||||
else:
|
|
||||||
patch_module = "builtins.open"
|
|
||||||
with patch(patch_module, m, create=True, **kwargs) as mo:
|
|
||||||
stream = StringIO(content)
|
stream = StringIO(content)
|
||||||
rv = mo.return_value
|
rv = mo.return_value
|
||||||
rv.write = lambda x: content_out.write(bytes(x, "utf-8"))
|
rv.write = lambda x: content_out.write(bytes(x, "utf-8"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue