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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:35:50+00:00 2026-05-13T07:35:50+00:00

EDIT: Upon re-reading my original question I realized very quickly that it was very

  • 0

EDIT: Upon re-reading my original question I realized very quickly that it was very poorly worded, ambiguous, and too confusing to ever get a decent answer. That’s what I get for rushing out a question at the end of my lunch break. Hopefully this will be clearer:

I am trying to expose a simple C structure to Python (3.x) as a PyBuffer so I can retrieve a MemoryView from it. The structure I want to expose is similar to this:

struct ImageBuffer {
    void* bytes;
    int row_count;
    int bytes_per_row;
};

and it is my desire to allow the script writer to access the data like so:

img_buffer = img.get_buffer()
img_buffer[1::4] = 255 # Set every Red component to full intensity

Unfortunately the existing documentation about the C API for these structures is pretty sparse, self contradictory in places, and outright wrong in others (documented function signatures do not match those in the headers, etc.) As such I don’t have a very good idea about how to best expose this. Also, I would like to avoid including third party libs to achieve functionality that should be part of the core libs, but it feels to me like the PyBuffer functionality is still fairly immature, and perhaps something like NumPy would be a better choice.

Does anyone have any advice on this?

  • 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-13T07:35:51+00:00Added an answer on May 13, 2026 at 7:35 am

    The set of methods to implement so that your extension type supports the buffer protocol is described here: http://docs.python.org/3.1/c-api/typeobj.html#buffer-object-structures

    I recognize that the documentation is pretty rough, so the best advice I can give is to start from an existing implementation of the buffer API by a C type, for example bytesobject.c or bytearrayobject.c in the official Python source code.

    However, please note that the buffer protocol doesn’t give access to high-level notations such as the one you quoted: img_buffer[1::4] = 255 won’t work on a memoryview object.

    Edit: to be more precise, memoryviews support some kinds of slice assignment, but not all of them. Also, they are not “smart” enough to understand that assigning 255 to a slice actually means that you want the byte value to be repeated. Example:

    >>> b = bytearray(b"abcd")
    >>> m = memoryview(b)
    >>> m[0:2] = b"xy"
    >>> b
    bytearray(b'xycd')
    >>> m[0:2] = 255
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'int' does not support the buffer interface
    >>> m[0:2] = b"x"
    Traceback (most recent call last):
      File "", line 1, in 
    ValueError: cannot modify size of memoryview object
    >>> m[0::2] = b"xy"
    Traceback (most recent call last):
      File "", line 1, in 
    NotImplementedError
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: From another question I provided an answer that has links to a lot
While looking for a light-weight Scala development environment, I came upon an Scala edit
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT What small things which are too easy to overlook do I need to
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
I'm reading in a large text file with 1.4 million lines that is 24
I've been using J for a few months now, and I find that reading

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.