瀏覽代碼

Python 3 compatibility Frappe test fixes (#4318)

* Read image file in binary mode

* Read files in binary mode

* Don't use bytestrings unnecessarily

* Correctly convert byte strings to unicode in frappe.as_unicode

* Convert content to bytes before writing to file
version-14
Aditya Hase 7 年之前
committed by Rushabh Mehta
父節點
當前提交
38d63f7dc7
共有 4 個檔案被更改,包括 10 行新增8 行删除
  1. +2
    -2
      frappe/__init__.py
  2. +3
    -3
      frappe/email/email_body.py
  3. +2
    -2
      frappe/email/test_email_body.py
  4. +3
    -1
      frappe/utils/file_manager.py

+ 2
- 2
frappe/__init__.py 查看文件

@@ -6,7 +6,7 @@ globals attached to frappe module
"""
from __future__ import unicode_literals, print_function

from six import iteritems, text_type, string_types
from six import iteritems, binary_type, text_type, string_types
from werkzeug.local import Local, release_local
import os, sys, importlib, inspect, json

@@ -61,7 +61,7 @@ def as_unicode(text, encoding='utf-8'):
return text
elif text==None:
return ''
elif isinstance(text, string_types):
elif isinstance(text, binary_type):
return text_type(text, encoding)
else:
return text_type(text)


+ 3
- 3
frappe/email/email_body.py 查看文件

@@ -322,9 +322,9 @@ def add_attachment(fname, fcontent, content_type=None,
# Set the filename parameter
if fname:
attachment_type = 'inline' if inline else 'attachment'
part.add_header(b'Content-Disposition', attachment_type, filename=text_type(fname))
part.add_header('Content-Disposition', attachment_type, filename=text_type(fname))
if content_id:
part.add_header(b'Content-ID', '<{0}>'.format(content_id))
part.add_header('Content-ID', '<{0}>'.format(content_id))

parent.attach(part)

@@ -415,7 +415,7 @@ def get_filecontent_from_path(path):
full_path = path

if os.path.exists(full_path):
with open(full_path) as f:
with open(full_path, 'rb') as f:
filecontent = f.read()

return filecontent


+ 2
- 2
frappe/email/test_email_body.py 查看文件

@@ -21,9 +21,9 @@ This is the text version of this email
'''

img_path = os.path.abspath('assets/frappe/images/favicon.png')
with open(img_path) as f:
with open(img_path, 'rb') as f:
img_content = f.read()
img_base64 = base64.b64encode(img_content)
img_base64 = base64.b64encode(img_content).decode()

# email body keeps 76 characters on one line
self.img_base64 = fixed_column_width(img_base64, 76)


+ 3
- 1
frappe/utils/file_manager.py 查看文件

@@ -191,7 +191,9 @@ def write_file(content, fname, is_private=0):
# create directory (if not exists)
frappe.create_folder(file_path)
# write the file
with open(os.path.join(file_path.encode('utf-8'), fname.encode('utf-8')), 'w+') as f:
if isinstance(content, text_type):
content = content.encode()
with open(os.path.join(file_path.encode('utf-8'), fname.encode('utf-8')), 'wb+') as f:
f.write(content)

return get_files_path(fname, is_private=is_private)


Loading…
取消
儲存