Replace StringIO with BytesIO

This commit is contained in:
Siu Kwan Lam 2013-02-27 13:25:19 -06:00
commit 740d2595ee
3 changed files with 17 additions and 28 deletions

View file

@ -28,10 +28,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import contextlib, weakref import contextlib, weakref
import llvm import llvm
@ -337,7 +334,7 @@ class Module(llvm.Wrapper):
bc = fileobj_or_str bc = fileobj_or_str
else: else:
bc = fileobj_or_str.read() bc = fileobj_or_str.read()
errbuf = StringIO() errbuf = BytesIO()
context = api.llvm.getGlobalContext() context = api.llvm.getGlobalContext()
m = api.llvm.ParseBitCodeFile(bc, context, errbuf) m = api.llvm.ParseBitCodeFile(bc, context, errbuf)
if not m: if not m:
@ -359,7 +356,7 @@ class Module(llvm.Wrapper):
ir = fileobj_or_str ir = fileobj_or_str
else: else:
ir = fileobj_or_str.read() ir = fileobj_or_str.read()
errbuf = StringIO() errbuf = BytesIO()
context = api.llvm.getGlobalContext() context = api.llvm.getGlobalContext()
m = api.llvm.ParseAssemblyString(ir, None, api.llvm.SMDiagnostic.new(), m = api.llvm.ParseAssemblyString(ir, None, api.llvm.SMDiagnostic.new(),
context) context)
@ -432,7 +429,7 @@ class Module(llvm.Wrapper):
enum_mode = api.llvm.Linker.LinkerMode enum_mode = api.llvm.Linker.LinkerMode
mode = enum_mode.PreserveSource if preserve else enum_mode.DestroySource mode = enum_mode.PreserveSource if preserve else enum_mode.DestroySource
with contextlib.closing(StringIO()) as errmsg: with contextlib.closing(BytesIO()) as errmsg:
failed = api.llvm.Linker.LinkModules(self._ptr, failed = api.llvm.Linker.LinkModules(self._ptr,
other._ptr, other._ptr,
mode, mode,
@ -512,7 +509,7 @@ class Module(llvm.Wrapper):
Checks module for errors. Raises `llvm.LLVMException' on any Checks module for errors. Raises `llvm.LLVMException' on any
error.""" error."""
action = api.llvm.VerifierFailureAction.ReturnStatusAction action = api.llvm.VerifierFailureAction.ReturnStatusAction
errio = StringIO() errio = BytesIO()
broken = api.llvm.verifyModule(self._ptr, action, errio) broken = api.llvm.verifyModule(self._ptr, action, errio)
if broken: if broken:
raise llvm.LLVMException(errio.getvalue()) raise llvm.LLVMException(errio.getvalue())
@ -530,7 +527,7 @@ class Module(llvm.Wrapper):
ret = False ret = False
if fileobj is None: if fileobj is None:
ret = True ret = True
fileobj = StringIO fileobj = BytesIO
api.llvm.WriteBitcodeToFile(self._ptr, fileobj) api.llvm.WriteBitcodeToFile(self._ptr, fileobj)
if ret: if ret:
return fileobj.getvalue() return fileobj.getvalue()
@ -571,7 +568,7 @@ class Module(llvm.Wrapper):
ret = False ret = False
if fileobj is None: if fileobj is None:
ret = True ret = True
fileobj = StringIO() fileobj = BytesIO()
from llvm.ee import TargetMachine from llvm.ee import TargetMachine
tm = TargetMachine.new() tm = TargetMachine.new()
fileobj.write(tm.emit_object(self)) fileobj.write(tm.emit_object(self))
@ -588,7 +585,7 @@ class Module(llvm.Wrapper):
ret = False ret = False
if fileobj is None: if fileobj is None:
ret = True ret = True
fileobj = StringIO() fileobj = BytesIO()
from llvm.ee import TargetMachine from llvm.ee import TargetMachine
tm = TargetMachine.new() tm = TargetMachine.new()
fileobj.write(tm.emit_assembly(self)) fileobj.write(tm.emit_assembly(self))
@ -2269,7 +2266,7 @@ def load_library_permanently(filename):
Load the given shared library (filename argument specifies the full Load the given shared library (filename argument specifies the full
path of the .so file) using LLVM. Symbols from these are available path of the .so file) using LLVM. Symbols from these are available
from the execution engine thereafter.""" from the execution engine thereafter."""
with contextlib.closing(StringIO()) as errmsg: with contextlib.closing(BytesIO()) as errmsg:
failed = api.llvm.sys.DynamicLibrary.LoadPermanentLibrary(filename, failed = api.llvm.sys.DynamicLibrary.LoadPermanentLibrary(filename,
errmsg) errmsg)
if failed: if failed:

View file

@ -30,10 +30,7 @@
"Execution Engine and related classes." "Execution Engine and related classes."
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import contextlib import contextlib
import llvm import llvm
@ -244,7 +241,7 @@ class TargetMachine(llvm.Wrapper):
triple = get_default_triple() triple = get_default_triple()
if not cpu: if not cpu:
cpu = get_host_cpu_name() cpu = get_host_cpu_name()
with contextlib.closing(StringIO()) as error: with contextlib.closing(BytesIO()) as error:
target = api.llvm.TargetRegistry.lookupTarget(triple, error) target = api.llvm.TargetRegistry.lookupTarget(triple, error)
if not target: if not target:
raise llvm.LLVMException(error) raise llvm.LLVMException(error)
@ -273,7 +270,7 @@ class TargetMachine(llvm.Wrapper):
use: `llvm-as < /dev/null | llc -march=xyz -mattr=help` use: `llvm-as < /dev/null | llc -march=xyz -mattr=help`
''' '''
triple = api.llvm.Triple.new() triple = api.llvm.Triple.new()
with contextlib.closing(StringIO()) as error: with contextlib.closing(BytesIO()) as error:
target = api.llvm.TargetRegistry.lookupTarget(arch, triple, error) target = api.llvm.TargetRegistry.lookupTarget(arch, triple, error)
if not target: if not target:
raise llvm.LLVMException(error) raise llvm.LLVMException(error)

View file

@ -10,13 +10,8 @@ import subprocess
import tempfile import tempfile
import contextlib import contextlib
is_py3k = bool(sys.version_info[0] == 3)
BITS = tuple.__itemsize__ * 8 BITS = tuple.__itemsize__ * 8
from io import BytesIO
if is_py3k:
from io import StringIO
else:
from cStringIO import StringIO
import llvm import llvm
@ -123,7 +118,7 @@ class TestAttr(TestCase):
ret void ret void
} }
""" """
return Module.from_assembly(StringIO(test_module)) return Module.from_assembly(BytesIO(test_module))
def test_align(self): def test_align(self):
m = self.make_module() m = self.make_module()
@ -255,7 +250,7 @@ entry:
} }
""" """
def test_operands(self): def test_operands(self):
m = Module.from_assembly(StringIO(self.test_module)) m = Module.from_assembly(BytesIO(self.test_module))
test_func = m.get_function_named("test_func") test_func = m.get_function_named("test_func")
prod = m.get_function_named("prod") prod = m.get_function_named("prod")
@ -317,7 +312,7 @@ entry:
} }
""" """
def test_passes(self): def test_passes(self):
m = Module.from_assembly(StringIO(self.asm)) m = Module.from_assembly(BytesIO(self.asm))
fn_test1 = m.get_function_named('test1') fn_test1 = m.get_function_named('test1')
fn_test2 = m.get_function_named('test2') fn_test2 = m.get_function_named('test2')
@ -365,7 +360,7 @@ entry:
self.assertNotEqual(str(fn_test1).strip(), original_test1.strip()) self.assertNotEqual(str(fn_test1).strip(), original_test1.strip())
def test_passes_with_pmb(self): def test_passes_with_pmb(self):
m = Module.from_assembly(StringIO(self.asm)) m = Module.from_assembly(BytesIO(self.asm))
fn_test1 = m.get_function_named('test1') fn_test1 = m.get_function_named('test1')
fn_test2 = m.get_function_named('test2') fn_test2 = m.get_function_named('test2')