473,604 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's wrong?

Hello everyone!

I wanted to write python script with defined function. From that script
I wanted to call C module, which in it's turn call defined python
function. Sounds simple. But I've got some troubles in realization. I'm
beginner at python so it's normal, I think. :)

Here's the code. I'll apreciate if anyone can answer me what's wrong
here.

-------------------------------------------------------------------
power.c:

#include "Python.h"

static PyObject *
power(PyObject *self, PyObject *args)
{
PyObject *result, *temp;

int num;

if (PyArg_ParseTup le(args, "iO", &num, &temp))
{
if (!PyCallable_Ch ack(temp)) //here's error, but I don't know
why...
{
return NULL;
}
}

result = Py_BuildValue(" i", num*num);

PyEval_CallObje ct(temp, result);
Py_DECREF(resul t);

Py_INCREF(Py_No ne);
retrun Py_None;
}

static struct PyMethodDef power_methods[] = {
{"power", power, METH_VARARGS, "powering number"},
{NULL, NULL, 0, NULL}
};

void initpower()
{
(void) Py_InitModule(" power", power_methods);
}
-------------------------------------------------------------------

-------------------------------------------------------------------
power.py:

import power

def sst(some):
print some

power.power(9, sst)
-------------------------------------------------------------------

Jan 25 '06 #1
7 1605
kishkin wrote:
Hello everyone!

I wanted to write python script with defined function. From that script
I wanted to call C module, which in it's turn call defined python
function. Sounds simple. But I've got some troubles in realization. I'm
beginner at python so it's normal, I think. :)

Here's the code. I'll apreciate if anyone can answer me what's wrong
here.
[snip]
if (!PyCallable_Ch ack(temp)) //here's error,

but > I don't know why...

Er, do we have to guess what error you are getting?

No, no, I love to guess... should the function be spelt
PyCallable_Choc k with an o instead of an a?

--
Steven.

Jan 25 '06 #2
"kishkin" <ki*****@gmail. com> wrote:
I wanted to write python script with defined function. From that script
I wanted to call C module, which in it's turn call defined python
function. Sounds simple. But I've got some troubles in realization. I'm
beginner at python so it's normal, I think. :)

Here's the code. I'll apreciate if anyone can answer me what's wrong
here.
instead of letting everyone guess, maybe you could explain *why* you
think something's wrong with your code ?

(I suspect that if you try to write down what happens when you try
to compile and run this code, you'll figure out how to fix it all by your-
self)

</F>
-------------------------------------------------------------------
power.c:

#include "Python.h"

static PyObject *
power(PyObject *self, PyObject *args)
{
PyObject *result, *temp;

int num;

if (PyArg_ParseTup le(args, "iO", &num, &temp))
{
if (!PyCallable_Ch ack(temp)) //here's error, but I don't know
why...
{
return NULL;
}
}


</F>

Jan 25 '06 #3
:)) Oа course it PyCallable_Chec k there!!! :)) And error is that
PyCallable_Chec k returned 0.

power.c compiles without errors.

error is here:
-----------------------------------
[kishkin@linuxa-miho 1]$ make -f makefile.power
gcc power.c -g -I/usr/local/include/python2.4 -fpic -shared -o power.so
[kishkin@linuxa-miho 1]$ python power.py
Exception exceptions.Type Error: 'argument list must be a tuple' in
'garbage collection' ignored
Fatal Python error: enexpected exception during garbage collection
Aborted
-----------------------------------

Jan 25 '06 #4
Thank you for fast response! And I'm sorry for bad style of asking
questions!

Jan 25 '06 #5
"kishkin" wrote:
Thank you for fast response! And I'm sorry for bad style of asking
questions!


did you (with the help of your compiler) figure out what was wrong?

</F>

Jan 25 '06 #6
"kishkin" wrote:
:)) O? course it PyCallable_Chec k there!!! :)) And error is that
PyCallable_Chec k returned 0.

power.c compiles without errors.

error is here:
-----------------------------------
[kishkin@linuxa-miho 1]$ make -f makefile.power
gcc power.c -g -I/usr/local/include/python2.4 -fpic -shared -o power.so
[kishkin@linuxa-miho 1]$ python power.py
Exception exceptions.Type Error: 'argument list must be a tuple' in
'garbage collection' ignored
Fatal Python error: enexpected exception during garbage collection
enexpected? are you typing things in by hand, or does your computer
have a bad RAM chip ?
Aborted
-----------------------------------


try changing

result = Py_BuildValue(" i", num*num);
PyEval_CallObje ct(temp, result);
Py_DECREF(resul t);

to

result = Py_BuildValue(" (i)", num*num);
PyEval_CallObje ct(temp, result);
Py_DECREF(resul t);

or, more convenient

result = PyObject_CallFu nction(temp, "i", num*num);
if (!result)
return NULL;

(note that you don't really need to check for callables; the CallFunction
API will do that for you, and generate a proper exception if you pass in
the wrong thing).

</F>

Jan 25 '06 #7
Man! It's alive! :)

Now next question: where are you living? I must know that for buy you
some beer!!

Thanks a lot!!!

Jan 25 '06 #8

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

Similar topics

3
2368
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't help but say i was totally impressed. I needed an open source wikiblog/wikilog, whatever you wanna call it, basically a hybrid of a blog and a wiki. I checked out snipsnap, which uses java, it was said on their site to be a clone of vanilla, a...
5
1837
by: tin | last post by:
<script language="Javascript"> <!-- function apri (theURL,winName,features){ window.open (theURL,winName,features); var a=null; oldwindow = window.self; oldwindow.opener = window.self; oldwindow.close(); } -->
10
2053
by: G Matthew J | last post by:
interesting "signal vs. noise" blog entry: http://37signals.com/svn/archives2/whats_wrong_with_ajax.php
51
13339
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
8
279
by: DJ | last post by:
What is wrong? #include <stdio.h> #define N 8 void order(int *p, int *q); int main(void)
8
1833
by: Brian Basquille | last post by:
Hello all, Bit of a change of pace now. As opposed to the typical questions regarding my Air Hockey game, am also working on a Photo Album which uses an Access Database to store information about photos. This information is held inside the database (photoDB.mdb) in a table called 'photos' - information being recorded in there are photo information (photoID, location, phDate, category, caption). I have inputted my own photos (not via...
10
3214
by: Protoman | last post by:
Could you tell me what's wrong with this program, it doesn't compile: #include <iostream> #include <cstdlib> using namespace std; class Everything { public: static Everything* Instance()
9
2114
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
8
1339
by: adamisko | last post by:
Hello, what is wrong with this code, I want to divide string to pieces 4 chars length. string tekst = "divide string to 4 chars pieces"; Regex regex = new Regex("(.{1,4})"); string substrings = regex.Split(tekst); but instead of array with: "divi","de s","trin" etc.. i have
2
2228
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2; i++){ for (k = 0; k < point3; k++){
0
7997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7929
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8409
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8065
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3907
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.