Skip to content

Commit

Permalink
[NFC][Py Reformat] Reformat python files in llvm
Browse files Browse the repository at this point in the history
This is the first commit in a series that will reformat
all the python files in the LLVM repository.

Reformatting is done with `black`.

See more information here:

https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style

Reviewed By: jhenderson, JDevlieghere, MatzeB

Differential Revision: https://reviews.llvm.org/D150545
  • Loading branch information
tru committed May 17, 2023
1 parent 7beb2ca commit b71edfa
Show file tree
Hide file tree
Showing 262 changed files with 17,956 additions and 14,089 deletions.
9 changes: 6 additions & 3 deletions llvm/bindings/python/llvm/bit_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from .common import LLVMObject
from .common import c_object_p
from .common import get_library
Expand All @@ -10,21 +9,25 @@
from ctypes import byref
from ctypes import c_char_p
from ctypes import cast
__all__ = ['parse_bitcode']

__all__ = ["parse_bitcode"]
lib = get_library()


def parse_bitcode(mem_buffer):
"""Input is .core.MemoryBuffer"""
module = c_object_p()
result = lib.LLVMParseBitcode2(mem_buffer, byref(module))
if result:
raise RuntimeError('LLVM Error')
raise RuntimeError("LLVM Error")
m = Module(module)
m.take_ownership(mem_buffer)
return m


def register_library(library):
library.LLVMParseBitcode2.argtypes = [MemoryBuffer, POINTER(c_object_p)]
library.LLVMParseBitcode2.restype = bool


register_library(lib)
33 changes: 19 additions & 14 deletions llvm/bindings/python/llvm/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#===- common.py - Python LLVM Bindings -----------------------*- python -*--===#
# ===- common.py - Python LLVM Bindings -----------------------*- python -*--===#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#===------------------------------------------------------------------------===#
# ===------------------------------------------------------------------------===#

from ctypes import POINTER
from ctypes import c_void_p
Expand All @@ -15,20 +15,22 @@

# LLVM_VERSION: sync with PACKAGE_VERSION in CMakeLists.txt
# but leave out the 'svn' suffix.
LLVM_VERSION = '10.0.0'
LLVM_VERSION = "10.0.0"

__all__ = [
'c_object_p',
'get_library',
"c_object_p",
"get_library",
]

c_object_p = POINTER(c_void_p)


class LLVMObject(object):
"""Base class for objects that are backed by an LLVM data structure.
This class should never be instantiated outside of this package.
"""

def __init__(self, ptr, ownable=True, disposer=None):
assert isinstance(ptr, c_object_p)

Expand Down Expand Up @@ -61,24 +63,26 @@ def from_param(self):
return self._as_parameter_

def __del__(self):
if not hasattr(self, '_self_owned') or not hasattr(self, '_disposer'):
if not hasattr(self, "_self_owned") or not hasattr(self, "_disposer"):
return

if self._self_owned and self._disposer:
self._disposer(self)


class CachedProperty(object):
"""Decorator that caches the result of a property lookup.
This is a useful replacement for @property. It is recommended to use this
decorator on properties that invoke C API calls for which the result of the
call will be idempotent.
"""

def __init__(self, wrapped):
self.wrapped = wrapped
try:
self.__doc__ = wrapped.__doc__
except: # pragma: no cover
except: # pragma: no cover
pass

def __get__(self, instance, instance_type=None):
Expand All @@ -90,6 +94,7 @@ def __get__(self, instance, instance_type=None):

return value


def get_library():
"""Obtain a reference to the llvm library."""

Expand All @@ -101,14 +106,14 @@ def get_library():
# library into a default linker search path. Always Try ctypes.cdll.LoadLibrary()
# with all possible library names first, then try ctypes.util.find_library().

names = ['LLVM-' + LLVM_VERSION, 'LLVM-' + LLVM_VERSION + 'svn']
names = ["LLVM-" + LLVM_VERSION, "LLVM-" + LLVM_VERSION + "svn"]
t = platform.system()
if t == 'Darwin':
pfx, ext = 'lib', '.dylib'
elif t == 'Windows':
pfx, ext = '', '.dll'
if t == "Darwin":
pfx, ext = "lib", ".dylib"
elif t == "Windows":
pfx, ext = "", ".dll"
else:
pfx, ext = 'lib', '.so'
pfx, ext = "lib", ".so"

for i in names:
try:
Expand All @@ -122,4 +127,4 @@ def get_library():
t = ctypes.util.find_library(i)
if t:
return cdll.LoadLibrary(t)
raise Exception('LLVM shared library not found!')
raise Exception("LLVM shared library not found!")
Loading

0 comments on commit b71edfa

Please sign in to comment.