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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:04:13+00:00 2026-05-27T04:04:13+00:00

I ran into a situation with pure python and C python module. To summarize,

  • 0

I ran into a situation with pure python and C python module.
To summarize, how can I accept and manipulate python object in C module?
My python part will look like this.


    #!/usr/bin/env python

    import os, sys
    from c_hello import *

    class Hello:
        busyHello = _sayhello_obj

    class Man:
        def __init__(self, name):
            self.name = name
        def getName(self):
            return self.name

    h = Hello()
    h.busyHello( Man("John") )

in C, two things need to be resolved.
first, how can I receive object?
second, how can I call a method from the object?


    static PyObject *
    _sayhello_obj(PyObject *self, PyObject *args)
    {
      PyObject *obj;
      // How can I fill obj?

      char s[1024];
      // How can I fill s, from obj.getName() ?

      printf("Hello, %s\n", s);
      return Py_None;
    }
  • 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-27T04:04:14+00:00Added an answer on May 27, 2026 at 4:04 am

    To extract an argument from an invocation of your method, you need to look at the functions documented in Parsing arguments and building values, such as PyArg_ParseTuple. (That’s for if you’re only taking positional args! There are others for positional-and-keyword args, etc.)

    The object you get back from PyArg_ParseTuple doesn’t have it’s reference count increased. For simple C functions, you probably don’t need to worry about this. If you’re interacting with other Python/C functions, or if you’re releasing the global interpreter lock (ie. allowing threading), you need to think very carefully about object ownership.

    static PyObject *
    _sayhello_obj(PyObject *self, PyObject *args)
    {
      PyObject *obj = NULL;
      // How can I fill obj?
    
      static char fmt_string = "O" // For "object"
    
      int parse_result = PyArg_ParseTuple(args, fmt_string, &obj);
    
      if(!parse_res)
      {
        // Don't worry about using PyErr_SetString, all the exception stuff should be
        // done in PyArg_ParseTuple()
        return NULL;
      }
    
      // Of course, at this point you need to do your own verification of whatever
      // constraints might be on your argument.
    

    For calling a method on an object, you need to use either PyObject_CallMethod or PyObject_CallMethodObjArgs, depending on how you construct the argument list and method name. And see my comment in the code about object ownership!

    Quick digression just to make sure you’re not setting yourself up for a fall later: If you really are just getting the string out to print it, you’re better off just getting the object reference and passing it to PyObject_Print. Of course, maybe this is just for illustration, or you know better than I do what you want to do with the data 😉

      char s[1024];
      // How can I fill s, from obj.getName() ?
    
      // Name of the method
      static char method_name = "getName";
      // No arguments? Score! We just need NULL here
      char method_fmt_string = NULL;
    
      PyObject *objname = PyObject_CallMethod(obj, obj_method, method_fmt_string);
      // This is really important! What we have here now is a Python object with a newly
      // incremented reference count! This means you own it, and are responsible for
      // decrementing the ref count when you're done. See below.
    
      // If there's a failure, we'll get NULL
      if(objname == NULL)
      {
        // Again, this should just propagate the exception information
        return NULL;
      }
    

    Now there are a number of functions in the String/Bytes Objects section of the Concrete Objects Layer docs; use whichever works best for you.

    But do not forget this bit:

      // Now that we're done with the object we obtained, decrement the reference count
      Py_XDECREF(objname);
    
      // You didn't mention whether you wanted to return a value from here, so let's just
      // return the "None" singleton.
      // Note: this macro includes the "return" statement!
      Py_RETURN_NONE;
    }
    

    Note the use of Py_RETURN_NONE there, and note that it’s not return Py_RETURN_NONE!

    PS. The structure of this code is dictated to a great extent by personal style (eg. early returns, static char format strings inside the function, initialisation to NULL). Hopefully the important information is clear enough apart from stylistic conventions.

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

Sidebar

Related Questions

I ran into this situation today. I have an object which I'm testing for
A co-worker recently ran into a situation where a query to look up security
I ran into a situation where I need access to a javascript object from
I ran into an unusual situation yesterday night. I need to match only part
I ran into a situation today where Java was not invoking the method I
I ran into the situation where my class is making one asynchronous web call
I've been working on optimizing a query and have ran into a situation that's
So odd situation that I ran into today with OrderBy: Func<SomeClass, int> orderByNumber =
I ran into a situation where I had the following two implementations located in
I'm pretty new to the Springframework (as you will guess) and ran into 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.