1

I'm trying to extend Python 2.7.x with a "C" code.

I know I can create a module using

Py_InitModule(char *name, PyMethodDef *methods)

This works fine to add methods to a module.

I want to add a string to the module I created using Py_InitModule(). How can I add a member variable, such as name = "my_module", or idx=5?

Thank you!

1 Answer 1

2

There's an overview, and examples, in the Extending Python with C or C++ chapter of the extending and embedding docs. Which you really need to read if you want to write extension modules by hand. Reference details are mostly in the Module Objects chapter of the C-API docs.

While module objects are perfectly normal objects, and you can always access their dict with PyModule_GetDict, or just PyObject_SetAttr them, there are special functions to make this easier. The most common way to do it is to construct the object in the module init function, the same way you'd construct any Python object anywhere, then call PyModule_AddObject:

Add an object to module as name. This is a convenience function which can be used from the module’s initialization function. This steals a reference to value. Return -1 on error, 0 on success.

One major variation on this is storing the object in a static and over-refcounting it so it can never be deleted. See the SpamError example in the Extending chapter.

But for your specific example, you're just trying to add a string constant, which is much easier, because there are even-specialer functions for adding native-typed constants:

PyMODINIT_FUNC
initspam(void) {
    PyObject *mod = Py_InitModule("spam", SpamMethods);
    if (!mod) return;
    if (PyModule_AddStringConstant(mod, "mystring", "This is my string") == -1) return;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, abamert! It worked. I'm aware of the overview you mentioned. There are many examples showing how to implement methods in C as that is what most people want. I failed to find any good examples showing how to add module attributes in C. I was looking at wrong chapters in reference. Now I know where to look. Hope this helps others like me in the future. Thank you!
@user2756376 The way the extending and embedding docs are organized, you pretty much have to read through the whole thing once, at least to learn where everything is for later, even though half of it is irrelevant to you.
@user2756376 Meanwhile, unless you just love C, it's probably worth learning, and switching to, one of the higher-level wrappers for C++ (PyCxx or Boost), Rust (rust-python), D (PyD), etc. They're a lot easier to use, letting you declare stuff in the source language instead of a bunch of macro-filled structs, automating the ref counting, taking care of argument conversions, etc. You need to know the raw C API at least for debugging purposes, but once you've learned it, you're usually better off not using it. Also, if you've never used Cython, definitely give that a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.