changeset 59:f379cb14c4d4 pytave-new

implement deleting variables
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 08 Jun 2009 15:35:08 +0200
parents 9279ecdea19e
children bb7c52547fcf 46e6fa692e0c 37fd244f015e f7c3eaab5bd6
files ChangeLog package/pytave.py pytave.cc
diffstat 3 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Jun 08 13:55:29 2009 +0200
+++ b/ChangeLog	Mon Jun 08 15:35:08 2009 +0200
@@ -1,3 +1,8 @@
+2009-06-03  Jaroslav Hajek  <highegg@gmail.com>
+
+	* pytave.cc (delvar): New function.
+	* package/pytave.py (_VariablesDict.__delitem__): New method.
+
 2009-06-03  Jaroslav Hajek  <highegg@gmail.com>
 
 	* pytave.cc (isvar): Fix tests.
--- a/package/pytave.py	Mon Jun 08 13:55:29 2009 +0200
+++ b/package/pytave.py	Mon Jun 08 15:35:08 2009 +0200
@@ -180,6 +180,18 @@
 			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)))
+		# Octave does not gripe when clearing non-existent
+		# variables. To be consistent with Python dict
+		# behavior, we shall do so.
+		if self.__contains__(name):
+			_pytave.delvar(name, self.global_variables)
+		else:
+			raise KeyError('No Octave variable named ' + name)
+
+
 locals = _VariablesDict(global_variables=False)
 globals = _VariablesDict(global_variables=True)
 
--- a/pytave.cc	Mon Jun 08 13:55:29 2009 +0200
+++ b/pytave.cc	Mon Jun 08 15:35:08 2009 +0200
@@ -260,6 +260,19 @@
       return retval;
    }
 
+   void delvar(const string& name, bool global) {
+
+      if (global) {
+
+         // FIXME: workaround a bug in Octave 3.2.0.
+         if (! symbol_table::is_global (name))
+            symbol_table::insert (name).mark_global ();
+
+         symbol_table::clear_global (name);
+      } else
+         symbol_table::clear_variable (name);
+   }
+
    int push_scope() {
       symbol_table::scope_id local_scope = symbol_table::alloc_scope();
       symbol_table::set_scope(local_scope);
@@ -287,6 +300,7 @@
    def("getvar", pytave::getvar);
    def("setvar", pytave::setvar);
    def("isvar", pytave::isvar);
+   def("delvar", pytave::delvar);
    def("push_scope", pytave::push_scope);
    def("pop_scope", pytave::pop_scope);
    def("get_exceptions", pytave::get_exceptions);