From 323d43284749a8bf827c469be53e7f0e35793324 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Wed, 26 Sep 2012 10:19:11 -0500 Subject: [PATCH] Implements Jun Koi's suggestion to improve consistency of the api: * allow to_bitcode to return a string if no fileobj is defined. * also modify from_bitcode and from_assembly to accept a string. --- llvm/core.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/llvm/core.py b/llvm/core.py index 2aee074..da260a3 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -343,11 +343,18 @@ class Module(llvm.Ownable, llvm.Cacheable): return Module(_core.LLVMModuleCreateWithName(id)) @staticmethod - def from_bitcode(fileobj): + def from_bitcode(fileobj_or_str): """Create a Module instance from the contents of a bitcode - file.""" + file. - data = fileobj.read() + fileobj_or_str -- takes a file-like object or string that contains + a module represented in bitcode. + """ + + if isinstance(fileobj_or_str, basestring): + data = fileobj_or_str + else: + data = fileobj_or_str.read() ret = _core.LLVMGetModuleFromBitcode(data) if not ret: raise llvm.LLVMException("Unable to create module from bitcode") @@ -357,11 +364,19 @@ class Module(llvm.Ownable, llvm.Cacheable): return Module(ret) @staticmethod - def from_assembly(fileobj): + def from_assembly(fileobj_or_str): """Create a Module instance from the contents of an LLVM - assembly (.ll) file.""" + assembly (.ll) file. - data = fileobj.read() + + fileobj_or_str -- takes a file-like object or string that contains + a module represented in llvm-ir assembly. + """ + + if isinstance(fileobj_or_str, basestring): + data = fileobj_or_str + else: + data = fileobj_or_str.read() ret = _core.LLVMGetModuleFromAssembly(data) if not ret: raise llvm.LLVMException("Unable to create module from assembly") @@ -499,14 +514,24 @@ class Module(llvm.Ownable, llvm.Cacheable): if ret != "": raise llvm.LLVMException(ret) - def to_bitcode(self, fileobj): + def to_bitcode(self, fileobj=None): """Write bitcode representation of module to given file-like - object.""" + object. + + fileobj -- A file-like object to where the bitcode is written. + If it is None, the bitcode is returned. + + Return value -- Returns None if fileobj is not None. + Otherwise, return the bitcode as a bytestring. + """ data = _core.LLVMGetBitcodeFromModule(self.ptr) if not data: raise llvm.LLVMException("Unable to create bitcode") - fileobj.write(data) + if fileobj is not None: + fileobj.write(data) + else: + return data def add_library(self, name): return _core.LLVMModuleAddLibrary(self.ptr, name)