Improve bytes/unicode distinction for reading modules from bitcode & assembly

This commit is contained in:
Thomas Kluyver 2012-09-30 15:07:51 +01:00 • committed by Siu Kwan Lam
commit aba3e208d2
3 changed files with 17 additions and 2 deletions

View file

@ -351,7 +351,9 @@ class Module(llvm.Ownable, llvm.Cacheable):
a module represented in bitcode.
"""
if _util.isstring(fileobj_or_str):
if isinstance(fileobj_or_str, _util.unicode_type):
data = fileobj_or_str.encode('utf-8')
elif isinstance(fileobj_or_str, bytes):
data = fileobj_or_str
else:
data = fileobj_or_str.read()
@ -373,10 +375,14 @@ class Module(llvm.Ownable, llvm.Cacheable):
a module represented in llvm-ir assembly.
"""
if _util.isstring(fileobj_or_str):
if isinstance(fileobj_or_str, _util.unicode_type):
data = fileobj_or_str.encode('utf-8')
elif isinstance(fileobj_or_str, bytes):
data = fileobj_or_str
else:
data = fileobj_or_str.read()
if isinstance(data, _util.unicode_type):
data = data.encode()
ret = _core.LLVMGetModuleFromAssembly(data)
if not ret:
raise llvm.LLVMException("Unable to create module from assembly")