Skip to content

Instantly share code, notes, and snippets.

@wolfhechel
Created February 28, 2015 07:54
Show Gist options
  • Save wolfhechel/3b8a517ed2582b633928 to your computer and use it in GitHub Desktop.
Save wolfhechel/3b8a517ed2582b633928 to your computer and use it in GitHub Desktop.
ctypes changes between Python 2 and Python 3
import ctypes
import ctypes.util
def load_libcap(lib_name=None):
if lib_name is None:
lib_name = 'cap'
if not load_libcap.loaded_library:
libcap_name = ctypes.util.find_library(lib_name)
if not libcap_name:
raise LookupError('Failed to load libcap, no library found with name %s' % lib_name)
lib = ctypes.cdll.LoadLibrary(libcap_name)
if not lib:
raise IOError('Failed to load libcap, LoadLibrary(%s) failed' % libcap_name)
load_libcap.loaded_library = lib
return load_libcap.loaded_library
load_libcap.loaded_library = None
libcap = load_libcap()
capabilities = libcap.cap_get_proc()
# Always negative integer (example -1585665644) in Python 3,
# always positive integer (example 15452516) in Python 2
print(capabilities)
# Segfaults in Python 3,
# Real output (example = cap_chown,cap_dac_override,cap_dac_read_search,) in Python 2
libcap.cap_to_text.restype = ctypes.c_char_p
print(libcap.cap_to_text(capabilities, None))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment