Calling __builtins__ from C API

Adrian Eyre a.eyre at optichrome.com
Tue Apr 6 09:04:38 EDT 1999


> However, I can't figure out the proper way to refer to this 
> instance from the C API.  While I can use the PyRun_SimpleString
> function to refer the instance, this seems like a hack.

Try:

#include <Python.h>
void main()
{
	Py_Initialize();
	PyObject* builtinsMod = PyImport_ImportModule("__builtin__");
	PyObject* builtinsDict = PyModule_GetDict(builtinsMod);
	PyObject* someString = PyString_FromString("someValue");
	PyDict_SetItemString(builtinsDict, "someName", someString);
	Py_DECREF(builtinsMod);
	Py_DECREF(builtinsDict);
	Py_DECREF(someString);
	Py_Finalize();
}



> What are the disadvantages of using this over the more cumbersome,
> PyObject_GetAttrString(), Py_BuildValue(), PyEval_CallObject(), etc.?
> How do you refer to a built-in function or object?

AFAIK, the only difference is that with PyRun_String(), it has to run
throught the parser first.

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com 
--------------------------------------------





More information about the Python-list mailing list