Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C-API: Get scope in function

0 views
Skip to first unread message

moerchendiser2k3

unread,
Jan 17, 2010, 8:23:03 AM1/17/10
to
Hi,

I have a small problem how to get the scope from a C-API function.
Check out this code snippet:

[code]
variable = 3

def test():
print variable #output: 3
print globals() # ... 'variable': 3, ...

test()
[/code


In my case I know there is a variable in the scope where this function
is called from, but I dont know how to get it.
Check out:

[code]
PyObject *MyFunction_Test(PyObject* self, PyObject *args, PyObject
*keywords)
{
... ?
}
[/code]

Is it possible to get the dictionary of the scope where this function
is called from?


Thanks a lot!!

Bye, googler

Gabriel Genellina

unread,
Jan 17, 2010, 4:16:55 PM1/17/10
to pytho...@python.org
En Sun, 17 Jan 2010 10:23:03 -0300, moerchendiser2k3
<googler.1...@spamgourmet.com> escribiᅵ:

> I have a small problem how to get the scope from a C-API function.
> Check out this code snippet:
>
> [code]
> variable = 3
>
> def test():
> print variable #output: 3
> print globals() # ... 'variable': 3, ...
>
> test()
> [/code
>
>
> In my case I know there is a variable in the scope where this function
> is called from, but I dont know how to get it.

Note that in this case the relevant scope is the one where the Python
function was *defined*, not where it is *called*.

> Check out:
>
> [code]
> PyObject *MyFunction_Test(PyObject* self, PyObject *args, PyObject
> *keywords)
> {
> ... ?
> }
> [/code]
>
> Is it possible to get the dictionary of the scope where this function
> is called from?

Yes, you can obtain the global variables of the current execution frame
using PyEval_GetGlobals()

--
Gabriel Genellina

moerchendiser2k3

unread,
Jan 17, 2010, 7:49:25 PM1/17/10
to
Perfect, this helped me a lot, thx! :)
0 new messages