f



Nested functions, how do they work (stack related)

I was reading the wiki on 'Call stack' because I wanted to understand 
what a traceback object was. My C/C++ isn't good enough to deal with raw 
python source since I have no background in CS. Also, you just can't 
dive into the python src - it takes a good deal of reading and 
background.. (the types will be confusing for a start)

https://en.wikipedia.org/wiki/Call_stack

'Programming languages that support nested subroutines also have a field 
in the call frame that points to the stack frame of the latest 
activation of the procedure that most closely encapsulates the callee, 
i.e. the immediate scope of the callee. This is called an access link or 
static link (as it keeps track of static nesting during dynamic and 
recursive calls) and provides the routine (as well as any other routines 
it may invoke) access to the local data of its encapsulating routines at 
every nesting level. 

Some architectures, compilers, or optimization cases store one link for 
each enclosing level (not just the immediately enclosing), so that 
deeply nested routines that access shallow data do not have to traverse 
several links; this strategy is often called a "display".'

1. What is the difference between a 'call frame' and a 'stack frame' in 
the above context? I know that a stack frame is all the data related to 
one - CALL foo; 

2. He's saying that within the 'call frame' (whatever that is) there's 
an address to one of the previous stack frames of the wrapper function ? 
What does all that mean in terms of nested functions? Access link? How 
are nested function stacks setup..

3. What exactly is a traceback object - we know that an instance object 
is a dictionary and some glue logic that allows you to pretend that 
methods are stored within the instance and call using x.sin(self) etc.
But I was reading: pydoc traceback AND:
http://effbot.org/librarybook/traceback.htm

'Extract the raw traceback from the current stack frame'
A stack frame contains (from wiki) the parameters, local variables, next 
instruction address so.. what's a raw traceback - does the default 
exception handler realize 'okay error' and then walk the stack and 
extract data and prettify it for display and build a magical traceback 
object? Is this documented for dummies what exactly it does?
(i know that's what it's doing but I HAVE NO CLUE so.. are there books 
on this)

How exactly does an exception fit in with tracebacks? How does all this 
fit in with nested functions? 

4. When you call a nested function (decorator), it generally returns a 
wrapper function but I thought he was just returning a reference to a 
function object but obviously since it can see it's environment, how is 
the stack being setup?


0
Veek
12/13/2016 7:13:31 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

5 Replies
685 Views

Similar Articles

[PageSpeed] 33

Veek M wrote:

> I was reading the wiki on 'Call stack' because I wanted to understand
> what a traceback object was. My C/C++ isn't good enough to deal with
> raw python source since I have no background in CS. Also, you just
> can't dive into the python src - it takes a good deal of reading and
> background.. (the types will be confusing for a start)
> 
> https://en.wikipedia.org/wiki/Call_stack
> 
> 'Programming languages that support nested subroutines also have a
> field in the call frame that points to the stack frame of the latest
> activation of the procedure that most closely encapsulates the callee,
> i.e. the immediate scope of the callee. This is called an access link
> or static link (as it keeps track of static nesting during dynamic and
> recursive calls) and provides the routine (as well as any other
> routines it may invoke) access to the local data of its encapsulating
> routines at every nesting level.
> 
> Some architectures, compilers, or optimization cases store one link
> for each enclosing level (not just the immediately enclosing), so that
> deeply nested routines that access shallow data do not have to
> traverse several links; this strategy is often called a "display".'
> 
> 1. What is the difference between a 'call frame' and a 'stack frame'
> in the above context? I know that a stack frame is all the data
> related to one - CALL foo;
> 
> 2. He's saying that within the 'call frame' (whatever that is) there's
> an address to one of the previous stack frames of the wrapper function
> ? What does all that mean in terms of nested functions? Access link?
> How are nested function stacks setup..
> 
> 3. What exactly is a traceback object - we know that an instance
> object is a dictionary and some glue logic that allows you to pretend
> that methods are stored within the instance and call using x.sin(self)
> etc. But I was reading: pydoc traceback AND:
> http://effbot.org/librarybook/traceback.htm
> 
> 'Extract the raw traceback from the current stack frame'
> A stack frame contains (from wiki) the parameters, local variables,
> next instruction address so.. what's a raw traceback - does the
> default exception handler realize 'okay error' and then walk the stack
> and extract data and prettify it for display and build a magical
> traceback object? Is this documented for dummies what exactly it does?
> (i know that's what it's doing but I HAVE NO CLUE so.. are there books
> on this)
> 
> How exactly does an exception fit in with tracebacks? How does all
> this fit in with nested functions?
> 
> 4. When you call a nested function (decorator), it generally returns a
> wrapper function but I thought he was just returning a reference to a
> function object but obviously since it can see it's environment, how
> is the stack being setup?

found this:
http://www.drdobbs.com/cpp/how-nested-functions-work-part-1/228701476
(still reading it)
0
Veek
12/13/2016 8:47:24 AM
Veek M <vek.m1234@gmail.com>:

> https://en.wikipedia.org/wiki/Call_stack
>
> 'Programming languages that support nested subroutines also have a field 
> in the call frame that points to the stack frame of the latest 
> activation of the procedure that most closely encapsulates the callee, 
> i.e. the immediate scope of the callee. This is called an access link or 
> static link (as it keeps track of static nesting during dynamic and 
> recursive calls) and provides the routine (as well as any other routines 
> it may invoke) access to the local data of its encapsulating routines at 
> every nesting level. 
>
> Some architectures, compilers, or optimization cases store one link for 
> each enclosing level (not just the immediately enclosing), so that 
> deeply nested routines that access shallow data do not have to traverse 
> several links; this strategy is often called a "display".'
>
> 1. What is the difference between a 'call frame' and a 'stack frame' in 
> the above context?

