From 6525aa3c58da2ee1eed0f9b058df1de2b614ac6d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 17 Apr 2006 05:36:47 +0000 Subject: [PATCH] * symrec.pxi: Change various things to properties rather than functions. Visibility is handled via set of strings. * test_symrec.py: Test. svn path=/trunk/yasm/; revision=1503 --- tools/python-yasm/symrec.pxi | 62 +++++++++++++++++++------- tools/python-yasm/tests/test_symrec.py | 53 +++++++++++++++++++--- 2 files changed, 92 insertions(+), 23 deletions(-) diff --git a/tools/python-yasm/symrec.pxi b/tools/python-yasm/symrec.pxi index 212e5ba4..500d93fe 100644 --- a/tools/python-yasm/symrec.pxi +++ b/tools/python-yasm/symrec.pxi @@ -63,6 +63,7 @@ cdef extern from "libyasm/symrec.h": cdef int yasm_symrec_get_label(yasm_symrec *sym, yasm_symrec_get_label_bytecodep *precbc) cdef int yasm_symrec_is_special(yasm_symrec *sym) + cdef int yasm_symrec_is_curpos(yasm_symrec *sym) cdef void* yasm_symrec_get_data(yasm_symrec *sym, yasm_assoc_data_callback *callback) cdef void yasm_symrec_add_data(yasm_symrec *sym, @@ -81,18 +82,41 @@ cdef class Symbol: # no deref or destroy necessary - def get_name(self): return yasm_symrec_get_name(self.sym) - def get_visibility(self): return yasm_symrec_get_visibility(self.sym) - def get_equ(self): - return __make_expression(yasm_expr_copy(yasm_symrec_get_equ(self.sym))) - def get_label(self): - cdef yasm_symrec_get_label_bytecodep bc - if yasm_symrec_get_label(self.sym, &bc): pass # TODO - #return Bytecode(bc) - else: raise TypeError("Symbol '%s' is not a label" % self.get_name()) - - def get_is_special(self): - return yasm_symrec_is_special(self.sym) + property name: + def __get__(self): return yasm_symrec_get_name(self.sym) + + property visibility: + def __get__(self): + cdef yasm_sym_vis vis + s = set() + vis = yasm_symrec_get_visibility(self.sym) + if vis & YASM_SYM_GLOBAL: s.add('global') + if vis & YASM_SYM_COMMON: s.add('common') + if vis & YASM_SYM_EXTERN: s.add('extern') + if vis & YASM_SYM_DLOCAL: s.add('dlocal') + return s + + property equ: + def __get__(self): + cdef yasm_expr *e + e = yasm_symrec_get_equ(self.sym) + if not e: + raise AttributeError("not an EQU") + return __make_expression(yasm_expr_copy(e)) + + property label: + def __get__(self): + cdef yasm_symrec_get_label_bytecodep bc + if yasm_symrec_get_label(self.sym, &bc): + return None #Bytecode(bc) + else: + raise AttributeError("not a label or not defined") + + property is_special: + def __get__(self): return bool(yasm_symrec_is_special(self.sym)) + + property is_curpos: + def __get__(self): return bool(yasm_symrec_is_curpos(self.sym)) def get_data(self): pass # TODO #return (yasm_symrec_get_data(self.sym, PyYasmAssocData)) @@ -222,7 +246,14 @@ cdef class SymbolTable: vis)) def declare(self, name, vis, line): - return __make_symbol(yasm_symtab_declare(self.symtab, name, vis, line)) + cdef yasm_sym_vis cvis + if not vis or vis == 'local': cvis = YASM_SYM_LOCAL + elif vis == 'global': cvis = YASM_SYM_GLOBAL + elif vis == 'common': cvis = YASM_SYM_COMMON + elif vis == 'extern': cvis = YASM_SYM_EXTERN + elif vis == 'dlocal': cvis = YASM_SYM_DLOCAL + else: raise ValueError("bad visibility value '%s'" % vis) + return __make_symbol(yasm_symtab_declare(self.symtab, name, cvis, line)) # # Methods to make SymbolTable behave like a dictionary of Symbols. @@ -284,8 +315,5 @@ cdef class SymbolTable: def iterkeys(self): return SymbolTableKeyIterator(self) def itervalues(self): return SymbolTableValueIterator(self) def iteritems(self): return SymbolTableItemIterator(self) - - # This doesn't follow Python's guideline to make this iterkeys() for - # mappings, but makes more sense in this context, e.g. for sym in symtab. - def __iter__(self): return SymbolTableValueIterator(self) + def __iter__(self): return SymbolTableKeyIterator(self) diff --git a/tools/python-yasm/tests/test_symrec.py b/tools/python-yasm/tests/test_symrec.py index eff6a3b8..e61f4d9f 100644 --- a/tools/python-yasm/tests/test_symrec.py +++ b/tools/python-yasm/tests/test_symrec.py @@ -3,11 +3,52 @@ from tests import TestCase, add from yasm import SymbolTable class TSymbolTable(TestCase): + def setUp(self): + self.symtab = SymbolTable() def test_keys(self): - symtab = SymbolTable() - self.failUnlessEqual(len(symtab.keys()), 0) - symtab.declare("foo", 0, 0) - keys = symtab.keys() - self.failUnlessEqual(len(keys), 1) - self.failUnlessEqual(keys[0], "foo") + self.assertEquals(len(self.symtab.keys()), 0) + self.symtab.declare("foo", None, 0) + keys = self.symtab.keys() + self.assertEquals(len(keys), 1) + self.assertEquals(keys[0], "foo") add(TSymbolTable) + +class TSymbolAttr(TestCase): + def setUp(self): + self.symtab = SymbolTable() + self.declsym = self.symtab.declare("foo", None, 0) + + def test_visibility(self): + sym = self.symtab.declare("local1", None, 0) + self.assertEquals(sym.visibility, set()) + sym = self.symtab.declare("local2", '', 0) + self.assertEquals(sym.visibility, set()) + sym = self.symtab.declare("local3", 'local', 0) + self.assertEquals(sym.visibility, set()) + sym = self.symtab.declare("global", 'global', 0) + self.assertEquals(sym.visibility, set(['global'])) + sym = self.symtab.declare("common", 'common', 0) + self.assertEquals(sym.visibility, set(['common'])) + sym = self.symtab.declare("extern", 'extern', 0) + self.assertEquals(sym.visibility, set(['extern'])) + sym = self.symtab.declare("dlocal", 'dlocal', 0) + self.assertEquals(sym.visibility, set(['dlocal'])) + + self.assertRaises(ValueError, + lambda: self.symtab.declare("extern2", 'foo', 0)) + def test_name(self): + self.assertEquals(self.declsym.name, "foo") + + def test_equ(self): + self.assertRaises(AttributeError, lambda: self.declsym.equ) + + def test_label(self): + self.assertRaises(AttributeError, lambda: self.declsym.label) + + def test_is_special(self): + self.assertEquals(self.declsym.is_special, False) + + def test_is_curpos(self): + self.assertEquals(self.declsym.is_curpos, False) + +add(TSymbolAttr) -- 2.40.0