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 8282445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:22:21+00:00 2026-06-08T10:22:21+00:00

I am making my first venture into integrating C and Python 2.7.3. For starters,

  • 0

I am making my first venture into integrating C and Python 2.7.3. For starters, I’m just trying to write a C module for Python that can do basic addition. (It is called npfind because once I figure this out, I want to write a find method for numpy)

npfind.h:

#include <math.h>

extern int add(int a, int b);

npfind.c:

#include "npfind.h"

int add(int a, int b)
{
    return a + b;
}

pynpfind.c:

#include "Python.h"
#include "npfind.h"

static char* py_add_doc = "Adds two numbers.";
static PyObject* py_add(PyObject* self, PyObject* args)
{
    int a, b, r;

    if (!PyArg_ParseTuple(args, "ii", &a, &b))
    {
        return NULL;
    }

    r = a + b;

    return Py_BuildValue("i", r);
}

static PyMethodDef* _npfindmethods = {
    {"add", py_add, METH_VARARGS, py_add_doc},
    {NULL, NULL, 0, NULL}
};

void init_npfind(void)
{
    PyObject* mod;
    mod = Py_InitModule("_npfind", _npfindmethods);
}

npfind.py:

from _npfind import *

#Do stuff with the methods

npfindsetup.py

from distutils.core import setup, Extension

setup(name="npfind", version="1.0", py_modules = ['npfind.py'],
      ext_modules=[Extension("_npfind", ["pynpfind.c", "npfind.c"])])

After all that, on Windows 7, I type

python npfindsetup.py build_ext --inplace --compiler=mingw32

Which seems to work. When I then try to find npfind.py, I get this error:

Traceback (most recent call last):
  File "npfind.py", line 1, in <module>
    from _npfind import *
ValueError: module functions cannot set METH_CLASS or METH_STATIC

I cannot figure out what it is talking about. What are METH_CLASS and METH_STATIC, and why I am I trying to set them?

  • 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-06-08T10:22:24+00:00Added an answer on June 8, 2026 at 10:22 am

    You are declaring the _npfindmethods as a pointer, and trying to initialize it as an array. When I build a code copied from your snippets, I get a lot of warnings like:

    a.c:24:5: warning: braces around scalar initializer [enabled by default]
    a.c:24:5: warning: (near initialization for '_npfindmethods') [enabled by default]
    a.c:24:5: warning: initialization from incompatible pointer type [enabled by default]
    a.c:24:5: warning: (near initialization for '_npfindmethods') [enabled by default]
    (...)
    

    The variable is initialized with an incorrect value, and thus Python finds random data inside.


    You should declare _npfindmethods as an array instead:

    static PyMethodDef _npfindmethods[] = {
        {"add", py_add, METH_VARARGS, py_add_doc},
        {NULL, NULL, 0, NULL}
    };
    

    Now it will be initialized as you expect it to. Also, because now py_add_doc needs to have constant address, you have to make it an array as well:

    static char py_add_doc[] = "Adds two numbers.";
    

    So, your final pynpfind.c would look like:

    #include "Python.h"
    #include "npfind.h"
    
    static char py_add_doc[] = "Adds two numbers.";
    static PyObject* py_add(PyObject* self, PyObject* args)
    {
        int a, b, r;
    
        if (!PyArg_ParseTuple(args, "ii", &a, &b))
        {
            return NULL;
        }
    
        r = a + b;
    
        return Py_BuildValue("i", r);
    }
    
    static PyMethodDef _npfindmethods[] = {
        {"add", py_add, METH_VARARGS, py_add_doc},
        {NULL, NULL, 0, NULL}
    };
    
    void init_npfind(void)
    {
        PyObject* mod;
        mod = Py_InitModule("_npfind", _npfindmethods);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting out making my first website. What I am trying to
I am making my first serious foray into Prism(Unity). I have a module with
I am making my first foray into scala for a production app. The app
I'm making my first game in canvas/JS and I'm running into an issue with
I am making my first attempts to write a R package. I am loading
I create a simple home page making at first the controller that point to
I'm trying to learn lisp and as i'm making my first steps i got
I am making my first program using Java3D. I have setup some transformGroups that
I've been making my first ASP.NET Visual Studio website and I have just started
I'm making some first steps in Typo3 and am currently trying to make a

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.