Yu**********@gm ail.com wrote:
Hi,
I have written a small prototype Python extension for a C-library.
I have the methods all sorted out and it is working fine.
In the C-library, they are various constants of types like string,
integer, float and matrix. I'd like to expose them as READONLY values.
Is the use of PyMemberDefs a suitable way to provide such access?
I'd like the user of the extension to be able to do something like
the follow
import MyExtension
MyExtension.beg in()
mv = MyExtension.min Value
MyExtension.pro cess(MyExtensio n.versionStr)
MyExtension.end ()
Is someone able to point me to an example code of how I can write my
extension so that the references to those constants can be used in the
above way.
Regards
A couple of years ago I wanted to do something similar for a project I
was working on.
Below is a snippet of some sample C code from the module's
initialization function which should give you an idea of the approach
used. It's a limited example and only creates module identifiers for
simple integer values, not the more complex types you are interested
in, nor does it do anything to make them read only, but it might help
get you started.
If you find something that addresses these deficiencies, please post
your findings.
Best,
-Martin
// snippet
#include "Python.h"
enum METAtags
{
kMETA_MojikumiX 4051 = 0,
kMETA_UNIUnifie dBaseChars = 1,
kMETA_BaseFontN ame = 2
}
void initPyExtension ()
{
PyObject *m, *d, *o;
m = Py_InitModule4( "PyExtensio n", pyis_methods,
PySING_module_d ocumentation,
(PyObject*)NULL , PYTHON_API_VERS ION);
// Add some symbolic constants to the module
d = PyModule_GetDic t(m); // get modules dictionary
PyObject* o = PyInt_FromLong( kMETA_MojikumiX 4051);
PyDict_SetItemS tring(d, "idMojikumiX405 1", o);
Py_INCREF(o); // for dictionary ref
PyObject* o = PyInt_FromLong( kMETA_UNIUnifie dBaseChars);
PyDict_SetItemS tring(d, "idUNIUnifiedBa seChars", obj);
Py_INCREF(o);
PyObject* o = PyInt_FromLong( kMETA_BaseFontN ame);
PyDict_SetItemS tring(d, "idBaseFontName ", obj);
Py_INCREF(o);