From aba3e208d29eb85ed60330befa9ea363d75834a7 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Sun, 30 Sep 2012 15:07:51 +0100 Subject: [PATCH] Improve bytes/unicode distinction for reading modules from bitcode & assembly --- llvm/_core.cpp | 4 ++++ llvm/_util.py | 5 +++++ llvm/core.py | 10 ++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/llvm/_core.cpp b/llvm/_core.cpp index b8899ef..1ec86aa 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -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; } diff --git a/llvm/_util.py b/llvm/_util.py index 8f06122..da003b0 100644 --- a/llvm/_util.py +++ b/llvm/_util.py @@ -101,3 +101,8 @@ def _isstring_choose(): isstring = _isstring_choose() +try: + unicode_type = unicode +except NameError: # Py3 + unicode_type = str + diff --git a/llvm/core.py b/llvm/core.py index 6b1b0aa..2de3615 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -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")