Mercurial > pytave
changeset 55:04e18a0cf25d pytave-native
Tried to implement delitem
author | David Grundberg <individ@acc.umu.se> |
---|---|
date | Mon, 08 Jun 2009 20:02:16 +0200 |
parents | 9180ce65a7bd |
children | 46e6fa692e0c |
files | package/pytave.py pytave.cc test/test.py |
diffstat | 3 files changed, 68 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/package/pytave.py Thu Jun 04 16:40:52 2009 +0200 +++ b/package/pytave.py Mon Jun 08 20:02:16 2009 +0200 @@ -160,6 +160,11 @@ """See Octave documentation""" return _pytave.feval(1, "path", paths)[0] +#xxx +_pytave.setvar("foo", "value", True) +print _pytave.isvar("foo", True) +print _pytave.getvar("foo", True, True) +#xxx class _VariablesDict(UserDict.DictMixin): def __init__(self, global_variables, native): self.global_variables = global_variables @@ -183,6 +188,15 @@ raise TypeError('Expected a string, not a ' + repr(type(name))) return _pytave.isvar(name, self.global_variables) + def __delitem__(self, name): + if not isinstance(name, basestring): + raise TypeError('Expected a string, not a ' + repr(type(name))) + try: + _pytave.clearvar(name, self.global_variables) + except VarNameError: + raise KeyError('No Octave variable named ' + name) + + locals = _VariablesDict(global_variables=False, native=False) globals = _VariablesDict(global_variables=True, native=False) native_locals = _VariablesDict(global_variables=False, native=True)
--- a/pytave.cc Thu Jun 04 16:40:52 2009 +0200 +++ b/pytave.cc Mon Jun 08 20:02:16 2009 +0200 @@ -252,12 +252,30 @@ } bool isvar(const string& name, bool global) { - bool retval; + octave_value val; if (global) - retval = symbol_table::is_global (name); + val = symbol_table::global_varval(name); else - retval = symbol_table::is_local_variable (name); + val = symbol_table::varval(name); + + return val.is_defined(); + } + + bool clearvar(const string& name, bool global) { + bool retval; + + if (! isvar (name, global)) + { + throw variable_name_exception (name + " not defined in " + ((global) ? "global" : "local") + " table"); + } + +// if (global) +// symbol_table::clear_global (name); +// else +// symbol_table::clear_variable (name); + symbol_table::clear_global (name); + symbol_table::clear_variable (name); return retval; } @@ -289,6 +307,7 @@ def("getvar", pytave::getvar); def("setvar", pytave::setvar); def("isvar", pytave::isvar); + def("clearvar", pytave::clearvar); def("push_scope", pytave::push_scope); def("pop_scope", pytave::pop_scope); def("get_exceptions", pytave::get_exceptions);
--- a/test/test.py Thu Jun 04 16:40:52 2009 +0200 +++ b/test/test.py Mon Jun 08 20:02:16 2009 +0200 @@ -124,20 +124,20 @@ try: variables[name] = value if name not in variables: - print "FAIL: set/get: ", name,": Should exist, not there." + fail("set/get: %s: Should exist, not there." % name) result, = pytave.feval(1, "isequal", value, variables[name]) if not result: - print "FAIL: set/get: ", name," -> ",value," results diverged" + fail("set/get: %s -> %s: results diverged" % (name, value)) except Exception, e: - print "FAIL: set/get: ", name, ":", e + fail("set/get: %s" % name, e) def testexception(exception, func): try: func() - print "FAIL: ", name + fail("Expecting %s but nothing was raised." % repr(exception)) except Exception, e: if not isinstance(e, exception): - print "FAIL:", name, ":", e + fail("Expecting %s but got %s instead" % (repr(exception), repr(e)), e) def testlocalscope(x): @@ -300,6 +300,33 @@ testlocalscope(5) +testexception(KeyError, lambda: pytave.locals["localvariable"]) +pytave.locals["localvariable"] = 1 +if "localvariable" in pytave.globals: + fail("Local variable in globals") +del pytave.locals["localvariable"] +if "localvariable" in pytave.locals: + fail("Could not clear local variable") +testexception(KeyError, lambda: pytave.locals["localvariable"]) +def func(): + del pytave.locals["localvariable"] +testexception(KeyError, lambda: func()) + +testexception(KeyError, lambda: pytave.globals["globalvariable"]) +pytave.globals["globalvariable"] = 1 +if "globalvariable" in pytave.locals: + fail("Global variable in locals") +print pytave.eval(1, "exist globalvariable") +del pytave.globals["globalvariable"] +pytave.eval(0, "clear -global globalvariable") +print pytave.eval(1, "exist globalvariable") +if "globalvariable" in pytave.globals: + fail("Could not clear global variable") +testexception(KeyError, lambda: pytave.globals["globalvariable"]) +def func(): + del pytave.globals["globalvariable"] +testexception(KeyError, lambda: func()) + # Try converting Numeric arrays of objects and characters testexpect(arr1o,arr1o[0].tolist())