There's no difference.

> 2. He's saying that within the 'call frame' (whatever that is) there's 
> an address to one of the previous stack frames of the wrapper function ? 
> What does all that mean in terms of nested functions? Access link? How 
> are nested function stacks setup..

The classic C stack frame contains two addresses (in addition to the
arguments and local variables):

 * the return address in the calling function

 * the frame pointer in the calling function

Some languages (notably Pascal) add a third address:

 * the frame pointer in the outer function

Often, the outer function is the same as the calling function. However,
if the inner functions call each other, the outer function may be
further up the stack. Since the outer function's local variables are
seen by the inner functions, the extra pointer is needed to access them
directly.

Python has nested functions. Thus, the same technique can be used to
implement Python's internal call stack.

> 3. What exactly is a traceback object

It is an object that reports details of the call stack. It is mostly
useful for troubleshooting.

> How exactly does an exception fit in with tracebacks? How does all this 
> fit in with nested functions?

Well, the traceback object contains also the exception since it is
essential for troubleshooting.

> 4. When you call a nested function (decorator), it generally returns a 
> wrapper function but I thought he was just returning a reference to a 
> function object but obviously since it can see it's environment, how is 
> the stack being setup?

Now I don't exactly understand your question.


Marko
0
Marko
12/13/2016 10:46:51 AM
http://web.archive.org/web/20111030134120/http://www.sidhe.org/~dan/blog/archives/000211.html
(great tail recursion article - best i've seen! SO doesn't really 
explain it unless you already knew it to begin with, but here's the 
link:http://stackoverflow.com/questions/310974/what-is-tail-call-optimization)

I found it useful to read because it deals with the stack. Basically 
when an exception occurs you need to mess with the stack so..
0
Veek
12/13/2016 11:32:05 AM
Op 13-12-16 om 08:13 schreef Veek M:
> 4. When you call a nested function (decorator), it generally returns a 
> wrapper function but I thought he was just returning a reference to a 
> function object but obviously since it can see it's environment, how is 
> the stack being setup?

Here you are no longer just talking about nested functions, you are talking
about closures. A stack is no longer sufficient for implementing closures.
The environment for the nested variables of the closure is often alloceted
on the heap.

-- 
Antoon Pardon.

0
Antoon
12/13/2016 11:44:39 AM
Marko Rauhamaa wrote:

> Veek M <vek.m1234@gmail.com>:
> 
>> https://en.wikipedia.org/wiki/Call_stack
>>
>> 'Programming languages that support nested subroutines also have a
>> field in the call frame that points to the stack frame of the latest
>> activation of the procedure that most closely encapsulates the
>> callee, i.e. the immediate scope of the callee. This is called an
>> access link or static link (as it keeps track of static nesting
>> during dynamic and recursive calls) and provides the routine (as well
>> as any other routines it may invoke) access to the local data of its
>> encapsulating routines at every nesting level.
>>
>> Some architectures, compilers, or optimization cases store one link
>> for each enclosing level (not just the immediately enclosing), so
>> that deeply nested routines that access shallow data do not have to
>> traverse several links; this strategy is often called a "display".'
>>
>> 1. What is the difference between a 'call frame' and a 'stack frame'
>> in the above context?
> 
> There's no difference.
> 
>> 2. He's saying that within the 'call frame' (whatever that is)
>> there's an address to one of the previous stack frames of the wrapper
>> function ? What does all that mean in terms of nested functions?
>> Access link? How are nested function stacks setup..
> 
> The classic C stack frame contains two addresses (in addition to the
> arguments and local variables):
> 
>  * the return address in the calling function
> 
>  * the frame pointer in the calling function
> 
> Some languages (notably Pascal) add a third address:
> 
>  * the frame pointer in the outer function
> 
> Often, the outer function is the same as the calling function.
> However, if the inner functions call each other, the outer function
> may be further up the stack. Since the outer function's local
> variables are seen by the inner functions, the extra pointer is needed
> to access them directly.
> 
> Python has nested functions. Thus, the same technique can be used to
> implement Python's internal call stack.
> 
>> 3. What exactly is a traceback object
> 
> It is an object that reports details of the call stack. It is mostly
> useful for troubleshooting.
> 
>> How exactly does an exception fit in with tracebacks? How does all
>> this fit in with nested functions?
> 
> Well, the traceback object contains also the exception since it is
> essential for troubleshooting.
> 
>> 4. When you call a nested function (decorator), it generally returns
>> a wrapper function but I thought he was just returning a reference to
>> a function object but obviously since it can see it's environment,
>> how is the stack being setup?
> 
> Now I don't exactly understand your question.
> 
> 
> Marko

Umm.. here's an article on windows exception handling.. i was hoping for 
something like that.. (it's very badly written but informative about 
win32 exception handling - i'm still reading it) wanted something 
similar.. I'll quote the highlights.. I KNOW NOTHING about ANY exception 
handling so..

http://www.codeproject.com/KB/cpp/exceptionhandler.aspx

' On the Intel Win32 platform, the FS register always points to the
current TIB. Thus, at FS:[0] you can find a pointer to an 
EXCEPTION_REGISTRATION structure. Now I'm getting somewhere! When an 
exception occurs, the system looks at the TIB of the faulting
thread and retrieves a pointer to an EXCEPTION_REGISTRATION structure. 
In this structure is a pointer to an _except_handler callback function.
The operating system now knows enough to call the _except_handler 
function,'

'When you use a compiler's _try/_except syntax, the compiler also builds 
the EXCEPTION_REGISTRATION struct on the stack. I'm simply showing you a 
simplified version of what a compiler would do if you used 
_try/_except.'




0
Veek
12/13/2016 12:11:53 PM
Reply: