473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

supplying constants in an extension module

Hi,

i write an extension module in C at the moment.

I want to define some constants (integer mainly,
but maybe also some strings).

How do i do that best within this extension module
in C? Do i supply them as RO attributes?

What's the best way for it?
Thanks for hints,
Torsten.

Jul 18 '05 #1
1 1411
Torsten Mohr wrote:
i write an extension module in C at the moment.

I want to define some constants (integer mainly,
but maybe also some strings).

How do i do that best within this extension module
in C? Do i supply them as RO attributes?

What's the best way for it?


reading the source for existing modules will teach you many useful
idioms. here's how this is currently done:

PyMODINIT_FUNC
initmymodule(void)
{
PyObject *m;

m = Py_InitModule(...);

PyModule_AddIntConstant(m, "int", value);
PyModule_AddStringConstant(m, "string", "string value");
}

(both functions set the exception state and return -1 if they fail, but you
can usually ignore this; the importing code will check the state on return
from the init function)

if you want to support older versions of Python, you need to add stuff to
the module dictionary yourself. an example:

#if PY_VERSION_HEX < 0x02030000
DL_EXPORT(void)
#else
PyMODINIT_FUNC
#endif
initmymodule(void)
{
PyObject* m;
PyObject* d;
PyObject* x;

m = Py_InitModule(...);
d = PyModule_GetDict(m);

x = PyInt_FromLong(value);
if (x) {
PyDict_SetItemString(d, "INT", x);
Py_DECREF(x);
}

x = PyString_FromString("string value");
if (x) {
PyDict_SetItemString(d, "STRING", x);
Py_DECREF(x);
}
}

</F>

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
8
by: Torsten Mohr | last post by:
Hi, i write an extension module in C at the moment. This module does some work on some own data types that consist of some values. The functions that can change the data are written in C. ...
0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
4
by: Tomasz Lisowski | last post by:
Hi, We are distributing our Python application as the short main script (.py file) and a set of modules compiled to the .pyc files. So far, we have always treated .pyc files as portable between...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
11
by: Bill Nguyen | last post by:
I need to make a set of constants to be available for the whole application. Public const is confined to the form from which constants are declared. Is there a way to declare once and all forms can...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
1
by: Nathan Harmston | last post by:
Hi All, I ve got a single module which I m using to contain a lot of dictionaries, constants, general information, which are used by various other modules. However I can't seem to access them: ...
1
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.