def test_cons_ctr(): a = e.intern('a') b = e.intern('b') assert repr(e.cons(a, b)) == '(a . b)' assert repr(e.cons(a)) == '(a)' assert repr(e.cons()) == 'nil'
def test_list(): list = e.intern('list') assert e.functionp(list) a = e.intern('a') b = e.intern('b') c = e.intern('c') lst = list(a, b, c) assert e.equal(lst, list(a, b, c)) assert not e.eq(lst, list(a, b, c)) assert repr(lst) == '(a b c)' assert str(lst) == '(a b c)' with pytest.raises(TypeError): int(lst) with pytest.raises(TypeError): float(lst) assert lst.type() == 'cons' assert lst.is_a('cons') assert not e.integerp(lst) assert not e.floatp(lst) assert not e.stringp(lst) assert not e.symbolp(lst) assert e.consp(lst) assert not e.vectorp(lst) assert e.listp(lst) assert not e.functionp(lst) assert lst
def test_vector(): vector = e.intern('vector') assert e.functionp(vector) a = e.intern('a') b = e.intern('b') c = e.intern('c') vec = vector(a, b, c) assert e.equal(vec, vector(a, b, c)) assert not e.eq(vec, vector(a, b, c)) assert repr(vec) == '[a b c]' assert str(vec) == '[a b c]' with pytest.raises(TypeError): int(vec) with pytest.raises(TypeError): float(vec) assert vec.type() == 'vector' assert vec.is_a('vector') assert not e.integerp(vec) assert not e.floatp(vec) assert not e.stringp(vec) assert not e.symbolp(vec) assert not e.consp(vec) assert e.vectorp(vec) assert not e.listp(vec) assert not e.functionp(vec) assert vec
def _colonify(key): if _keywordp(key): return key key = str(key) if key.startswith(':'): return intern(key) return intern(':' + key)
def test_binding(): get = lambda s: e.intern('symbol-value')(e.intern(s)) from emacs.emacs import version assert e.eq(version[binding], get('emacs-version')) import emacs with pytest.raises(NameError): emacs.doesnt.exist[fbinding]
def test_bound(): from emacs.emacs import version assert version[bound()] == e.intern('emacs-version') import emacs with pytest.raises(NameError): emacs.doesnt.exist[bound()] assert emacs.doesnt.exist[bound(exists=False)] == e.intern('doesnt-exist')
def test_call(): def a(): return e.int(1) func = e.function(a, 0, 0) e.intern('fset')(e.intern('test-symbol'), func) from emacs import test assert test.symbol() == e.int(1)
def test_sym(): from emacs.emacs import version assert version[sym] == e.intern('emacs-version') from emacs.doesnt import exist assert exist[sym] == e.intern('doesnt-exist') import emacs beta = emacs[seps('$')].alpha.beta assert beta[sym] == e.intern('alpha$beta')
def test_place(): obj = PlaceOrSymbol() assert er.eq(obj.place, er.intern('nil')) assert er.eq(er.EmacsObject(obj), er.intern('nil')) new_place = er.list([er.int(1), er.int(2), er.int(3)]) obj._bind(new_place) assert er.eq(obj.place, new_place) assert er.eq(er.EmacsObject(obj), new_place)
def test_length(): a = e.intern('a') b = e.intern('b') c = e.intern('c') assert len(e.list([a, b, c])) == 3 assert len(e.vector([a, b])) == 2 assert len(e.str('alpha')) == 5 assert len(e.intern('nil')) == 0 with pytest.raises(TypeError): len(e.cons(a, b))
def test_cons(): cons = e.intern('cons') assert e.functionp(cons) a = e.intern('a') b = e.intern('b') c = e.intern('c') nil = e.intern('nil') assert repr(cons(kwd='value')) == '(:kwd . "value")' cell = cons(a, b) lst = cons(a, cons(b, cons(c, nil))) assert e.equal(cell, cons(a, b)) assert not e.eq(cell, cons(a, b)) assert repr(cell) == '(a . b)' assert str(cell) == '(a . b)' with pytest.raises(TypeError): int(cell) with pytest.raises(TypeError): float(cell) assert cell.type() == 'cons' assert cell.is_a('cons') assert not e.integerp(cell) assert not e.floatp(cell) assert not e.stringp(cell) assert not e.symbolp(cell) assert e.consp(cell) assert not e.vectorp(cell) assert e.listp(cell) assert not e.functionp(cell) assert cell assert e.equal(lst, cons(a, cons(b, cons(c, nil)))) assert not e.eq(lst, cons(a, cons(b, cons(c, nil)))) assert repr(lst) == '(a b c)' assert str(lst) == '(a b c)' with pytest.raises(TypeError): int(lst) with pytest.raises(TypeError): float(lst) assert lst.type() == 'cons' assert lst.is_a('cons') assert not e.integerp(lst) assert not e.floatp(lst) assert not e.stringp(lst) assert not e.symbolp(lst) assert e.consp(lst) assert not e.vectorp(lst) assert e.listp(lst) assert not e.functionp(lst) assert lst
def __init__(self, place=None): if place is None: self._place = intern('nil') elif isinstance(place, EmacsNamespace): self._symbol = place[bound(exists=False)] elif isinstance(place, str): self._symbol = intern(place) elif not isinstance(place, EmacsObject): raise TypeError('Invalid place') elif not symbolp(place): self._place = place else: self._symbol = place
def test_vector_ctr(): a = e.intern('a') b = e.intern('b') c = e.intern('c') assert repr(e.vector([a, b, c])) == '[a b c]' assert repr(e.vector((a, b, c))) == '[a b c]' assert repr(e.vector(iter([a, b, c]))) == '[a b c]' assert repr(e.vector([])) == '[]' assert repr(e.vector()) == '[]' assert repr(e.vector([a, b, c])[0]) == 'a' assert repr(e.vector([a, b, c])[-1]) == 'c' with pytest.raises(IndexError): e.vector()[0]
def test_prefer_symbol(): @coerce(prefer_symbol=True) def test(a): assert er.symbolp(a) test('a') test(er.intern('a'))
def test_prefer_symbol_none_but_passed(): @coerce(prefer_symbol=False) def test(a, b, c): assert not er.symbolp(a) assert er.symbolp(b) assert not er.symbolp(c) test('a', er.intern('b'), 'c')
def test_error(): list = e.intern('list') error = e.intern('error') msg = e.str('An error message') with pytest.raises(e.Signal) as ex: error(msg) sym, data = ex.value.args assert e.eq(sym, e.intern('error')) assert e.equal(data, list(msg)) throw = e.intern('throw') tag = e.intern('tag') value = e.int(1) with pytest.raises(e.Throw) as ex: throw(tag, value) sym, data = ex.value.args assert e.eq(sym, e.intern('tag')) assert e.equal_sign(data, value) def err(): error = e.intern('error') list = e.intern('list') raise e.Signal(error, list(e.str('message'))) func = e.function(err, 0, 0) with pytest.raises(e.Signal) as ex: func() sym, data = ex.value.args assert e.eq(sym, e.intern('error')) assert e.equal(data, list(e.str('message')))
def test_list_ctr(): a = e.intern('a') b = e.intern('b') c = e.intern('c') assert repr(e.list([a, b, c])) == '(a b c)' assert repr(e.list((a, b, c))) == '(a b c)' assert repr(e.list(iter([a, b, c]))) == '(a b c)' assert repr(e.list([])) == 'nil' assert repr(e.list()) == 'nil' assert repr(e.list([a, b, c])[0]) == 'a' assert repr(e.list([a, b, c])[-1]) == 'c' with pytest.raises(TypeError): a[0] with pytest.raises(IndexError): e.list()[0]
def test_empty(): l = List() setq(_('test'), er.intern('nil')) l = List(bind='test') l.push(_('a')) l.push(_('b')) l.push(_('c')) assert list(l) == py_list('cba')
def test_symbol(): mylist = er.list([er.int(1), er.int(2), er.int(3)]) sym = er.intern('alpha') setq = er.intern('set') setq(sym, mylist) obj = PlaceOrSymbol(sym) assert er.eq(obj.place, mylist) assert er.eq(er.EmacsObject(obj), mylist) mylist = er.list([er.int(1), er.int(2), er.int(3), er.int(4)]) obj._bind(mylist) assert er.eq(obj._symbol, er.intern('alpha')) assert er.eq(obj.place, mylist) assert er.eq(er.EmacsObject(obj), mylist) assert er.eq(er.intern('symbol-value')(er.intern('alpha')), mylist)
def test_almost_none_but_passed(): @coerce('a') def test(a, b, c, *args, **kwargs): assert isinstance(a, er.EmacsObject) assert not isinstance(b, er.EmacsObject) assert isinstance(c, er.EmacsObject) assert not isinstance(args, er.EmacsObject) assert not isinstance(kwargs, er.EmacsObject) for arg in args: assert not isinstance(arg, er.EmacsObject) for name, kwarg in kwargs.items(): assert not isinstance(name, er.EmacsObject) assert not isinstance(kwarg, er.EmacsObject) test(1, 2, er.int(3)) test('a', [34], er.intern('q')) test('a', [34], er.intern('q'), 'b', 'c', q='d')
def test_nil(): nil = e.intern('nil') assert e.eq(nil, e.intern('nil')) assert repr(nil) == 'nil' assert str(nil) == 'nil' with pytest.raises(TypeError): int(nil) with pytest.raises(TypeError): float(nil) assert nil.type() == 'symbol' assert nil.is_a('symbol') assert not e.integerp(nil) assert not e.floatp(nil) assert not e.stringp(nil) assert e.symbolp(nil) assert not e.consp(nil) assert not e.vectorp(nil) assert e.listp(nil) assert not e.functionp(nil) assert not nil
def test_insert(): l = List() setq(_('test'), er.intern('nil')) l = List(bind=_('test')) l.insert(0, _('a')) l.insert(0, _('b')) l.insert(1, _('c')) assert list(l) == py_list('bca') l.insert(3, _('d')) assert list(l) == py_list('bcad')
def test_intern(): alpha = e.intern('alpha') assert e.eq(alpha, e.intern('alpha')) assert repr(alpha) == 'alpha' assert str(alpha) == 'alpha' with pytest.raises(TypeError): int(alpha) with pytest.raises(TypeError): float(alpha) assert alpha.type() == 'symbol' assert alpha.is_a('symbol') assert not e.integerp(alpha) assert not e.floatp(alpha) assert not e.stringp(alpha) assert e.symbolp(alpha) assert not e.consp(alpha) assert not e.vectorp(alpha) assert not e.listp(alpha) assert not e.functionp(alpha) assert alpha with pytest.raises(TypeError): e.intern(2) with pytest.raises(TypeError): e.intern([])
def test_num(): assert e.int(0) + e.int(1) == 1 assert e.int(3) + 8 == 11 assert 7 + e.float(-3.1) == 3.9 assert 'alpha' + e.str('bravo') == 'alphabravo' assert e.int(8) - e.int(1) == 7 assert 13 - e.float(2.2) == 10.8 assert e.int(5) * e.float(2.2) == 11.0 assert 7 * e.int(2) == 14 assert e.int(17) // 3 == 5 assert 7 / e.int(2) == 3.5 assert e.int(8) / e.int(-1) == -8.0 assert divmod(e.int(10), e.int(3)) == (3, 1) assert 2**e.int(3) == 8 assert e.float(3.0)**e.int(3) == 27.0 assert pow(e.int(2), e.int(4), e.int(5)) == 1 assert -e.int(2) == -2 assert -e.float(-2.2) == 2.2 assert +e.int(-2) == -2 assert ~e.int(1) == ~1 assert (e.int(8) << 1) == 16 assert 8 >> e.int(1) == 4 with pytest.raises(TypeError): 13 - e.str('a') with pytest.raises(TypeError): e.intern('a') + 2 with pytest.raises(TypeError): 2 << e.intern('b') with pytest.raises(TypeError): pow(1, 2, e.intern('q'))
def test_setattr(): from emacs import test test.symbol = 'alpha' assert e.intern('symbol-value')(e.intern('test-symbol')) == 'alpha'
from collections import OrderedDict from collections.abc import MutableMapping from tripoli.util import PlaceOrSymbol, coerce from emacs_raw import intern, cons, EmacsObject, eq _car = intern('car') _cdr = intern('cdr') _cadr = intern('cadr') _cddr = intern('cddr') _setcar = intern('setcar') _setcdr = intern('setcdr') _keywordp = intern('keywordp') def _colonify(key): if _keywordp(key): return key key = str(key) if key.startswith(':'): return intern(key) return intern(':' + key) class PList(PlaceOrSymbol, MutableMapping): """Wraps an Emacs property list in a Pythonic mapping interface. :param place: The property list to wrap. See :class:`.PlaceOrSymbol` for more information. :param colonify: If true, keys are modified to start with colons.
from collections.abc import MutableSequence from tripoli.util import PlaceOrSymbol, coerce from emacs_raw import intern, cons _length = intern('length') _car = intern('car') _cdr = intern('cdr') _setcar = intern('setcar') _setcdr = intern('setcdr') def _push_head(cell, value): tail = cons(_car(cell), _cdr(cell)) _setcar(cell, value) _setcdr(cell, tail) class List(PlaceOrSymbol, MutableSequence): """Wraps an Emacs list in a Pythonic list interface. :param place: The list to wrap. See :class:`.PlaceOrSymbol` for more information. :param prefer_symbol: If true, elements are coerced to symbols when possible. """ def __init__(self, initializer=None, bind=None, prefer_symbol=None): PlaceOrSymbol.__init__(self, bind) self.prefer_symbol = prefer_symbol if initializer is not None: self.clear()
from collections import OrderedDict from collections.abc import MutableMapping from tripoli.util import PlaceOrSymbol, coerce from emacs_raw import eq, cons, intern _nil = intern('nil') _car = intern('car') _cdr = intern('cdr') _setcdr = intern('setcdr') _keywordp = intern('keywordp') _listp = intern('listp') def _colonify(key): if _keywordp(key): return key key = str(key) if key.startswith(':'): return intern(key) return intern(':' + key) def _copy_list(cell, endp=lambda c: not c): head, tail = None, None while cell and not endp(cell): if head is None: head = cons(_car(cell)) tail = head else: new_tail = cons(_car(cell))
from collections import namedtuple from importlib.machinery import ModuleSpec from itertools import groupby, product from functools import partial import emacs_raw _symbol_value = emacs_raw.intern('symbol-value') _symbol_function = emacs_raw.intern('symbol-function') _fboundp = emacs_raw.intern('fboundp') _boundp = emacs_raw.intern('boundp') _setq = emacs_raw.intern('set') class EmacsNamespaceFinder: def find_spec(self, name, path, target): if not name.startswith('emacs'): return None module = EmacsNamespace() for p in name.split('.')[1:]: module = getattr(module, p) return ModuleSpec(name, EmacsNamespaceLoader(module), origin='emacs') class EmacsNamespaceLoader: def __init__(self, module): self.module = module def create_module(self, spec): return self.module
def __symbols(self, convert=True): iters = [] for parts in product(*self.__prefix[::-1]): name = ''.join(parts[::-1]) if convert: yield emacs_raw.intern(name) else: yield name