Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6762259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:17:56+00:00 2026-05-26T14:17:56+00:00

I am trying to figure out how in C extension modules to have a

  • 0

I am trying to figure out how in C extension modules to have a variable (and maybe) quite large number of arguments to a function.

Reading about PyArg_ParseTuple it seems you have to know how many to accept, some mandatory and some optional but all with their own variable. I was hoping PyArg_UnpackTuple would be able to handle this but it seems to just give me bus errors when I try and use it in what appears to be the wrong way.

As an example take the following python code that one might want to make into an extension module (in C).

def hypot(*vals):
    if len(vals) !=1 :
        return math.sqrt(sum((v ** 2 for v in vals)))
    else: 
        return math.sqrt(sum((v ** 2 for v in vals[0])))

This can be called with any number of arguments or iterated over, hypot(3,4,5), hypot([3,4,5]), and hypot(*[3,4,5]) all give the same answer.

The start of my C function looks like this

static PyObject *hypot_tb(PyObject *self, PyObject *args) {
// lots of code
// PyArg_ParseTuple or PyArg_UnpackTuple
}

Many thinks to yasar11732. Here for the next guy is a fully working extension module (_toolboxmodule.c) that simply takes in any number or integer arguments and returns a list made up of those arguments (with a poor name). A toy but illustrates what needed to be done.

#include <Python.h>

int ParseArguments(long arr[],Py_ssize_t size, PyObject *args) {
    /* Get arbitrary number of positive numbers from Py_Tuple */
    Py_ssize_t i;
    PyObject *temp_p, *temp_p2;

    for (i=0;i<size;i++) {
        temp_p = PyTuple_GetItem(args,i);
        if(temp_p == NULL) {return NULL;}

        /* Check if temp_p is numeric */
        if (PyNumber_Check(temp_p) != 1) {
            PyErr_SetString(PyExc_TypeError,"Non-numeric argument.");
            return NULL;
        }

        /* Convert number to python long and than C unsigned long */
        temp_p2 = PyNumber_Long(temp_p);
        arr[i] = PyLong_AsUnsignedLong(temp_p2);
        Py_DECREF(temp_p2);
    }
    return 1;
}

static PyObject *hypot_tb(PyObject *self, PyObject *args)
{
    Py_ssize_t TupleSize = PyTuple_Size(args);
    long *nums = malloc(TupleSize * sizeof(unsigned long));
    PyObject *list_out;
    int i;

    if(!TupleSize) {
        if(!PyErr_Occurred()) 
            PyErr_SetString(PyExc_TypeError,"You must supply at least one argument.");
        return NULL;
    }
    if (!(ParseArguments(nums, TupleSize, args)) { 
        free(nums);
        return NULL;
    }

    list_out = PyList_New(TupleSize);
    for(i=0;i<TupleSize;i++)
        PyList_SET_ITEM(list_out, i, PyInt_FromLong(nums[i]));
    free(nums);
    return (PyObject *)list_out;
}

static PyMethodDef toolbox_methods[] = {
   { "hypot", (PyCFunction)hypot_tb, METH_VARARGS,
     "Add docs here\n"},
    // NULL terminate Python looking at the object
     { NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC init_toolbox(void) {
    Py_InitModule3("_toolbox", toolbox_methods,
                     "toolbox module");
}

In python then it is:

>>> import _toolbox
>>> _toolbox.hypot(*range(4, 10))
[4, 5, 6, 7, 8, 9]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:17:57+00:00Added an answer on May 26, 2026 at 2:17 pm

    I had used something like this earlier. It could be a bad code as I am not an experienced C coder, but it worked for me. The idea is, *args is just a Python tuple, and you can do anything that you could do with a Python tuple. You can check http://docs.python.org/c-api/tuple.html .

    int
    ParseArguments(unsigned long arr[],Py_ssize_t size, PyObject *args) {
        /* Get arbitrary number of positive numbers from Py_Tuple */
        Py_ssize_t i;
        PyObject *temp_p, *temp_p2;
    
    
        for (i=0;i<size;i++) {
            temp_p = PyTuple_GetItem(args,i);
            if(temp_p == NULL) {return NULL;}
    
            /* Check if temp_p is numeric */
            if (PyNumber_Check(temp_p) != 1) {
                PyErr_SetString(PyExc_TypeError,"Non-numeric argument.");
                return NULL;
            }
    
            /* Convert number to python long and than C unsigned long */
            temp_p2 = PyNumber_Long(temp_p);
            arr[i] = PyLong_AsUnsignedLong(temp_p2);
            Py_DECREF(temp_p2);
            if (arr[i] == 0) {
                PyErr_SetString(PyExc_ValueError,"Zero doesn't allowed as argument.");
                return NULL;
            }
            if (PyErr_Occurred()) {return NULL; }
        }
    
        return 1;
    }
    

    I was calling this function like this:

    static PyObject *
    function_name_was_here(PyObject *self, PyObject *args)
    {
        Py_ssize_t TupleSize = PyTuple_Size(args);
        Py_ssize_t i;
        struct bigcouples *temp = malloc(sizeof(struct bigcouples));
        unsigned long current;
    
        if(!TupleSize) {
            if(!PyErr_Occurred()) 
                PyErr_SetString(PyExc_TypeError,"You must supply at least one argument.");
            free(temp);
            return NULL;
        }
    
        unsigned long *nums = malloc(TupleSize * sizeof(unsigned long));
    
        if(!ParseArguments(nums, TupleSize, args)){
            /* Make a cleanup and than return null*/
            return null;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to figure out why this is happening - I have an input text
I'm writing a simple Safari Extension, and I'm trying to figure out how to
I am trying to figure out how I would go about taking serial information
I'm trying to figure out if it is possible to make an extension that
I'm writing a Python extension in C , and I'm trying to figure out
I'm trying to figure out how to write a simple Chrome extension which allows
Ok so I have been trying to figure out a way to make plain
I have been trying to figure out the best way to start working on
I've been trying to figure out where my error is in this particular function.
Trying to figure out how I can do this properly. The print_r looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.