Skip to content

Commit

Permalink
change _read_as_base64 (b64encode returns bytes) (#8759)
Browse files Browse the repository at this point in the history
  • Loading branch information
em1le committed Jan 7, 2024
1 parent f957397 commit 232acf9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 2 additions & 3 deletions celery/utils/term.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Terminals and colors."""
import base64
import codecs
import os
import platform
import sys
Expand Down Expand Up @@ -166,9 +165,9 @@ def supports_images():


def _read_as_base64(path):
with codecs.open(path, mode='rb') as fh:
with open(path, mode='rb') as fh:
encoded = base64.b64encode(fh.read())
return encoded if isinstance(encoded, str) else encoded.decode('ascii')
return encoded.decode('ascii')


def imgcat(path, inline=1, preserve_aspect_ratio=0, **kwargs):
Expand Down
17 changes: 16 additions & 1 deletion t/unit/utils/test_term.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from base64 import b64encode
from tempfile import NamedTemporaryFile

import pytest

import t.skip
from celery.utils import term
from celery.utils.term import colored, fg
from celery.utils.term import _read_as_base64, colored, fg


@t.skip.if_win32
Expand Down Expand Up @@ -55,3 +58,15 @@ def test_more_unicode(self):
c2 = colored().blue('ƒƒz')
c3 = c._add(c, c2)
assert c3 == '\x1b[1;31m\xe5foo\x1b[0m\x1b[1;34m\u0192\u0192z\x1b[0m'

def test_read_as_base64(self):
test_data = b"The quick brown fox jumps over the lazy dog"
with NamedTemporaryFile(mode='wb') as temp_file:
temp_file.write(test_data)
temp_file.seek(0)
temp_file_path = temp_file.name

result = _read_as_base64(temp_file_path)
expected_result = b64encode(test_data).decode('ascii')

assert result == expected_result

0 comments on commit 232acf9

Please sign in to comment.