Re: Implementing class methods in C

2005-08-19 Thread Adriaan Renting
I think you'd need to write a C++ class that has the methods you want to 
implement in C++,
then wrap that with SWIG, then inherit from that, though multiple inheritance 
if you also need functions from a base Python class.
The PyQt people use SIP I think, instead of SWIG, might be useful to look into 
that too too.

Adriaan.
 
 
[EMAIL PROTECTED] 08/18/05 6:42 pm  
 
Nope, it still doesn't work. Anyway, that's not exactly what i want, since 
i want func2 to be accessible from all instances of Test() 
 
Naveen 
 
On Thu, 18 Aug 2005, Jeremy Moles wrote: 
 
I honestly don't know the answer to this and I am entirely guessing 
but--does it work without using the new module? That is: 
 
 
 
import _test 
 
class Foo: 
pass 
 
foo = Foo() 
 
foo.bar = _test.func2 
 
foo.bar() 
 
On Thu, 2005-08-18 at 12:09 -0400, [EMAIL PROTECTED] wrote: 
I am having a problem implementing some methods of a python class in C. 
The class is defined in python, but I would like to rewrite some methods 
in c. Here is an example of what I want to do: 
 
file _test.c: 
 
#include Python.h 
 
static PyObject 
func2(PyObject *self, PyObject *args) 
{ 
  if (self == NULL) { 
PyErr_SetString(PyExc_SystemError, self is NULL); 
return NULL; 
  } 
 
  // Parse arguments 
  if (!PyArg_ParseTuple(args, )) 
  { 
return NULL; 
  } 
 
  Py_INCREF(Py_None); 
  return Py_None; 
} 
 
static PyMethodDef TestMethods[] = { 
  {func2, func2, METH_VARARGS, func2.}, 
  {NULL, NULL, 0, NULL} /* Sentinel */ 
}; 
 
PyMODINIT_FUNC 
init_test(void) 
{ 
  (void) Py_InitModule(_test, TestMethods); 
} 
 
 
test.py: 
 
class Test: 
  def func1(self): 
print I am in func 1 
 
import _test 
import new 
Test.func2 = new.instancemethod(_test.func2, None, Test) 
del(new) 
 
t = Test() 
t.func2() 
 
 
When I run test.py, I get a SystemError exception (which is what I raise 
if self is NULL). I think my confusion lies in the use of PyObject* self 
in the function declaration. Shouldn't this be set to point to the 
instance of class Test that I am calling it from? Am I misunderstanding 
the purpose of PyObject* self? Thanks. 
 
Naveen 
 
- 
Naveen Michaud-Agrawal 
Program in Molecular Biophysics 
Johns Hopkins University 
(410) 614 4435 
 
-- 
http://mail.python.org/mailman/listinfo/python-list 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing class methods in C

2005-08-19 Thread BranoZ
[EMAIL PROTECTED] wrote:
 Am I misunderstanding the purpose of PyObject* self?

No. I think you do everything right, but it still doesn't work.

I have tried to implement it in Python:

_test.py:

def func2(self, *args):
  print type(self)
  print args

then all of this works:


import _test

class Test1:
  func2 = _test.func2

class Test2:
  pass

Test2.func2 = _test.func2

class Test3:
  pass

import new
Test3.func2 = new.instancemethod(_test.func2, None, Test3)
del new

Test1().func2(1, 2, 3)
Test2().func2(1, 2, 3)
Test3().func2(1, 2, 3)

If you implement _test in C, works none of the above.
The only difference I can see is that:

type(_test.func2)
type 'function'
is for Python implemented function and

type(_test.func2)
type 'builtin_function_or_method'
for C implementation

I would really like to know the answer too.
How do you implement some methods in C without subclassing ?

BranoZ

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing class methods in C

2005-08-19 Thread nmichaud

 If you implement _test in C, works none of the above.
 The only difference I can see is that:

 type(_test.func2)
 type 'function'
 is for Python implemented function and

 type(_test.func2)
 type 'builtin_function_or_method'
 for C implementation

 I would really like to know the answer too.
 How do you implement some methods in C without subclassing ?


