By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,482 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,482 IT Pros & Developers. It's quick & easy.

string to type object (C)

P: n/a
Hello,

I'm looking for a one-to-one function from strings to the built-in
data types in C. I will be keeping the string in a file. I need the
PyTypeObject* back from it. If nothing else I'll just do a bunch of
strcmp( "tuple" ) { return &PyTuple_Type; } commands, provided
&PyTuple_Type; will be linking the right address.

The project is for a hobby, no money or official credentials on the
line. Any help is appreciated.
Aug 5 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a


castironpi wrote:
Hello,

I'm looking for a one-to-one function from strings to the built-in
data types in C. I will be keeping the string in a file. I need the
PyTypeObject* back from it.
Use a dict mapping py strings to the object pointers (cast to py ints).

Aug 5 '08 #2

P: n/a
On Tue, Aug 5, 2008 at 2:30 AM, castironpi <ca********@gmail.comwrote:
I'm looking for a one-to-one function from strings to the built-in
data types in C. I will be keeping the string in a file. I need the
PyTypeObject* back from it. If nothing else I'll just do a bunch of
strcmp( "tuple" ) { return &PyTuple_Type; } commands, provided
&PyTuple_Type; will be linking the right address.
Something along the lines of this?
b = PyImport_Import("__builtin__");
return PyObject_GetAttrString(b, typestring);

-Miles
Aug 6 '08 #3

P: n/a
On Aug 5, 7:59*pm, Miles <semantic...@gmail.comwrote:
On Tue, Aug 5, 2008 at 2:30 AM, castironpi <castiro...@gmail.comwrote:
I'm looking for a one-to-one function from strings to the built-in
data types in C. *I will be keeping the string in a file. *I need the
PyTypeObject* back from it. *If nothing else I'll just do a bunch of
strcmp( "tuple" ) { return &PyTuple_Type; } commands, provided
&PyTuple_Type; will be linking the right address.

Something along the lines of this?
b = PyImport_Import("__builtin__");
return PyObject_GetAttrString(b, typestring);

-Miles
Miles,

That's what I was looking for. I wanted to avoid the heavy Import but
I thought I remembered that imports are free after the first time.

Import needed a PyString.

PyObject* s= PyString_FromString( "__builtin__" );
PyObject* b= PyImport_Import(s);
return PyObject_GetAttrString(b, "str");

I expect I need a Py_DECREF on s and b.

Do you know if uncooperative (not necc'ly malicious) code could
interfere:
>>__builtin__.int= None
__builtin__.int
__builtin__.int= type( 0 )
__builtin__.int
<type 'int'>

? Or would PyImport_Import(s) create a namespace that had the int
type in it from scratch?
Aug 6 '08 #4

P: n/a
En Wed, 06 Aug 2008 01:55:10 -0300, castironpi <ca********@gmail.com>
escribi�:
Do you know if uncooperative (not necc'ly malicious) code could
interfere:
>>>__builtin__.int= None
__builtin__.int
__builtin__.int= type( 0 )
__builtin__.int
<type 'int'>

? Or would PyImport_Import(s) create a namespace that had the int
type in it from scratch?
Once you modify __builtin__, any later access to builtin names will see
the changed values.
I don't know of any way to "reset" the builtin module - other than calling
again _PyBuiltin_Init from C code.

--
Gabriel Genellina

Aug 12 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.