implement direct variable queries & local scopes

Bug #376343 reported by highegg
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Pytave
Fix Committed
Undecided
highegg

Bug Description

an enhancement towards more convenient working with variables in Octave's current scope

summary of changes:
setvar, getvar and isvar are implemented to set, get and test existence of variables.

this allows doing things like

pytave.setvar("x", 1)
pytave.eval(0,"x += 1")
x = pytave.getvar("x")

push_scope and pop_scope are provided to create an anonymous scope and push it on Octave's call stack,
to prevent cluttering other variables if nested calls to pytave are in effect.

pytave.push_scope()
pytave.setvar("x", something)
pytave.eval(0, "... do something with x)
pytave.pop_scope() # wipes out x and restores its previous value, if any

of course, for proper safety, try/finally block should be used to ensure the cleanup. for convenience, a local_scope decorator is provided that encloses a whole function in push_scope/try/finally/pop_scope sequence:

@pytave.local_scope
def my_oct_add(x,y):
    pytave.setvar("x",x)
    pytave.setvar("y",y)
    result, = pytave.eval(1, "x + y")
    return result

this function, when called, will not affect the top-level values of x and y, if any.

patch attached

Revision history for this message
highegg (highegg) wrote :
Changed in pytave:
assignee: nobody → highegg (highegg)
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.