Skip to content

Commit

Permalink
#49 fix utf8 issue while read module in python3 and python2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
apisu authored and apisu committed Dec 20, 2017
1 parent f7abd88 commit 74ff15b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 9 additions & 3 deletions python/cohorte/repositories/python/ipopo.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,16 @@ def _extract_module_factories(filename):
"""
visitor = ComponentFactoryVisitor()
try:
with open(filename, encoding="utf-8") as filep:
with open(filename,encoding="utf8") as filep:
source = filep.read()
except (OSError, IOError) as ex:
raise ValueError("Error reading {0}: {1}".format(filename, ex))
except (OSError, IOError,TypeError) as ex:
try:
import io
with io.open(filename,encoding="utf8") as filep:
source = filep.read()
except (OSError, IOError) as ex2:
_logger.exception(ex2)
raise ValueError("Error reading {0}: {1}".format(filename, ex2))

try:
module = ast.parse(source, filename, 'exec')
Expand Down
13 changes: 10 additions & 3 deletions python/cohorte/repositories/python/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,17 @@ def _extract_module_info(filename, module_name, is_package):
:raise ValueError: Unreadable file
"""
try:
with open(filename, encoding="utf-8") as filep:
with open(filename,encoding="utf8") as filep:
source = filep.read()
except (OSError, IOError) as ex:
raise ValueError("Error reading {0}: {1}".format(filename, ex))
except (OSError, IOError,TypeError) as ex:
try:
import io
with io.open(filename,encoding="utf8") as filep:
source = filep.read()
except (OSError, IOError) as ex2:
_logger.exception(ex2)
raise ValueError("Error reading {0}: {1}".format(filename, ex))


visitor = AstVisitor(module_name, is_package)
try:
Expand Down

0 comments on commit 74ff15b

Please sign in to comment.