But the strange thing is if I use new.instancemethod the c function
becomes bound (using my previous code for _test.c)

import _test

class Test:
def func1(self):
print In class +repr(self.__class__.__namd__)

import new
Test.func2 = new.instancemethod(_test.func2, None, Test)
del new

t = Test
type(_test.func2)   # returns type 'builtin_function_or_method'
type(T.func1)   # returns unbound method Test.func
type(t.func1)   # returns bound method Test.func of __main__.Test 
instance at 0x4eb4b8
type(T.func2)   # returns built-in function func2
type(t.func2)   # returns bound method Test.func2 of __main__.Test 
instance at 0x4eb4b8

So is seems like it is bound appropriately, but when called across the
C/Python api the first argument (self) of func2 is not separated
from the other arguments (and thus is not sent into the c
function as PyObject* self, but instead as part of PyObject* args)
-- 
http://mail.python.org/mailman/listinfo/python-list


Implementing class methods in C

2005-08-18 Thread nmichaud

I am having a problem implementing some methods of a python class in C.
The class is defined in python, but I would like to rewrite some methods
in c. Here is an example of what I want to do:

file _test.c:

#include Python.h

static PyObject *
func2(PyObject *self, PyObject *args)
{
  if (self == NULL) {
PyErr_SetString(PyExc_SystemError, self is NULL);
return NULL;
  }

  // Parse arguments
  if (!PyArg_ParseTuple(args, ))
  {
return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

static PyMethodDef TestMethods[] = {
  {func2, func2, METH_VARARGS, func2.},
  {NULL, NULL, 0, NULL} /* Sentinel */
};

PyMODINIT_FUNC
init_test(void)
{
  (void) Py_InitModule(_test, TestMethods);
}


test.py:

class Test:
  def func1(self):
print I am in func 1

import _test
import new
Test.func2 = new.instancemethod(_test.func2, None, Test)
del(new)

t = Test()
t.func2()


When I run test.py, I get a SystemError exception (which is what I raise
if self is NULL). I think my confusion lies in the use of PyObject* self
in the function declaration. Shouldn't this be set to point to the
instance of class Test that I am calling it from? Am I misunderstanding
the purpose of PyObject* self? Thanks.

Naveen

-
Naveen Michaud-Agrawal
Program in Molecular Biophysics
Johns Hopkins University
(410) 614 4435
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing class methods in C

2005-08-18 Thread Jeremy Moles
I honestly don't know the answer to this and I am entirely guessing
but--does it work without using the new module? That is:



import _test

class Foo:
pass

foo = Foo()

foo.bar = _test.func2

foo.bar()

On Thu, 2005-08-18 at 12:09 -0400, [EMAIL PROTECTED] wrote:
 I am having a problem implementing some methods of a python class in C.
 The class is defined in python, but I would like to rewrite some methods
 in c. Here is an example of what I want to do:
 
 file _test.c:
 
 #include Python.h
 
 static PyObject *
 func2(PyObject *self, PyObject *args)
 {
   if (self == NULL) {
 PyErr_SetString(PyExc_SystemError, self is NULL);
 return NULL;
   }
 
   // Parse arguments
   if (!PyArg_ParseTuple(args, ))
   {
 return NULL;
   }
 
   Py_INCREF(Py_None);
   return Py_None;
 }
 
 static PyMethodDef TestMethods[] = {
   {func2, func2, METH_VARARGS, func2.},
   {NULL, NULL, 0, NULL} /* Sentinel */
 };
 
 PyMODINIT_FUNC
 init_test(void)
 {
   (void) Py_InitModule(_test, TestMethods);
 }
 
 
 test.py:
 
 class Test:
   def func1(self):
 print I am in func 1
 
 import _test
 import new
 Test.func2 = new.instancemethod(_test.func2, None, Test)
 del(new)
 
 t = Test()
 t.func2()
 
 
 When I run test.py, I get a SystemError exception (which is what I raise
 if self is NULL). I think my confusion lies in the use of PyObject* self
 in the function declaration. Shouldn't this be set to point to the
 instance of class Test that I am calling it from? Am I misunderstanding
 the purpose of PyObject* self? Thanks.
 
 Naveen
 
 -
 Naveen Michaud-Agrawal
 Program in Molecular Biophysics
 Johns Hopkins University
 (410) 614 4435

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing class methods in C

2005-08-18 Thread nmichaud

Nope, it still doesn't work. Anyway, that's not exactly what i want, since
i want func2 to be accessible from all instances of Test()

Naveen

On Thu, 18 Aug 2005, Jeremy Moles wrote:

 I honestly don't know the answer to this and I am entirely guessing
 but--does it work without using the new module? That is:

 

 import _test

 class Foo:
   pass

 foo = Foo()

 foo.bar = _test.func2

 foo.bar()

 On Thu, 2005-08-18 at 12:09 -0400, [EMAIL PROTECTED] wrote:
  I am having a problem implementing some methods of a python class in C.
  The class is defined in python, but I would like to rewrite some methods
  in c. Here is an example of what I want to do:
 
  file _test.c:
 
  #include Python.h
 
  static PyObject *
  func2(PyObject *self, PyObject *args)
  {
if (self == NULL) {
  PyErr_SetString(PyExc_SystemError, self is NULL);
  return NULL;
}
 
// Parse arguments
if (!PyArg_ParseTuple(args, ))
{
  return NULL;
}
 
Py_INCREF(Py_None);
return Py_None;
  }
 
  static PyMethodDef TestMethods[] = {
{func2, func2, METH_VARARGS, func2.},
{NULL, NULL, 0, NULL} /* Sentinel */
  };
 
  PyMODINIT_FUNC
  init_test(void)
  {
(void) Py_InitModule(_test, TestMethods);
  }
 
  
  test.py:
 
  class Test:
def func1(self):
  print I am in func 1
 
  import _test
  import new
  Test.func2 = new.instancemethod(_test.func2, None, Test)
  del(new)
 
  t = Test()
  t.func2()
 
 
  When I run test.py, I get a SystemError exception (which is what I raise
  if self is NULL). I think my confusion lies in the use of PyObject* self
  in the function declaration. Shouldn't this be set to point to the
  instance of class Test that I am calling it from? Am I misunderstanding
  the purpose of PyObject* self? Thanks.
 
  Naveen
 
  -
  Naveen Michaud-Agrawal
  Program in Molecular Biophysics
  Johns Hopkins University
  (410) 614 4435

-- 
http://mail.python.org/mailman/listinfo/python-list


Implementing class methods in C

2005-08-15 Thread nmichaud

I am having a problem implementing some methods of a python class in C.
The class is defined in python, but I would like to rewrite some methods
in c. Here is an example of what I want to do:

file _test.c:

#include Python.h

static PyObject *
func2(PyObject *self, PyObject *args)
{
  if (self == NULL) {
PyErr_SetString(PyExc_SystemError, self is NULL);
return NULL;
  }

  // Parse arguments
  if (!PyArg_ParseTuple(args, ))
  {
return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

static PyMethodDef TestMethods[] = {
  {func2, func2, METH_VARARGS, func2.},
  {NULL, NULL, 0, NULL} /* Sentinel */
};

PyMODINIT_FUNC
init_test(void)
{
  (void) Py_InitModule(_test, TestMethods);
}


test.py:

class Test:
  def func1(self):
print I am in func 1

import _test
import new
Test.func2 = new.instancemethod(_test.func2, None, Test)
del(new)

t = Test()
t.func2()


When I run test.py, I get a SystemError exception (which is what I raise
if self is NULL). I think my confusion lies in the use of PyObject* self
in the function declaration. Shouldn't this be set to point to the
instance of class Test that I am calling it from? Am I misunderstanding
the purpose of PyObject* self? Thanks.

Naveen

-
Naveen Michaud-Agrawal
Program in Molecular Biophysics
Johns Hopkins University
(410) 614 4435
-- 
http://mail.python.org/mailman/listinfo/python-list