@@ -85,7 +85,6 @@ gentype_alloc(PyTypeObject *type, Py_ssize_t nitems)
85
85
static void
86
86
gentype_dealloc (PyObject * v )
87
87
{
88
- _dealloc_cached_buffer_info (v );
89
88
Py_TYPE (v )-> tp_free (v );
90
89
}
91
90
@@ -1691,7 +1690,6 @@ gentype_reduce(PyObject *self, PyObject *NPY_UNUSED(args))
1691
1690
* sticks around after the release.
1692
1691
*/
1693
1692
PyBuffer_Release (& view );
1694
- _dealloc_cached_buffer_info (self );
1695
1693
}
1696
1694
else {
1697
1695
Py_DECREF (ret );
@@ -2365,9 +2363,167 @@ static PySequenceMethods voidtype_as_sequence = {
2365
2363
};
2366
2364
2367
2365
2368
- static PyBufferProcs gentype_as_buffer = {
2369
- .bf_getbuffer = gentype_getbuffer ,
2370
- /* release buffer not defined (see buffer.c) */
2366
+
2367
+ /**begin repeat
2368
+ * #name = bool, byte, short, int, long, longlong, ubyte, ushort, uint, ulong,
2369
+ * ulonglong, half, float, double, longdouble, cfloat, cdouble,
2370
+ * clongdouble#
2371
+ * #Name = Bool, Byte, Short, Int, Long, LongLong, UByte, UShort, UInt, ULong,
2372
+ * ULongLong, Half, Float, Double, LongDouble, CFloat, CDouble,
2373
+ * CLongDouble#
2374
+ * #NAME = BOOL, BYTE, SHORT, INT, LONG, LONGLONG, UBYTE, USHORT, UINT, ULONG,
2375
+ * ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, CDOUBLE,
2376
+ * CLONGDOUBLE#
2377
+ * #fmt = ?, b, h, i, l, q, B, H, I, L, Q, e, f, d, g, Zf, Zd, Zg#
2378
+ */
2379
+
2380
+ static int
2381
+ @name @_getbuffer (PyObject * self , Py_buffer * view , int flags )
2382
+ {
2383
+ if ((flags & PyBUF_WRITEABLE ) == PyBUF_WRITEABLE ) {
2384
+ return -1 ;
2385
+ }
2386
+ Py @Name @ScalarObject * scalar = (Py @Name @ScalarObject * )self ;
2387
+
2388
+ static char fmt [3 ] = "@fmt@" ;
2389
+
2390
+ view -> ndim = 0 ;
2391
+ view -> len = sizeof (scalar -> obval );
2392
+ view -> itemsize = sizeof (scalar -> obval );
2393
+ view -> shape = NULL ;
2394
+ view -> strides = NULL ;
2395
+ view -> suboffsets = NULL ;
2396
+ Py_INCREF (self );
2397
+ view -> obj = self ;
2398
+ view -> buf = & (scalar -> obval );
2399
+
2400
+ if ((flags & PyBUF_FORMAT ) != PyBUF_FORMAT ) {
2401
+ /* It is unnecessary to find the correct format */
2402
+ view -> format = NULL ;
2403
+ return 0 ;
2404
+ }
2405
+
2406
+ view -> format = fmt ;
2407
+
2408
+ return 0 ;
2409
+ }
2410
+
2411
+ static PyBufferProcs @name @_arrtype_as_buffer = {
2412
+ .bf_getbuffer = @name @_getbuffer ,
2413
+ /* No need to release the buffer */
2414
+ };
2415
+
2416
+ /**end repeat**/
2417
+
2418
+ static int
2419
+ unicode_getbuffer (PyObject * self , Py_buffer * view , int flags )
2420
+ {
2421
+ if ((flags & PyBUF_WRITEABLE ) == PyBUF_WRITEABLE ) {
2422
+ return -1 ;
2423
+ }
2424
+ PyUnicodeScalarObject * scalar = (PyUnicodeScalarObject * )self ;
2425
+ Py_ssize_t length = PyUnicode_GetLength (self );
2426
+
2427
+ view -> ndim = 0 ;
2428
+ view -> len = length * 4 ;
2429
+ view -> itemsize = length * 4 ;
2430
+ view -> shape = NULL ;
2431
+ view -> strides = NULL ;
2432
+ view -> suboffsets = NULL ;
2433
+ Py_INCREF (self );
2434
+ view -> obj = self ;
2435
+
2436
+ if (scalar -> obval == NULL ) {
2437
+ /*
2438
+ * Unicode may not have the representation available, `scalar_value`
2439
+ * ensures materialization.
2440
+ */
2441
+ PyArray_Descr * descr = PyArray_DescrFromType (NPY_UNICODE );
2442
+ scalar_value (self , descr );
2443
+ Py_DECREF (descr );
2444
+ if (scalar -> obval == NULL ) {
2445
+ /* allocating memory failed */
2446
+ Py_SETREF (view -> obj , NULL );
2447
+ return -1 ;
2448
+ }
2449
+ }
2450
+ view -> buf = scalar -> obval ;
2451
+
2452
+ if ((flags & PyBUF_FORMAT ) != PyBUF_FORMAT ) {
2453
+ /* It is unnecessary to find the correct format */
2454
+ view -> format = NULL ;
2455
+ return 0 ;
2456
+ }
2457
+
2458
+ if (scalar -> buffer_fmt != NULL ) {
2459
+ view -> format = scalar -> buffer_fmt ;
2460
+ }
2461
+ else {
2462
+ scalar -> buffer_fmt = PyObject_Malloc (22 );
2463
+ if (scalar -> buffer_fmt == NULL ) {
2464
+ Py_SETREF (view -> obj , NULL );
2465
+ return -1 ;
2466
+ }
2467
+ PyOS_snprintf (scalar -> buffer_fmt , 22 , "%" NPY_INTP_FMT "w" , length );
2468
+ view -> format = scalar -> buffer_fmt ;
2469
+ }
2470
+
2471
+ return 0 ;
2472
+ }
2473
+
2474
+ static PyBufferProcs unicode_arrtype_as_buffer = {
2475
+ .bf_getbuffer = unicode_getbuffer ,
2476
+ /* No need to release the buffer */
2477
+ };
2478
+
2479
+
2480
+ /**begin repeat
2481
+ * #name = datetime, timedelta#
2482
+ * #Name = Datetime, Timedelta#
2483
+ */
2484
+
2485
+ static int
2486
+ @name @_getbuffer (PyObject * self , Py_buffer * view , int flags )
2487
+ {
2488
+ if ((flags & PyBUF_WRITEABLE ) == PyBUF_WRITEABLE ) {
2489
+ return -1 ;
2490
+ }
2491
+ Py @Name @ScalarObject * scalar = (Py @Name @ScalarObject * )self ;
2492
+
2493
+ view -> ndim = 1 ;
2494
+ view -> len = 8 ;
2495
+ view -> itemsize = 1 ;
2496
+ static Py_ssize_t length = 8 ;
2497
+ view -> shape = & length ;
2498
+ view -> strides = NULL ;
2499
+ view -> suboffsets = NULL ;
2500
+ Py_INCREF (self );
2501
+ view -> obj = self ;
2502
+
2503
+ view -> buf = & (scalar -> obval );
2504
+
2505
+ if ((flags & PyBUF_FORMAT ) != PyBUF_FORMAT ) {
2506
+ /* It is unnecessary to find the correct format */
2507
+ view -> format = NULL ;
2508
+ return 0 ;
2509
+ }
2510
+
2511
+ /* export datetime scalars as bytes (although arrays are not exported) */
2512
+ view -> format = "B" ;
2513
+
2514
+ return 0 ;
2515
+ }
2516
+
2517
+ static PyBufferProcs @name @_arrtype_as_buffer = {
2518
+ .bf_getbuffer = @name @_getbuffer ,
2519
+ /* No need to release the buffer */
2520
+ };
2521
+
2522
+ /**end repeat**/
2523
+
2524
+ static PyBufferProcs void_arrtype_as_buffer = {
2525
+ .bf_getbuffer = void_getbuffer , /* defined in buffer.c */
2526
+ /* No need to release the buffer */
2371
2527
};
2372
2528
2373
2529
@@ -3584,7 +3740,6 @@ initialize_numeric_types(void)
3584
3740
init_basetypes ();
3585
3741
PyGenericArrType_Type .tp_dealloc = (destructor )gentype_dealloc ;
3586
3742
PyGenericArrType_Type .tp_as_number = & gentype_as_number ;
3587
- PyGenericArrType_Type .tp_as_buffer = & gentype_as_buffer ;
3588
3743
PyGenericArrType_Type .tp_as_mapping = & gentype_as_mapping ;
3589
3744
PyGenericArrType_Type .tp_flags = BASEFLAGS ;
3590
3745
PyGenericArrType_Type .tp_methods = gentype_methods ;
@@ -3668,10 +3823,15 @@ initialize_numeric_types(void)
3668
3823
Py @NAME @ArrType_Type .tp_new = @name @_arrtype_new ;
3669
3824
Py @NAME @ArrType_Type .tp_richcompare = gentype_richcompare ;
3670
3825
3826
+ #define _IS_ @NAME@ /* inherit string buffer */
3827
+ #if !defined(_IS_String )
3828
+ Py @NAME @ArrType_Type .tp_as_buffer = & @name @_arrtype_as_buffer ;
3829
+ #endif
3830
+ #undef _IS_@NAME@
3831
+
3671
3832
/**end repeat**/
3672
3833
3673
3834
PyUnicodeArrType_Type .tp_dealloc = unicode_arrtype_dealloc ;
3674
- PyUnicodeArrType_Type .tp_as_buffer = & gentype_as_buffer ;
3675
3835
3676
3836
/**begin repeat
3677
3837
* #name = bool, byte, short, ubyte, ushort, uint, ulong, ulonglong,
0 commit comments