@@ -83,7 +83,7 @@ typedef struct {
83
83
int depth ;
84
84
PyObject * str ;
85
85
char * ptr ;
86
- char * end ;
86
+ const char * end ;
87
87
char * buf ;
88
88
_Py_hashtable_t * hashtable ;
89
89
int version ;
@@ -114,7 +114,7 @@ w_reserve(WFILE *p, Py_ssize_t needed)
114
114
}
115
115
assert (p -> str != NULL );
116
116
pos = p -> ptr - p -> buf ;
117
- size = PyBytes_Size (p -> str );
117
+ size = PyBytes_GET_SIZE (p -> str );
118
118
if (size > 16 * 1024 * 1024 )
119
119
delta = (size >> 3 ); /* 12.5% overallocation */
120
120
else
@@ -126,7 +126,7 @@ w_reserve(WFILE *p, Py_ssize_t needed)
126
126
}
127
127
size += delta ;
128
128
if (_PyBytes_Resize (& p -> str , size ) != 0 ) {
129
- p -> ptr = p -> buf = p -> end = NULL ;
129
+ p -> end = p -> ptr = p -> buf = NULL ;
130
130
return 0 ;
131
131
}
132
132
else {
@@ -138,7 +138,7 @@ w_reserve(WFILE *p, Py_ssize_t needed)
138
138
}
139
139
140
140
static void
141
- w_string (const char * s , Py_ssize_t n , WFILE * p )
141
+ w_string (const void * s , Py_ssize_t n , WFILE * p )
142
142
{
143
143
Py_ssize_t m ;
144
144
if (!n || p -> ptr == NULL )
@@ -194,14 +194,14 @@ w_long(long x, WFILE *p)
194
194
#endif
195
195
196
196
static void
197
- w_pstring (const char * s , Py_ssize_t n , WFILE * p )
197
+ w_pstring (const void * s , Py_ssize_t n , WFILE * p )
198
198
{
199
199
W_SIZE (n , p );
200
200
w_string (s , n , p );
201
201
}
202
202
203
203
static void
204
- w_short_pstring (const char * s , Py_ssize_t n , WFILE * p )
204
+ w_short_pstring (const void * s , Py_ssize_t n , WFILE * p )
205
205
{
206
206
w_byte (Py_SAFE_DOWNCAST (n , Py_ssize_t , unsigned char ), p );
207
207
w_string (s , n , p );
@@ -274,21 +274,18 @@ w_float_bin(double v, WFILE *p)
274
274
p -> error = WFERR_UNMARSHALLABLE ;
275
275
return ;
276
276
}
277
- w_string (( const char * ) buf , 8 , p );
277
+ w_string (buf , 8 , p );
278
278
}
279
279
280
280
static void
281
281
w_float_str (double v , WFILE * p )
282
282
{
283
- int n ;
284
283
char * buf = PyOS_double_to_string (v , 'g' , 17 , 0 , NULL );
285
284
if (!buf ) {
286
285
p -> error = WFERR_NOMEMORY ;
287
286
return ;
288
287
}
289
- n = (int )strlen (buf );
290
- w_byte (n , p );
291
- w_string (buf , n , p );
288
+ w_short_pstring (buf , strlen (buf ), p );
292
289
PyMem_Free (buf );
293
290
}
294
291
@@ -378,11 +375,10 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
378
375
Py_ssize_t i , n ;
379
376
380
377
if (PyLong_CheckExact (v )) {
381
- long x = PyLong_AsLong (v );
382
- if ((x == -1 ) && PyErr_Occurred ()) {
383
- PyLongObject * ob = (PyLongObject * )v ;
384
- PyErr_Clear ();
385
- w_PyLong (ob , flag , p );
378
+ int overflow ;
379
+ long x = PyLong_AsLongAndOverflow (v , & overflow );
380
+ if (overflow ) {
381
+ w_PyLong ((PyLongObject * )v , flag , p );
386
382
}
387
383
else {
388
384
#if SIZEOF_LONG > 4
@@ -433,15 +429,15 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
433
429
W_TYPE (TYPE_SHORT_ASCII_INTERNED , p );
434
430
else
435
431
W_TYPE (TYPE_SHORT_ASCII , p );
436
- w_short_pstring (( char * ) PyUnicode_1BYTE_DATA (v ),
432
+ w_short_pstring (PyUnicode_1BYTE_DATA (v ),
437
433
PyUnicode_GET_LENGTH (v ), p );
438
434
}
439
435
else {
440
436
if (PyUnicode_CHECK_INTERNED (v ))
441
437
W_TYPE (TYPE_ASCII_INTERNED , p );
442
438
else
443
439
W_TYPE (TYPE_ASCII , p );
444
- w_pstring (( char * ) PyUnicode_1BYTE_DATA (v ),
440
+ w_pstring (PyUnicode_1BYTE_DATA (v ),
445
441
PyUnicode_GET_LENGTH (v ), p );
446
442
}
447
443
}
@@ -462,7 +458,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
462
458
}
463
459
}
464
460
else if (PyTuple_CheckExact (v )) {
465
- n = PyTuple_Size (v );
461
+ n = PyTuple_GET_SIZE (v );
466
462
if (p -> version >= 4 && n < 256 ) {
467
463
W_TYPE (TYPE_SMALL_TUPLE , p );
468
464
w_byte ((unsigned char )n , p );
@@ -496,34 +492,18 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
496
492
w_object ((PyObject * )NULL , p );
497
493
}
498
494
else if (PyAnySet_CheckExact (v )) {
499
- PyObject * value , * it ;
495
+ PyObject * value ;
496
+ Py_ssize_t pos = 0 ;
497
+ Py_hash_t hash ;
500
498
501
- if (PyObject_TypeCheck (v , & PySet_Type ))
502
- W_TYPE (TYPE_SET , p );
503
- else
499
+ if (PyFrozenSet_CheckExact (v ))
504
500
W_TYPE (TYPE_FROZENSET , p );
505
- n = PyObject_Size (v );
506
- if (n == -1 ) {
507
- p -> depth -- ;
508
- p -> error = WFERR_UNMARSHALLABLE ;
509
- return ;
510
- }
501
+ else
502
+ W_TYPE (TYPE_SET , p );
503
+ n = PySet_GET_SIZE (v );
511
504
W_SIZE (n , p );
512
- it = PyObject_GetIter (v );
513
- if (it == NULL ) {
514
- p -> depth -- ;
515
- p -> error = WFERR_UNMARSHALLABLE ;
516
- return ;
517
- }
518
- while ((value = PyIter_Next (it )) != NULL ) {
505
+ while (_PySet_NextEntry (v , & pos , & value , & hash )) {
519
506
w_object (value , p );
520
- Py_DECREF (value );
521
- }
522
- Py_DECREF (it );
523
- if (PyErr_Occurred ()) {
524
- p -> depth -- ;
525
- p -> error = WFERR_UNMARSHALLABLE ;
526
- return ;
527
507
}
528
508
}
529
509
else if (PyCode_Check (v )) {
@@ -638,8 +618,8 @@ typedef struct {
638
618
FILE * fp ;
639
619
int depth ;
640
620
PyObject * readable ; /* Stream-like object being read from */
641
- char * ptr ;
642
- char * end ;
621
+ const char * ptr ;
622
+ const char * end ;
643
623
char * buf ;
644
624
Py_ssize_t buf_size ;
645
625
PyObject * refs ; /* a list */
@@ -652,7 +632,7 @@ r_string(Py_ssize_t n, RFILE *p)
652
632
653
633
if (p -> ptr != NULL ) {
654
634
/* Fast path for loads() */
655
- char * res = p -> ptr ;
635
+ const char * res = p -> ptr ;
656
636
Py_ssize_t left = p -> end - p -> ptr ;
657
637
if (left < n ) {
658
638
PyErr_SetString (PyExc_EOFError ,
@@ -1564,8 +1544,8 @@ PyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len)
1564
1544
PyObject * result ;
1565
1545
rf .fp = NULL ;
1566
1546
rf .readable = NULL ;
1567
- rf .ptr = ( char * ) str ;
1568
- rf .end = ( char * ) str + len ;
1547
+ rf .ptr = str ;
1548
+ rf .end = str + len ;
1569
1549
rf .buf = NULL ;
1570
1550
rf .depth = 0 ;
1571
1551
rf .refs = PyList_New (0 );
@@ -1587,8 +1567,8 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
1587
1567
wf .str = PyBytes_FromStringAndSize ((char * )NULL , 50 );
1588
1568
if (wf .str == NULL )
1589
1569
return NULL ;
1590
- wf .ptr = wf .buf = PyBytes_AS_STRING (( PyBytesObject * ) wf .str );
1591
- wf .end = wf .ptr + PyBytes_Size (wf .str );
1570
+ wf .ptr = wf .buf = PyBytes_AS_STRING (wf .str );
1571
+ wf .end = wf .ptr + PyBytes_GET_SIZE (wf .str );
1592
1572
wf .error = WFERR_OK ;
1593
1573
wf .version = version ;
1594
1574
if (w_init_refs (& wf , version )) {
@@ -1598,13 +1578,7 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
1598
1578
w_object (x , & wf );
1599
1579
w_clear_refs (& wf );
1600
1580
if (wf .str != NULL ) {
1601
- char * base = PyBytes_AS_STRING ((PyBytesObject * )wf .str );
1602
- if (wf .ptr - base > PY_SSIZE_T_MAX ) {
1603
- Py_DECREF (wf .str );
1604
- PyErr_SetString (PyExc_OverflowError ,
1605
- "too much marshal data for a bytes object" );
1606
- return NULL ;
1607
- }
1581
+ const char * base = PyBytes_AS_STRING (wf .str );
1608
1582
if (_PyBytes_Resize (& wf .str , (Py_ssize_t )(wf .ptr - base )) < 0 )
1609
1583
return NULL ;
1610
1584
}
0 commit comments