Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
78 lines (63 sloc) 2.04 KB
import weakref
from ._lowlevel import ffi, lib
from .exceptions import exceptions_by_code, SourmashError
attached_refs = weakref.WeakKeyDictionary()
class RustObject(object):
__dealloc_func__ = None
_objptr = None
_shared = False
def __init__(self):
raise TypeError("Cannot instanciate %r objects" % self.__class__.__name__)
def _from_objptr(cls, ptr, shared=False):
rv = object.__new__(cls)
rv._objptr = ptr
rv._shared = shared
return rv
def _methodcall(self, func, *args):
return rustcall(func, self._get_objptr(), *args)
def _get_objptr(self):
if not self._objptr:
raise RuntimeError("Object is closed")
return self._objptr
def __del__(self):
if self._objptr is None or self._shared:
f = self.__class__.__dealloc_func__
if f is not None:
rustcall(f, self._objptr)
self._objptr = None
def decode_str(s, free=False):
"""Decodes a SourmashStr"""
if s.len == 0:
return u""
return ffi.unpack(, s.len).decode("utf-8", "replace")
if free:
def encode_str(s):
"""Encodes a SourmashStr"""
rv ="SourmashStr *")
if isinstance(s, text_type):
s = s.encode("utf-8") = ffi.from_buffer(s)
rv.len = len(s)
# we have to hold a weak reference here to ensure our string does not
# get collected before the string is used.
attached_refs[rv] = s
return rv
def rustcall(func, *args):
"""Calls rust method and does some error handling."""
rv = func(*args)
err = lib.sourmash_err_get_last_code()
if not err:
return rv
msg = lib.sourmash_err_get_last_message()
cls = exceptions_by_code.get(err, SourmashError)
exc = cls(decode_str(msg))
backtrace = decode_str(lib.sourmash_err_get_backtrace())
if backtrace:
exc.rust_info = backtrace
raise exc