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

Re: C API type issue

En Fri, 03 Oct 2008 12:27:50 -0300, Rich Henry <ag***********@gmail.com>
escribió:
Made a simple little test program as im learning to embed python, have a
simple script that just sets x=10.0 in test.py and prints type(x). Python
prints that x is a float but PyFloat_Check() returns false. If i removed
the
check and just force it to print the double value, its correct. Any ideas
why PyFloat_Check() returns false on a float? Thanks in advance.
You got the reference count wrong. This is by far the most common source
of errors when writting C code for Python (at least for beginners)
#include "Python.h"
#include <cstdio>

int main(int argc, char** argv)
{
FILE* fp = fopen("test.py", "r");
assert(fp);

Py_Initialize();

//
// create a dictionary, load the builtins and execute our file
//
PyObject *d = PyDict_New();
assert(d);
PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
PyRun_File(fp, "test.py", Py_file_input, d, NULL);
Instead of a bare dictionary, I'd use a module. Or the __main__ module.
See how PyRun_SimpleFileExFlags handles this (or just use that function,
or other variant).
//
// get a variable that should be in our global namespace
//
PyObject *x = PyDict_GetItemString(d, "x");
assert(x);
This returns a borrowed reference, not a new one.
Py_DECREF(d);
This releases the only reference to d, and the dictionary is deleted. This
in turn releases the last refrence to x, and it's deleted too.
// determine its type and print it
//
if(PyFloat_Check(x))
{
printf("x = %lf\n", PyFloat_AS_DOUBLE(x));
}
At this stage x is an invalid object.
Py_DECREF(x);
And this is wrong now.
>
Py_Finalize();
fclose(fp);
return 0;
}


--
Gabriel Genellina

Oct 3 '08 #1
0 871

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

Similar topics

9
by: Jason | last post by:
Hi, I am working on a project that creates Banner images programmatically. I have the application working but need to use the Hevetica Neue font but it does not appear in the font list. My...
21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
4
by: RichB | last post by:
Hello, The .NET application I have just installed at a client web site is throwing a strange error. At first the applications works without any problems, but after 10 mins or so Access denied...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
21
by: Lane Straatman | last post by:
#include <stdio.h> #include <stdlib.h> #include <complex.h> int main(void) { _Complex z1, z2, z3; z1 = .4 + .7i; z2 = pow(z1, 2); z3 = z1 * z1; printf("%f, %f \n", z1);
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: George2 | last post by:
Hello everyone, My question is whether my understanding of the solution to type safe issue in CoCreateInstance is correct. There is type safe issue in CoCreateInstance, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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
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...

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.