Extension problems (Python->C) (Att. prob.)

Morten W. Petersen morten at src.no
Fri Aug 18 11:07:53 EDT 2000


I've got a problem, trying to extend Python, using C.
The bug is that if I call method spam_system, from the
tapi module with the wrong number of arguments, I don't
get a TypeError exception until I do the same mistake again, with
or without the same arguments.

================================= Begin C-extension

#include "Python.h"

static PyObject *
spam_system(self,args)
     PyObject *self;
     PyObject *args;
{
  char *command;
  int sts;

  if(!PyArg_ParseTuple(args, "s", &command))
    return Py_BuildValue("s", "Oops");

  sts = system(command);

  return Py_BuildValue("i", sts);
}

static PyMethodDef tapi_methods[] =
{
  { "spam_system", spam_system, METH_VARARGS},
  { NULL, NULL }
};

void inittapi()
{
  Py_InitModule("tapi",tapi_methods);
}

================================= End C-extension

================================= Begin session

Script started on Fri Aug 18 13:59:26 2000
morten at slakka:~/programming/c > python
Python 1.5.2 (#1, Mar 11 2000, 13:03:53)  [GCC 2.95.2 19991024 (release)]
on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from tapi import *
>>> spam_system('abcdef','abcdef')
'Oops'
>>> spam_system('abcdef','abcdef','abcdef')
TypeError: function requires exactly 1 argument; 2 given
>>> spam_system('ls -l')
total 40
-rw-r--r--   1 morten   users          69 Aug 18 11:40 Makefile
drwxr-xr-x  15 morten   users        4096 Aug 18 01:38 gnupg-1.0.2
-rw-r--r--   1 morten   users        1707 Aug 18 13:56 pythonapi.c
-rw-r--r--   1 morten   users         286 Aug 17 22:26 pythonapi.c~
-rwxr-xr-x   1 morten   users        7482 Aug 18 13:56 pythonapi.so
-rw-r--r--   1 morten   users         449 Aug 18 13:58 tapi.c
-rw-r--r--   1 morten   users        1707 Aug 18 13:57 tapi.c~
-rwxr-xr-x   1 morten   users        6143 Aug 18 13:58 tapi.so
-rw-r--r--   1 morten   users           0 Aug 18 13:59 typescript
0
>>>
morten at slakka:~/programming/c > exit

Script done on Fri Aug 18 14:00:11 2000

================================= End session

================================= Start session

Script started on Fri Aug 18 14:06:06 2000
morten at slakka:~/programming/c > python
Python 1.5.2 (#1, Mar 11 2000, 13:03:53)  [GCC 2.95.2 19991024 (release)]
on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from tapi import *
>>> spam_system('abcdef','abcdef')
'Oops'
>>> spam_system('abcdef','abcdef')
TypeError: function requires exactly 1 argument; 2 given
>>>
morten at slakka:~/programming/c > exit

Script done on Fri Aug 18 14:06:23 2000

================================= End session




More information about the Python-list mailing list