Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Accessing Python variables in an extension module

Reply
Thread Tools

Accessing Python variables in an extension module

 
 
MD
Guest
Posts: n/a
 
      07-16-2007
Hi,

I would like to access "variables" defined in my Python program in
a C module extension for Python. Is this possible? I looked at the
Python C API reference but didn't find anything there that could help
me.
Thanks in advance for any help/tips.

Regards,
-MD

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      07-16-2007
MD <(E-Mail Removed)> wrote:

> Hi,
>
> I would like to access "variables" defined in my Python program in
> a C module extension for Python. Is this possible? I looked at the
> Python C API reference but didn't find anything there that could help
> me.


If they're global variables of a certain module XYZ, your C code may
"import" XZY (or look it up in the C API equivalent of the sys.modules
dict) and get those variables as attributes of object XYZ. Is that what
you mean by ``variables defined in your Python program''...?


Alex
 
Reply With Quote
 
 
 
 
MD
Guest
Posts: n/a
 
      07-16-2007
Hi Alex,
Thanks for your reply. It was exactly what I was looking for. Two
additional questions
1) Is there anyway to find out which modules a variable belongs to
when I have only its name (and its not qualified with the complete
name like module.varname)
2) Is there anyway to find the type of the object in C using something
like a switch statement? I was looking for something like this
switch type(object) {
STRING: "This is a string object";
break;
INTEGER: "This is an integer object";
break;
BOOLEAN: "This is a boolean object";
.........
.........
}
I don't want to run all the C Py***_Check functions on the object.
Something like the switch statement above will lead to nice and clean
code.
Thanks again for your reply.

Regards,
-Manas

On Jul 15, 11:02 pm, (E-Mail Removed) (Alex Martelli) wrote:
> MD <(E-Mail Removed)> wrote:
> > Hi,

>
> > I would like to access "variables" defined in my Python program in
> > a C module extension for Python. Is this possible? I looked at the
> > Python C API reference but didn't find anything there that could help
> > me.

>
> If they're global variables of a certain module XYZ, your C code may
> "import" XZY (or look it up in the C API equivalent of the sys.modules
> dict) and get those variables as attributes of object XYZ. Is that what
> you mean by ``variables defined in your Python program''...?
>
> Alex



 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      07-16-2007
MD wrote:

> Hi Alex,
> Thanks for your reply. It was exactly what I was looking for. Two
> additional questions
> 1) Is there anyway to find out which modules a variable belongs to
> when I have only its name (and its not qualified with the complete
> name like module.varname)


No.

> 2) Is there anyway to find the type of the object in C using something
> like a switch statement? I was looking for something like this
> switch type(object) {
> STRING: "This is a string object";
> break;
> INTEGER: "This is an integer object";
> break;
> BOOLEAN: "This is a boolean object";
> .........
> .........
> }
> I don't want to run all the C Py***_Check functions on the object.
> Something like the switch statement above will lead to nice and clean
> code.


In python,

obj.__class__.__name__

should work. I don't know what to write in C to accomplish that, but it
can't be too hard. Factor it away in a function.

Diez
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      07-16-2007
MD <(E-Mail Removed)> wrote:

> Hi Alex,
> Thanks for your reply. It was exactly what I was looking for. Two
> additional questions
> 1) Is there anyway to find out which modules a variable belongs to
> when I have only its name (and its not qualified with the complete
> name like module.varname)


Loop through all modules in sys.modules and you will find a set (which
may be empty) of modules which happen to have that name as an attribute.
It's a very peculiar thing to do (not clear what you expect to do based
on that information) but not difficult.

> 2) Is there anyway to find the type of the object in C using something
> like a switch statement? I was looking for something like this
> switch type(object) {
> STRING: "This is a string object";
> break;
> INTEGER: "This is an integer object";
> break;
> BOOLEAN: "This is a boolean object";
> .........
> .........
> }
> I don't want to run all the C Py***_Check functions on the object.
> Something like the switch statement above will lead to nice and clean
> code.


Each Python object, in C, carries a pointer to its type object. Of
course there are unbounded numbers of types, but at least you can get
such information as the type's name and the module if any in which it
may have been defined, essentially like you'd do with type(somobj) if
you were working directly in Python.


Alex
 
Reply With Quote
 
MD
Guest
Posts: n/a
 
      07-16-2007
Hi Alex,
Thanks for the answer. Are there any C defines (for e.g. STRING,
BOOLEAN) corresponding to each Python type?

Thanks,
-Manas

On Jul 16, 9:53 am, (E-Mail Removed) (Alex Martelli) wrote:
> MD <(E-Mail Removed)> wrote:
> > Hi Alex,
> > Thanks for your reply. It was exactly what I was looking for. Two
> > additional questions
> > 1) Is there anyway to find out which modules a variable belongs to
> > when I have only its name (and its not qualified with the complete
> > name like module.varname)

>
> Loop through all modules in sys.modules and you will find a set (which
> may be empty) of modules which happen to have that name as an attribute.
> It's a very peculiar thing to do (not clear what you expect to do based
> on that information) but not difficult.
>
> > 2) Is there anyway to find the type of the object in C using something
> > like a switch statement? I was looking for something like this
> > switch type(object) {
> > STRING: "This is a string object";
> > break;
> > INTEGER: "This is an integer object";
> > break;
> > BOOLEAN: "This is a boolean object";
> > .........
> > .........
> > }
> > I don't want to run all the C Py***_Check functions on the object.
> > Something like the switch statement above will lead to nice and clean
> > code.

>
> Each Python object, in C, carries a pointer to its type object. Of
> course there are unbounded numbers of types, but at least you can get
> such information as the type's name and the module if any in which it
> may have been defined, essentially like you'd do with type(somobj) if
> you were working directly in Python.
>
> Alex



 
Reply With Quote
 
Hrvoje Niksic
Guest
Posts: n/a
 
      07-16-2007
MD <(E-Mail Removed)> writes:

> 2) Is there anyway to find the type of the object in C using something
> like a switch statement? I was looking for something like this
> switch type(object) {
> STRING: "This is a string object";
> break;
> INTEGER: "This is an integer object";
> break;
> BOOLEAN: "This is a boolean object";
> .........
> .........
> }


Not switch, but the closest you'll get is:

if (object->ob_type == PyString_Type) {
... string
}
else if (object->ob_type == PyInt_Type) {
... int
}
else if (object->ob_type == PyBool_Type) {
... bool
}

> I don't want to run all the C Py***_Check functions on the object.


Py*_Check are not expensive if the object really is of the target
type. They are necessary to support subtyping correctly.
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      07-17-2007
MD <(E-Mail Removed)> wrote:

> Hi Alex,
> Thanks for the answer. Are there any C defines (for e.g. STRING,
> BOOLEAN) corresponding to each Python type?


No, I know of no such "defines" -- what good would they do?


Alex
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using exceptions in defined in an extension module inside anotherextension module Floris Bruynooghe Python 1 12-24-2008 10:33 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Accessing Module variables from another Module cjt22@bath.ac.uk Python 3 09-05-2007 01:30 PM
Accessing class member variables - properties or variables? dwok Java 7 03-04-2005 03:54 AM
Getting module object by name in C extension module Ilariu Raducan Python 2 07-14-2004 10:56 AM



Advertisments