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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:21:56+00:00 2026-05-27T10:21:56+00:00

I’m writing Python 3 extensions in C++ and I’m trying to find a way

  • 0

I’m writing Python 3 extensions in C++ and I’m trying to find a way to check if a PyObject is related to a type (struct) defining its instance layout. I’m only interested in static-size PyObject, not PyVarObject. The instance layout is defined by a struct with certain well-defined layout: mandatory PyObject header and (optional) user-defined members.

Below, is example of PyObject extension based on the well-known Noddy example in Defining New Types:

// Noddy struct specifies PyObject instance layout
struct Noddy {
    PyObject_HEAD
    int number;
};

// type object corresponding to Noddy instance layout
PyTypeObject NoddyType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "noddy.Noddy",             /*tp_name*/
    sizeof(Noddy),             /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    ...
    Noddy_new,                 /* tp_new */
};

It is important to notice that the Noddy is a type, a compile-time entity,
but NoddyType is an object present in memory at run-time.
The only obvious relation between the Noddy and NoddyType seems to be
value of sizeof(Noddy) stored in tp_basicsize member.

The hand-written inheritance implemented in Python specifies rules which allow to cast between PyObject and type used to declare the instance layout of that particular PyObject:

PyObject* Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    // When a Python object is a Noddy instance,
    // its PyObject* pointer can be safely cast to Noddy
    Noddy *self = reinterpret_cast<Noddy*>(type->tp_alloc(type, 0));

    self->number = 0; // initialise Noddy members

    return reinterpret_cast<PyObject*>(self);
}

In circumstances like various slot functions, it is safe to assume “a Python object is a Noddy” and cast without any checks.
However, sometimes it is necessary to cast in other situations, then it feels like a blind conversion:

void foo(PyObject* obj)
{
    // How to perform safety checks?
    Noddy* noddy = reinterpret_cast<Noddy*>(obj);
    ...
}

It is possible to check sizeof(Noddy) == Py_TYPE(obj)->tp_basicsize, but it is insufficient solution due to:

1) If a user will derive from Noddy

class BabyNoddy(Noddy):
    pass

and obj in foo points to instance of the BabyNoddy, Py_TYPE(obj)->tp_basicsize is diferent.
But, it is still safe to cast to reinterpret_cast<Noddy*>(obj) to get pointer to the instance layout part.

2) There can be other struct declaring instance layout of the same size as Noddy:

struct NeverSeenNoddy {
    PyObject_HEAD
    short word1;
    short word2;
};

In fact, C langauge level, NeverSeenNoddy struct is compatible with the NoddyType type object – it can fit into NoddyType. So, cast could be perfectly fine.

So, my big question is this:

Is there any Python policy which could be used to determine if a PyObject is compatible with the Noddy instance layout?

Any way to check if PyObject* points to the object part which is embedded in the Noddy?

If not policy, is there any hack possible?

EDIT: There are a few questions which seem to be similar, but in my opinion they are different to the one I have asked. For example: Accessing the underlying struct of a PyObject

EDIT2: In order to understand why I marked Sven Marnach’s response as the answer, see comments below that answer.

  • 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-27T10:21:57+00:00Added an answer on May 27, 2026 at 10:21 am

    In Python, you can check if obj is of type Noddy or a derived type by using the test isinstance(obj, Noddy). The test in the C-API whether some PyObject *obj is of type NoddyType or a derived type is basically the same, you use PyObject_IsInstance():

    PyObject_IsInstance(obj, &NoddyType)
    

    As for your second question, there is no way to achieve this, and if you think you need this, your design has severe shortcomings. It would be better to derive NeverSeenNoddyType from NoddyType in the first place — then the above check will also recognize an object of the derived type as an instance of NoddyType.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.