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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:46:50+00:00 2026-05-24T12:46:50+00:00

I have a C library with several datatypes which I have wrapped with Python

  • 0

I have a C library with several datatypes which I have wrapped with Python and ctypes – works very well! In C I have the following (schematic) code:

typedef struct type1_struct type1_t;
typedef struct type2_struct type2_t;  

void some_function( type1_t *t1 , type2_t *t2) {  
  if (t2 == NULL) {  
     // Do whatever  
  } else {  
     // 
  }
}  

The main point in this code is that some_function() can take NULL as value for the t2 argument. In Python the type1_t and type2_t types are wrapped with classes Type1 and Type2 using the from_param() method:

Class Type1:
   def from_param(self):
       return self.c_ptr


   def call_some_func(self , arg2 = None):
       # This fails when the python/ctypes tries to
       # lookup the from_param() method and arg2 is None. 
       cfunc_some_function( self , arg2 )     


lib_handle = ctypes.CDLL( lib )
cfunc_some_function = getattr( lib_handle , "some_function")
cfunc_some_function.argtypes = [Type1 , Type2]

So the cfunc_some_function function is initialized to take a Type1 and Type2 instance as arguments, the ctypes layer will then call the from_param() methods of the two input arguments; however I would like the ‘call_some_func()’ method of the Type1 class to accept None for the arg2 argument, but then ctypes tries to call the from_param() method of the None object – which obviously fails.

So – I guess my question is: Is it possible to get the ctypes function call code to just pass NULL when it gets a None input argument?

Joakim

  • 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-24T12:46:50+00:00Added an answer on May 24, 2026 at 12:46 pm

    The from_param() method needs to be a class method, but you have defined it as an instance method. Change it to a classmethod and check whether the parameter is None.

    Something like (untested):

    class Type1:
       @classmethod
       def from_param(cls, obj):
           if obj is None:
               return c_void_p()
           else:
               return obj.c_ptr
    

    and the same for Type2.

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

Sidebar

Related Questions

I have created a communication library which is linked statically into several different applications.
I'm building a library for the BlackBerry, which needs to have several strings in
We have a C++ library that we provide to several different clients. Recently we
I have several static factory patterns in my PHP library. However, memory footprint is
I have several C# projects along with some internal library components that I'm trying
I have a class library containing several structures each consisting of several value and
I have a project which uses a large library residing in its own repository.
I have written a library that exposes references to several related object types. All
I have fairly large C++ library with several sub-libraries that support it, and I
I have a solution that links to several library projects located elsewhere on my

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.