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

  • Home
  • SEARCH
  • 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 3444246
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:53:28+00:00 2026-05-18T08:53:28+00:00

I am trying to upgrade a small C module to work with Python 3.x

  • 0

I am trying to upgrade a small C module to work with Python 3.x and having trouble getting it to compile. My roadblock right now is that the preprocessor defines I am supposed to be using to check the Python version aren’t working.

The module contains two .c files at the moment (I’ve temporarily commented out the rest). In both files, PY_MAJOR_VERSION is undefined so the compiler cannot use the Python 3.x specific definitions where needed.

mymodule.c:

#ifndef PY_MAJOR_VERSION
#error Major version not defined!
#endif

#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif

#include "Python.h"
#include "myobj.h"

/* omitted: irrelevant boilerplate structs */

PyMODINIT_FUNC
initmymodule(void)
{
    PyObject* m;

#ifdef PY3K
    m = PyModule_Create(&mymodule_struct);
#else
    (void) Py_InitModule("mymodule", MyModMethods);
    m = Py_InitModule3("mymodule", NULL,
               "My Module");
#endif

    /* omitted: the rest of the module init code */
}

myobj.c:

#ifndef PY_MAJOR_VERSION
#error Major version not defined!
#endif

#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif

#include "Python.h"
#define NEED_STATIC
#include "myobj.h"
#undef NEED_STATIC

#ifdef PY3K
#define PYSTR_FROMC PyUnicode_FromString
#define PYSTR_FORMAT PyUnicode_Format
#define PYINT_FROMC PyLong_FromLong
#else
#define PYSTR_FROMC PyString_FromString
#define PYSTR_FORMAT PyString_Format
#define PYINT_FROMC PyInt_FromLong
#endif

/* omitted: rest of module code */

setup.py:

from distutils.core import setup, Extension

module1 = Extension('mymodule', sources = ['mymodule.c', 'myobj.c'])
setup(name='mymodule', version='0.1', ext_modules=[module1])

I am building with c:\python31\python setup.py bdist_wininst

Where is PY_MAJOR_VERSION supposed to get defined? Is it something I need to tell distutils to pass to the compiler?

  • 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-18T08:53:29+00:00Added an answer on May 18, 2026 at 8:53 am

    I figured out what I was doing wrong. It’s Python.h that defines PY_MAJOR_VERSION. By putting my #defines ahead of my #includes, I missed out on the definition. I don’t know why I was thinking that the build system would define it for me…

    Moving the #if PY_MAJOR_VERSION >= 3 so that it executes after #include “Python.h” fixes the problem. I will leave this here in case someone else is as dumb as I was, because there was nothing useful on Google for this query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been trying to upgrade my subversion installation, but due to (what I believe) are
I'm trying to upgrade my subversion server (I have it hosted with Dreamhost) This
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements
I am here to write a small database application that will be running in
I have a few models that contain static data, and I'm trying to cache
I am trying upgrade my javascript programming skills ( or lets say my programming
Trying to upgrade an MFC app to use the new MFC feature pack, we
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to get my css / C# functions to look like this: body {

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.