Skip to content

Commit c460267

Browse files
author
Steve Canny
committed
py3: add docx.compat module
* StringIO aliased to BytesIO * remove ascii check in setup.py * add Pillow dependency to tox.ini
1 parent 0832d7d commit c460267

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

docx/compat.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Provides Python 2/3 compatibility objects
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
import sys
12+
13+
# ===========================================================================
14+
# Python 3 versions
15+
# ===========================================================================
16+
17+
if sys.version_info >= (3, 0):
18+
19+
from io import BytesIO
20+
21+
# ===========================================================================
22+
# Python 2 versions
23+
# ===========================================================================
24+
25+
else:
26+
27+
from StringIO import StringIO as BytesIO # noqa

docx/parts/image.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
except ImportError:
1717
import Image as PIL_Image
1818

19-
from StringIO import StringIO
20-
19+
from docx.compat import BytesIO
2120
from docx.opc.constants import CONTENT_TYPE as CT
2221
from docx.opc.package import Part
2322
from docx.shared import Emu, Inches, lazyproperty
@@ -72,7 +71,7 @@ def filename(self):
7271

7372
@classmethod
7473
def from_blob(cls, blob):
75-
stream = StringIO(blob)
74+
stream = BytesIO(blob)
7675
return cls._from_stream(stream, blob)
7776

7877
@classmethod
@@ -85,7 +84,7 @@ def from_file(cls, image_descriptor):
8584
path = image_descriptor
8685
with open(path, 'rb') as f:
8786
blob = f.read()
88-
stream = StringIO(blob)
87+
stream = BytesIO(blob)
8988
filename = os.path.basename(path)
9089
else:
9190
stream = image_descriptor

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def text_of(relpath):
1515
file_path = os.path.join(thisdir, os.path.normpath(relpath))
1616
with open(file_path) as f:
1717
text = f.read()
18-
text.decode('ascii') # result discarded, just make sure no non-ascii chars
1918
return text
2019

2120
# Read the version from docx.__version__ without importing the package

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ deps =
2323
lxml
2424
mock
2525
pytest
26+
Pillow
2627

2728
commands =
2829
py.test -qx

0 commit comments

Comments
 (0)