numpy  2.0.0
src/multiarray/buffer.c File Reference
#include <Python.h>
#include "structmember.h"
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "npy_config.h"
#include "npy_pycompat.h"
#include "buffer.h"
#include "numpyos.h"
#include "arrayobject.h"

Data Structures

struct  _tmp_string_t
struct  _buffer_info_t

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define _MULTIARRAYMODULE
#define INIT_SIZE   16

Functions

static Py_ssize_t array_getsegcount (PyArrayObject *self, Py_ssize_t *lenp)
static Py_ssize_t array_getreadbuf (PyArrayObject *self, Py_ssize_t segment, void **ptrptr)
static Py_ssize_t array_getwritebuf (PyArrayObject *self, Py_ssize_t segment, void **ptrptr)
static Py_ssize_t array_getcharbuf (PyArrayObject *self, Py_ssize_t segment, constchar **ptrptr)
static int _append_char (_tmp_string_t *s, char c)
static int _append_str (_tmp_string_t *s, char const *p)
static int _is_natively_aligned_at (PyArray_Descr *descr, PyArrayObject *arr, Py_ssize_t offset)
static int _buffer_format_string (PyArray_Descr *descr, _tmp_string_t *str, PyArrayObject *arr, Py_ssize_t *offset, char *active_byteorder)
static _buffer_info_t_buffer_info_new (PyArrayObject *arr)
static Py_ssize_t _buffer_info_cmp (_buffer_info_t *a, _buffer_info_t *b)
static void _buffer_info_free (_buffer_info_t *info)
static _buffer_info_t_buffer_get_info (PyObject *arr)
static void _buffer_clear_info (PyObject *arr)
static int array_getbuffer (PyObject *obj, Py_buffer *view, int flags)
NPY_NO_EXPORT void _array_dealloc_buffer_info (PyArrayObject *self)
static int _descriptor_from_pep3118_format_fast (char *s, PyObject **result)
static int _pep3118_letter_to_type (char letter, int native, int complex)
NPY_NO_EXPORT PyArray_Descr_descriptor_from_pep3118_format (char *s)

Variables

static PyObject * _buffer_info_cache = NULL
NPY_NO_EXPORT PyBufferProcs array_as_buffer

Define Documentation

#define INIT_SIZE   16
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION

Function Documentation

static int _append_char ( _tmp_string_t s,
char  c 
) [static]
static int _append_str ( _tmp_string_t s,
char const *  p 
) [static]
NOTE: for backward compatibility (esp. with PyArg_ParseTuple("s#", ...)) we do not define bf_releasebuffer at all.
Instead, any extra data allocated with the buffer is released only in array_dealloc.
Ensuring that the buffer stays in place is taken care by refcounting; ndarrays do not reallocate if there are references to them, and a buffer view holds one reference.
This function may be called when processing an exception -- we need to stash the error state to avoid confusing PyDict
static void _buffer_clear_info ( PyObject *  arr) [static]
Clear buffer info from the global dictionary
static int _buffer_format_string ( PyArray_Descr descr,
_tmp_string_t str,
PyArrayObject arr,
Py_ssize_t *  offset,
char *  active_byteorder 
) [static]
Insert padding manually
Insert child item
Insert field name
Prefer native types, to cater for Cython
Data types that have no standard size
It's not possible to express native-only data types in non-native npy_byte orders
XXX: datetime
XXX: timedelta
NumPy Unicode is always 4-byte
Insert padding bytes
static _buffer_info_t* _buffer_get_info ( PyObject *  arr) [static]
Get buffer info from the global dictionary
Compute information
Check if it is identical with an old one; reuse old one, if yes
Needs insertion
static Py_ssize_t _buffer_info_cmp ( _buffer_info_t a,
_buffer_info_t b 
) [static]
Compare two info structures
static void _buffer_info_free ( _buffer_info_t info) [static]
static _buffer_info_t* _buffer_info_new ( PyArrayObject arr) [static]
Fill in the info structure
Fill in format
Fill in shape and strides

References _buffer_info_t::format, and _buffer_info_t::shape.

Fast path
Strip whitespace, except from field names
Convert
static int _descriptor_from_pep3118_format_fast ( char *  s,
PyObject **  result 
) [static]
System Message: SEVERE/4 (<string>, line 1)
Incomplete section title.

 Convert PEP 3118 format string to PyArray_Descr
Fast path for parsing buffer strings corresponding to simple types.
Currently, this deals only with single-element data types.
^ means no alignment; doesn't matter for a single element
Not a single-element data type
Something unknown
static int _is_natively_aligned_at ( PyArray_Descr descr,
PyArrayObject arr,
Py_ssize_t  offset 
) [static]
Return non-zero if a type is aligned in each item in the given array, AND, the descr element size is a multiple of the alignment, AND, the array data is positioned to alignment granularity.
static int _pep3118_letter_to_type ( char  letter,
int  native,
int  complex 
) [static]
Other unhandled cases
static int array_getbuffer ( PyObject *  obj,
Py_buffer *  view,
int  flags 
) [static]
Retrieving buffers
Check whether we can provide the wanted properties
Non-strided N-dim buffers must be C-contiguous
If a read-only buffer is requested on a read-write array, we return a read-write buffer, which is dubious behavior. But that's why this call is guarded by PyArray_ISWRITEABLE rather than (flags & PyBUF_WRITEABLE).
Fill in information
static Py_ssize_t array_getcharbuf ( PyArrayObject self,
Py_ssize_t  segment,
constchar **  ptrptr 
) [static]
!defined(NPY_PY3K)
static Py_ssize_t array_getreadbuf ( PyArrayObject self,
Py_ssize_t  segment,
void **  ptrptr 
) [static]
static Py_ssize_t array_getsegcount ( PyArrayObject self,
Py_ssize_t *  lenp 
) [static]

*********** Implement Buffer Protocol ************************

System Message: WARNING/2 (<string>, line 1)
Title overline too short.

   Implement Buffer Protocol ****************************
 
removed multiple segment interface

References PyArray_DATA, PyArray_ISONESEGMENT, and PyArray_NBYTES.

static Py_ssize_t array_getwritebuf ( PyArrayObject self,
Py_ssize_t  segment,
void **  ptrptr 
) [static]

Variable Documentation

PyObject* _buffer_info_cache = NULL [static]
{ id(array): [list of pointers to _buffer_info_t, the last one is latest] }
Because shape, strides, and format can be different for different buffers, we may need to keep track of multiple buffer infos for each array.
However, when none of them has changed, the same buffer info may be reused.
Thread-safety is provided by GIL.
Initial value:
 {

    (readbufferproc)array_getreadbuf,       
    (writebufferproc)array_getwritebuf,     
    (segcountproc)array_getsegcount,        
    (charbufferproc)array_getcharbuf,       

    (getbufferproc)array_getbuffer,
    (releasebufferproc)0,
}
System Message: ERROR/3 (<string>, line 1) Document or section may not begin with a transition.

System Message: ERROR/3 (<string>, line 1) Document may not end with a transition.