Skip to content

Commit 70ce190

Browse files
author
Steve Canny
committed
py3: add docx.opc.compat module
* extract is_string()
1 parent c460267 commit 70ce190

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

docx/opc/compat.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
def is_string(obj):
20+
"""
21+
Return True if *obj* is a string, False otherwise.
22+
"""
23+
return isinstance(obj, str)
24+
25+
# ===========================================================================
26+
# Python 2 versions
27+
# ===========================================================================
28+
29+
else:
30+
31+
def is_string(obj):
32+
"""
33+
Return True if *obj* is a string, False otherwise.
34+
"""
35+
return isinstance(obj, basestring)

docx/opc/phys_pkg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from zipfile import ZipFile, is_zipfile, ZIP_DEFLATED
1212

13+
from .compat import is_string
1314
from .exceptions import PackageNotFoundError
1415
from .packuri import CONTENT_TYPES_URI
1516

@@ -20,7 +21,7 @@ class PhysPkgReader(object):
2021
"""
2122
def __new__(cls, pkg_file):
2223
# if *pkg_file* is a string, treat it as a path
23-
if isinstance(pkg_file, basestring):
24+
if is_string(pkg_file):
2425
if os.path.isdir(pkg_file):
2526
reader_cls = _DirPkgReader
2627
elif is_zipfile(pkg_file):

0 commit comments

Comments
 (0)