473,396 Members | 1,749 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,396 software developers and data experts.

Problem with simple C extension

Am I stupid or what?

I want to implement a very simple extension class in C (as I did
it many times before...) The python equivalent of my class whould
look like:

class physics_DPD :
def __init__(self,cutoff,friction,noise,dt) :
self.cutoff = cutoff
self.friction = friction
self.noise = noise
self.dt = dt

That's all fir the start. Simple, heh? My C implementation is
the following:
#include <Python.h>
#include "structmember.h"

// PhysicsDPD instance structure
typedef struct {
PyObject_HEAD
double cutoff;
double friction;
double noise;
double dt;
} hyper_PhysicsDPD;
//----------------------------------------------------------------------
// tp_init
//----------------------------------------------------------------------
static int
hyper_PhysicsDPD_init(hyper_PhysicsDPD *self, PyObject *args, PyObject
*kwds)
{
if (!PyArg_ParseTuple(args,"ffff",
&self->cutoff,
&self->friction,
&self->noise,
&self->dt
))
return -1;

printf("%f %f %f
%f\n",self->cutoff,self->friction,self->noise,self->dt);

return 1;
}

//----------------------------------------------------------------------
// method table
//----------------------------------------------------------------------
static PyMethodDef hyper_PhysicsDPD_methods[] = {
{NULL} /* Sentinel */
};

//----------------------------------------------------------------------
// instance members
//----------------------------------------------------------------------
static PyMemberDef hyper_PhysicsDPD_members[] = {
{"cutoff", T_DOUBLE, offsetof(hyper_PhysicsDPD,cutoff), 0, "cutoff
radius"},
{"friction", T_DOUBLE, offsetof(hyper_PhysicsDPD,friction), 0,
"friction"},
{"noise", T_DOUBLE, offsetof(hyper_PhysicsDPD,noise), 0, "noise"},
{"dt", T_DOUBLE, offsetof(hyper_PhysicsDPD,dt), 0, "time step"},
{NULL} /* Sentinel */
};

//----------------------------------------------------------------------
// type structure
//----------------------------------------------------------------------
static PyTypeObject hyper_PhysicsDPDType = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"simulation.hyper.physics_DPD", /* tp_name */
sizeof(hyper_PhysicsDPD), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE , /* tp_flags */
"DPD physics", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
hyper_PhysicsDPD_methods, /* tp_methods */
hyper_PhysicsDPD_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)hyper_PhysicsDPD_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* freefunc tp_free */
0, /* inquiry tp_is_gc */
0, /* PyObject *tp_bases */
0, /* PyObject *tp_mro */
0, /* PyObject *tp_cache */
0, /* PyObject *tp_subclasses */
0, /* PyObject *tp_weaklist */
0, /* destructor tp_del */
};
//----------------------------------------------------------------------
// module hyper
//----------------------------------------------------------------------

static PyMethodDef hyper_methods[] = {
{NULL} /* Sentinel */
};

#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC inithyper(void)
{
PyObject* m;
m = Py_InitModule3("hyper", hyper_methods,
"Faster C implementation of the underlying physics.");

hyper_PhysicsDPDType.tp_new = PyType_GenericNew;
if (PyType_Ready(&hyper_PhysicsDPDType) < 0) return;

Py_INCREF(&hyper_PhysicsDPDType);
PyModule_AddObject(m, "physics_DPD", (PyObject
*)&hyper_PhysicsDPDType);
}
Now, compilation and import works without problem. But I cannot
assign members in the init-call.
import simulation.hyper.physics_DPD as Physics
p = Physics(1,2,3,4) 0.000000 0.000000 0.000000 0.000000 p.friction

5.3049894774131808e-315

I must be blind today. Can anyone see what I am doing wrong
in init???

Thanks a lot!

- harold -
--
A country without army is like a fish without bicycle.
--

Jul 19 '05 #1
0 1033

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

Similar topics

2
by: Mr Geetar | last post by:
I'm running PHP 4.3.2 with Apache 2.0.46 on Windows XP Home. Intel P4 1.5GHz chip with plenty of memory. I'm trying to enable DOM XML functions in PHP and having a bit of trouble. Per the PHP...
68
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I...
1
by: youngdubliner | last post by:
I'm having a problem ........ I've stripped all my code to help isolate the problem. Its seems to be with importing numarray when python is embedded in C. I have a simple C program it Opens...
3
by: Lee Mundie | last post by:
Hi there, Simple problem here but can't seem to fix it! Okay, I have a select list from which people choose avatars... the list is option values ie. <option>Worm</option> ...
7
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user...
3
by: Dejan | last post by:
I was trying, for the first time, to create a (fairly simple) SOAP extension for some of my web services. They were to be called from the WindowsForms host application. Following error message...
4
by: pepcag | last post by:
I used http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconalteringsoapmessageusingsoapextensions.asp as a template to create a very simple web method with soap...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
0
by: zac# | last post by:
i interesting to try using soap extension, and i using sample on msdn. but when i debug my soap extension, i have error message... i invoke soap extension using client desktop application, i want...
2
by: Frederik Vanderhaegen | last post by:
Hi, I'm writing a simple soap extension for a webservice I developed (without the use of an extension the webservice works perfect). The extension is registered through the web.config files of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.