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

@ -163,7 +163,11 @@ _wLLVMGetModuleFromAssembly(PyObject *self, PyObject *args)
{
LLVMPY_TRY
const char * str;
#if (PY_MAJOR_VERSION >= 3)
if ( !PyArg_ParseTuple(args, "y", &str) ){
#else
if ( !PyArg_ParseTuple(args, "s", &str) ){
#endif
return NULL;
}

View file

@ -101,3 +101,8 @@ def _isstring_choose():
isstring = _isstring_choose()
try:
unicode_type = unicode
except NameError: # Py3
unicode_type = str

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")