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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:04:07+00:00 2026-05-22T22:04:07+00:00

I am using Python/ctypes to wrap a C library. One of the structures I

  • 0

I am using Python/ctypes to wrap a C library. One of the structures I am wrapping resembles a numerical vector, and I would like the getitem() method of the corresponding Python class to support slices. At the C – level I have slice-aware function like this:

void * slice_copy( void * ptr , int index1 , int index2 , int step) {  
...  
}  

My Python getitem() looks like this:

def __getitem__(self , index):  
   if isinstance( index , types.SliceType):
      # Call slice_copy function ...
      # Need values for arguments index1, index2 and step.  
   elif isinstance( index , types.IntType):
      # Normal index lookup
   else:
      Raise TypeError("Index:%s has wrong type" % index)  

If the slice literal looks like [1:100:10] the start,end and step properties of the slice object are all set, but e.g. in the case [-100:] the start property will be -100 and both the end and step properties will be None, i.e. they need to be sanitized before I can pass integer values to the C function slice_copy(). Now, this sanitizing is not very difficult, but I would have thought that the necessary functionality was already included in the Python source – or?

  • 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-22T22:04:08+00:00Added an answer on May 22, 2026 at 10:04 pm

    There is indeed a function in the interface. slice objects have an indices() method that accepts the length of the sequence as parameter and returns the normalised start, stop and step values as a tuple.

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

Sidebar

Related Questions

Using Python's Imaging Library I want to create a PNG file. I would like
I am trying to wrap a C library using ctypes. One feature of the
I tried loading C shared library .so in Python using ctypes.CDLL class (Linux). Here
When using Python CTypes there are the Structures, that allow you to clone c-structures
I'm attempting to convert a C header into a Python library using ctypes and
I've written Python bindings using ctypes for the units library . The bindings themselves
I'm using ctypes to load a DLL in Python. This works great. Now we'd
Using python 2.4 and the built-in ZipFile library, I cannot read very large zip
Using Python, how would I go about reading in (be from a string, file
When using Python's super() to do method chaining, you have to explicitly specify your

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.