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

keywords for optional args in extension modules


Python 2.3.4 (#1, May 29 2004, 17:05:23)
[GCC 3.3.3] on linux2

Getting some strange behaviour with keyword arguments for optional
arguments in extension modules. See the simple test case below

--------8<--------------------------------------------------
#include "Python.h"

static PyObject *
keywdarg_test(PyObject *self, PyObject *args, PyObject *keywds)
{
int one;
unsigned int two = 2;
int three = 3;

static char *kwlist[] = {"one", "two", "three", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|Ii", kwlist,
&one, &two, &three))
return NULL;

Py_INCREF(Py_None);
return Py_None;
}

static PyMethodDef keywdarg_methods[] = {
{"test", (PyCFunction)keywdarg_test, METH_VARARGS | METH_KEYWORDS},
{NULL, NULL, 0, NULL}
};

void
initkeywdarg(void)
{
Py_InitModule("keywdarg", keywdarg_methods);
}
--------8<--------------------------------------------------

Compile the module

$ cc -g -c keywdarg.c -I/usr/include/python2.3
$ ld -shared -o keywdarg.so keywdarg.o
Test it

In [1]: from keywdarg import *

In [2]: test(1)

In [3]: test(1,two=2)

In [4]: test(1,two=2,three=3)

In [5]: test(1,three=3,two=2)

In [6]: test(1,three=3)
---------------------------------------------------------------------------
exceptions.TypeError Traceback (most recent call last)

/home/sean/research/code/framework/wiener/<console>

TypeError: argument 2, item 1074941247, item 1079307903 impossible<bad format char>
However if I change the order of the optional arguments so the unsigned
int is last as below then it works fine.

if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|iI", kwlist,
&one, &three, &two))
This seems buggy. Python 2.3 is getting a bit old now but do later later
versions still exhibit this behaviour?

Cheers, Sean

--
~/.signature
Aug 12 '05 #1
0 1014

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

Similar topics

0
by: Roger Upole | last post by:
I'm having a problem with an extension module using PyArg_ParseTupleAndKeywords with 2.3.2. Distilled down: PyObject *wtf(PyObject *self, PyObject *args, PyObject *kwargs) { WCHAR...
4
by: Brano Zarnovican | last post by:
Hi ! If I define 'f' like this def f(a): print a then, the call with keywords f(1, optional=2)
31
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based...
5
by: Ron Adam | last post by:
Is there a way to preserve or capture the order which keywords are given? >>> def foo(**kwds): .... print kwds .... >>> foo(one=1, two=2, three=3) {'three': 3, 'two': 2, 'one': 1} I...
9
by: Nenad Loncarevic | last post by:
I am a geologist, and over the years I've accumulated quite a number of proffesional papers on the subject, in various publications. I would like to make a database that would help me find the...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
1
by: zpinhead | last post by:
I am unable to get my downloaded extension from pecl to link up with php properly. seems like the php.so I could not use pear install http. pear claimed the extension was already installed....
1
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules...
4
by: mdh | last post by:
K&R briefly mention splitting programs into seperate files.(page 82) Using my compiler (X-code) I noticed that creating a header file gives, as expected one .h file, but creating a new ".c" file...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.