Skip to content

Commit 415b480

Browse files
tipabuDean Troyer
authored andcommitted
Before writing object data to stdout, re-open it in binary mode
Otherwise, you can hit TypeErrors on Python3. Change-Id: I9a891508886feddac3982ce593bd95130392e035 Closes-Bug: 1775482
1 parent c53de32 commit 415b480

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

openstackclient/api/object_store_v1.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,9 @@ def object_save(
378378
)
379379
if response.status_code == 200:
380380
if file == '-':
381-
for chunk in response.iter_content(64 * 1024):
382-
sys.stdout.write(chunk)
381+
with os.fdopen(sys.stdout.fileno(), 'wb') as f:
382+
for chunk in response.iter_content(64 * 1024):
383+
f.write(chunk)
383384
else:
384385
if not os.path.exists(os.path.dirname(file)):
385386
if len(os.path.dirname(file)) > 0:

openstackclient/tests/unit/object/v1/test_object_all.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,25 @@ def test_save_to_stdout(self):
241241

242242
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
243243

244-
with mock.patch('sys.stdout', new=six.BytesIO()) as fake_stdout:
244+
class FakeStdout(six.BytesIO):
245+
def __init__(self):
246+
six.BytesIO.__init__(self)
247+
self.context_manager_calls = []
248+
249+
def __enter__(self):
250+
self.context_manager_calls.append('__enter__')
251+
return self
252+
253+
def __exit__(self, *a):
254+
self.context_manager_calls.append('__exit__')
255+
256+
with mock.patch('sys.stdout') as fake_stdout, mock.patch(
257+
'os.fdopen', return_value=FakeStdout()) as fake_fdopen:
258+
fake_stdout.fileno.return_value = 123
245259
self.cmd.take_action(parsed_args)
246260

247-
self.assertEqual(fake_stdout.getvalue(), object_fakes.object_1_content)
261+
self.assertEqual(fake_fdopen.return_value.getvalue(),
262+
object_fakes.object_1_content)
263+
self.assertEqual(fake_fdopen.mock_calls, [mock.call(123, 'wb')])
264+
self.assertEqual(fake_fdopen.return_value.context_manager_calls,
265+
['__enter__', '__exit__'])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Re-open stdout in binary mode before writing object data in
5+
``object save --file -`` command.
6+
[Bug `1775482 <https://bugs.launchpad.net/python-openstackclient/+bug/1775482>`_]

0 commit comments

Comments
 (0)