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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:10:45+00:00 2026-05-27T19:10:45+00:00

I have a class that is getting exported from C++ to Python in SWIG.

  • 0

I have a class that is getting exported from C++ to Python in SWIG. Everything works fine. Now I want to define getattribute to handle virtualize access to variables and functions that are defined in a scripting language built into the C++ code. However when I define the getattribute function using %pythoncode it is not working as expected. If I cannot find the variables or functions I am supposed to raise and exception called AttributeError. However the SWIG function getattr chokes on this.

%pythoncode %{
    def __getattribute__(self, attribute):
        raise AttributeError(attribute)

%}

Now if I do this it is okay:

%pythoncode %{
    def __getattribute__(self, attribute):
        return object.__getattribute__(self, attribute)
%}

So, the behaviour of the SWIG produced getattr does not work properly when I raise an AttributeError like I am supposed to do when and attribute is not found. So, for my purposes I will be using the second example and inserting my own code before the routine to determine if a virtualized function exists. If not I will let the default object getattribute function handle it.

Is there a better way to approach this?

Now that I look at this i am finding it does not work in regular Python 2.7 either:

class testmethods(object):
    def __init__(self):
        self.nofunc1 = None
        self.nofunc2 = "nofunc2"
    def func1(self):
        print "func1"
    def func2(self):
        print "func2"

    def __getattribute__(self, attribute):
        print "Attribute:",attribute
        raise AttributeError(attribute)

This raises the exception, but does not switch responsibility to “getattr” function. So how is one supposed to handle this?

Okay, strike that. If getattr is present in the object raising the exception does work. So swig behaviour is not correct.

  • 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-27T19:10:45+00:00Added an answer on May 27, 2026 at 7:10 pm

    Now, I think I figured this out:

    def __getattribute__(self, attribute): 
        try: 
            defattrib = object.__getattribute__(self, attribute) 
        except AttributeError,e: 
            defattrib = None 
        if defattrib is not None: 
            return defattrib             
    
        # find attributes in user defined functions 
                ... 
    
    
        # if an attribute cannot be found 
        raise AttributeError(attribute) 
    

    This seems to work fine. swig getattr seems to handle the exception properly. So it is just my code that was not working.

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

Sidebar

Related Questions

I have a property inside a class that is getting changed by something. The
I have a class that I want to use to store properties for another
I have a class that contains, among other things, an std::list. I want to
I have a class that is getting called and for some reason one of
I have a class that I want symfony2 to autoload so I can reference
I have a class that inherits from UIView, and this class has some controls
I have a class that inherits from SPItemEventReceiver and implements ItemAdded, ItemUpdated, and ItemDeleted.
I have a class that inherits from a base class and implements the following...
I have a class that is used for LINQ extension methods. I am getting
If I have a Document class that extends MovieClip, and I want to use

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.