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

Embeding Py: failed to instantiate a class

Hi !

I need to import a module and create an instance
of a class from that module (in C).

import mod
o = mod.klass()

(mod.klass is a subclass of tuple)

When I receive a class object from the module..

module = PyImport_ImportModule("mod")
cls = PyObject_GetAttrString(module, "klass")

...it fails the PyClass_Check(cls) check.

Some classes pass the check, like:
class test1:
pass
class test3(test1):
pass

but most don't. It is enough to subclass
a buildin type. E.g. this class fails the check:
class test2(object):
pass

Q: Do I need to initialize the class object before
using ? How ?

I have found similar questions in this group, but
no "do-it-this-way" answer. It looks that new-style
classes have a different C API semantic than old ones.
To rephrase my question:

Q: Can you give an example code of creating an instance
of new-style class ?

Thanks,

BranoZ

PS: Below are some example code used to test it.

--- mod.py ---
class test1:
pass

class test2(object):
pass

class test3(test1):
pass

--- main.c ---
#include <Python.h>

static PyObject *module = NULL;

void check(char *name) {
PyObject *cls = NULL;

printf("%s - ", name);
if ((cls=PyObject_GetAttrString(module, name)) == NULL) {
PyErr_Print();
return;
}
PyObject_Print(cls, stdout, 0);
if (!PyClass_Check(cls))
printf(" - not a class!!\n");
else
printf(" - is a class.\n");
}

int main(int argc, char *argv[]) {

Py_Initialize();

if ((module=PyImport_ImportModule("mod")) == NULL) {
PyErr_Print();
return -1;
}
check("test1");
check("test2");
check("test3");

Py_Main(argc, argv);
Py_Finalize();
}

--- output ---
$ ./main
test1 - <class mod.test1 at 0x4032da7c> - is a class.
test2 - <class 'mod.test2'> - not a class!!
test3 - <class mod.test3 at 0x4032db3c> - is a class.
Python 2.3 (#2, Feb 9 2005, 13:41:20)
[GCC 3.3.1 (Mandrake Linux 9.2 3.3.1-2mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import mod
mod.test1 <class mod.test1 at 0x4032da7c> mod.test2 <class 'mod.test2'> mod.test1() <mod.test1 instance at 0x40337cac> mod.test2() <mod.test2 object at 0x40337cec>


Jul 18 '05 #1
0 1328

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

Similar topics

0
by: Brano Zarnovican | last post by:
Hi ! I need to import a module and create an instance of a class from that module (in C). import mod o = mod.klass() (mod.klass is a subclass of tuple)
2
by: Sascha Kerschhofer | last post by:
Hi, I am embeding my SVG Files in HTML in this way: <OBJECT DATA="foo.svg" WIDTH="300" HEIGHT="200" TYPE="image/svg+xml"> (using Acrobat SVG Viewer) Since I create the SVG dynamicaly the WIDTH...
5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
9
by: -Nacho- | last post by:
Hi all I have a table with some email addresses of a mailing list subscribers. I'm triying to send a mail to them by querying the table and sending the mail from a 'while' loop, but I only get...
2
by: Pyenos | last post by:
class model:pass class view: model() class controller: model() I can instantiate clsss model from inside class view but I can't instantiate class model from inside controller, due to the...
5
by: Pyenos | last post by:
class One: Two() #can't instantiate class Two: Three() #can't instantiate class Three:pass
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
1
by: tokcy | last post by:
Hi, I am embeding flash player in my site but i have some problem like... suppose i have 5 songs and i am playing one song but i want to play another while first one is not completed then both...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
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
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...

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.