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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:52:01+00:00 2026-06-15T20:52:01+00:00

I’ve been searching around the web with no luck. I have the following Python

  • 0

I’ve been searching around the web with no luck. I have the following Python code:

class LED(Structure):
_fields_ = [
            ('color', c_char_p),
            ('id', c_uint32)
            ]

class LEDConfiguration(Structure):
_fields_ = [
            ('daemon_user', c_char_p),
            ('leds', POINTER(LED)),
            ('num_leds', c_uint32)
            ]

Here is a simplified example function that uses these structures and returns an LEDConfiguration.

def parseLedConfiguration(path, board):
lc = LEDConfiguration()
for config in configs:
    if( config.attributes['ID'].value.lstrip().rstrip() == board ):
        lc.daemon_user = c_char_p('some_name')
        leds = []
        #Imagine this in a loop
        ld = LED()
        ld.color = c_char_p('red')
        ld.id = int(0)
        leds.append(ld)
        #end imagined loop

        lc.num_leds = len(leds)
        lc.leds = (LED * len(leds))(*leds)

return lc

Now this the C code I am using (I’ve stripped out everything involved with setting up python/calling the “parseLedConfiguration” function/etc but I can add it in if it is helpful).

        /*Calling the python function "parseLedConfiguration"
          pValue is the returned "LEDConfiguration" python Structure*/

        pValue = PyObject_CallObject(pFunc, pArgs);
        Py_DECREF(pArgs);

        if (pValue != NULL)
        {
            int i, num_leds;
            PyObject *obj = PyObject_GetAttr(pValue, PyString_FromString("daemon_user"));
            daemon_user = PyString_AsString(obj);
            Py_DECREF(obj);

            obj = PyObject_GetAttr(pValue, PyString_FromString("num_leds"));
            num_leds = PyInt_AsLong(obj);
            Py_DECREF(obj);

            obj = PyObject_GetAttr(pValue, PyString_FromString("leds"));
            PyObject_Print(obj, stdout, 0);

My problem is figuring out how to access what is returned to the final “obj”. The “PyObject_Print” on the “obj” shows this output:

<ConfigurationParser.LP_LED object at 0x7f678a06fcb0>

I want to get into a state where I can access that LP_LED object in the same way I’m accessing the above “LEDConfiguration” object.

EDIT 1

I guess another maybe more important question, is my python code correct? Is that how I should be storing a list or array of “Structure” inside another “Structure” so it can be accessed from the Python C API?

Thanks!

  • 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-15T20:52:03+00:00Added an answer on June 15, 2026 at 8:52 pm

    Since your EDIT 1 clarifies the underlying question, let me put that at top:

    guess another maybe more important question, is my python code correct? Is that how I should be storing a list or array of “Structure” inside another “Structure” so it can be accessed from the Python C API?

    No, that’s how you should be storing an array of Structure inside another Structure so it can be accessed from non-Python-C-API C code. If you want it to be accessed from the Python C API, just use a Python list.

    In general, if you’re writing code in both Python and C, only one side has to bend over backward to work with the other one. The point of using ctypes Structures and POINTERs and the like in Python is to allow them to work directly in C, without having to go through the C API. Conversely, the point of using functions like PyList_GetItem is to allow you to use normal Python code, not ctypes Python code.

    So, if you want to store a list inside a Structure to be accessed via the Python C API, just store a Python list—and you really don’t need the Structure in the first place; use a normal Python class (possibly with __slots__). You can write this code without importing ctypes at all.

    Conversely, if you want to store structures that can be used directly in C, you can do that with ctypes; then, in the C code, once you’ve gotten into Structure guts of the PyObject * you don’t need the Python API anymore, because the structure is all C. This is usually the way you go when you have existing C code, and want to interface with it from Python, rather than when you’re designing the C code from scratch, but there’s no rule that says you can’t use it the other way.

    Meanwhile, if this is your first attempt at writing C and Python code that talk to each other, I’d suggest you use Cython. Then, once you’re comfortable with that, if you want to learn ctypes, do a different project that uses Python with ctypes to talk to C code that knows nothing at all about Python. And then, a third project that uses the C API to talk to Python code that knows nothing about ctypes (like most C extension modules). Once you’re familiar with all three, you’ll be able to pick the right one for most projects in the future.

    Now, to answer the specific problem:

    First, when PyList_GetItem (or most other functions in the C API) returns NULL, this means there’s an exception, so you should check the exception and log it. Trying to debug NULL return values in the C API without looking at the set exception is like trying to debug Python code without looking at the tracebacks.

    Anyway, there are a few obvious reasons this function could fail: Maybe you’re calling it with an out-of-bounds index, or maybe you’re calling it on something that isn’t a list at all.

    In fact, the second one seems pretty obvious here. If printing out obj gives you this:

    <ConfigurationParser.LP_LED object at 0x7f678a06fcb0>

    Then you’ve got a (pointer to an) LED object, and LED objects aren’t lists.

    And if you look at your code, you don’t seem to have a list of LED objects anywhere, at least not in the code you show us. You do have a POINTER(LED), which could hold a C-array-decayed C array of LEDs, but that’s not the same thing as a Python list of them. It’s just a C array, which you use C array syntax to dereference:

    PyObject *led = ledarray[i];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.