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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:58:14+00:00 2026-06-04T12:58:14+00:00

Using boost::python , I have been able to wrap a class ( Node )

  • 0

Using boost::python, I have been able to wrap a class (Node) which has some virtual functions, and it’s been a hoot, but now I’m trying to override setattr/getattr for the class.
I’ve got boost::python to call my own setattr implementation, but I can’t figure out how to avoid the recursion that happens.

So I have a Node class, and I want to be able to write:

node1.param1 = 5          # Call node.SetParam
node1.plain_member = 7    # Call object.__setattr__

So far, I have (very abridged):

namespace bpy = boost::python;

struct NodeWrap : vcNode, bpy::wrapper<Node> {
  ...  
  static void setattr(bpy::object obj, std::string attr, bpy::object val)
  {
     NodeWrap const& node = bpy::extract<NodeWrap const&>(obj)();
     ParamRef p = node.FindParam(attr);
     if (p)
        py_to_param(p, val); //This part works fine
     else
     {
        obj.attr(attr) = val; //Problematic line - recurses indefinitely
     }
  }
};

then in the BOOST_PYTHON_MODULE section, I have:

bpy::class_<NodeWrap, boost::shared_ptr<NodeWrap>, boost::noncopyable>("Node")
  ...
  .def("__setattr__". &NodeWrap::setattr);

As I said, it compiles just fine, and will set any node “params” correctly, but if I try to set a non-param, such as self.plain_old_member = 7 then I get a “Recursion Depth Exceeded” error, because I gather boost::object::attr will call the class’ __setattr__ method

In python, I would get around this by using

super(Node).__setattr__(self, attr, val)

in the problem line, but I can’t figure out how to do that from boost::python.
I don’t mind going to the C API if I have to (in fact, I’ve tried using PyObject_SetAttrString to no avail), but I just don’t know how to get there from here.

Cheers,
Matt

  • 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-04T12:58:16+00:00Added an answer on June 4, 2026 at 12:58 pm

    I believe the Python C-API function you want is PyObject_GenericSetAttr; that is, you should replace the “problematic line” with:

    bpy::str attr_str(attr);
    if (PyObject_GenericSetAttr(obj.ptr(), attr_str.ptr(), val.ptr()) != 0)
        bpy::throw_error_already_set();
    

    I think this is equivalent to calling object.__setattr__(self, attr, val) in Python, which is not the same as calling __setattr__ on a base class, but the difference only matters if a base class also overrides __setattr__. If that’s the case, you’d need to do something like this:

    bpy::object cls(bpy::handle<>(PyObject_Type(obj.ptr())));
    bpy::object base_cls = cls.attr("__bases__")[0];
    base_cls.attr("__setattr__")(obj, attr, val);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using boost.python to make python-modules written in c++. I have some base class
I have some Base class in c++. I export it into python using boost::python.
I'm using boost python. I've exported some function which takes class CL_DomElement in arguments.
I have a C++ value type wrapped with Boost.Python which has a concept of
Class member functions in Python have to explicitly declare a self parameter which represents
Using boost python I need create nested namespace. Assume I have following cpp class
I am trying to overload operators of a C++ class using Boost.Python. According to
I am using boost shared pointers in my program, and I have a class
I've been trying to extend Python with C++ using Boost in Windows 7. This
I have problems importing a boost-python module on WinXP-32. I'm using python 2.6.6, boost.python

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.