Accessing Python variables in an extension module

M

MD

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
 
A

Alex Martelli

MD said:
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
 
M

MD

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
 
D

Diez B. Roggisch

MD said:
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
 
A

Alex Martelli

MD said:
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
 
M

MD

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

Thanks,
-Manas
 
H

Hrvoje Niksic

MD said:
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.
 
A

Alex Martelli

MD said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top