@@ -1405,11 +1405,14 @@ r_object(RFILE *p)
1405
1405
PyObject * columntable = NULL ;
1406
1406
PyObject * exceptiontable = NULL ;
1407
1407
struct _PyCodeConstructor con = { 0 }; // All zeros
1408
+ Py_ssize_t first_ref = -1 ;
1408
1409
1410
+ assert (flag == 0 ); // We don't handle references to code objects
1409
1411
v = NULL ;
1410
1412
1411
1413
/* XXX ignore long->int overflows for now */
1412
1414
const char * save_ptr = p -> ptr ;
1415
+ Py_ssize_t save_refs_pos = p -> refs_pos ;
1413
1416
1414
1417
datasize = (int )r_long (p );
1415
1418
if (PyErr_Occurred ())
@@ -1422,9 +1425,8 @@ r_object(RFILE *p)
1422
1425
if (PyErr_Occurred ())
1423
1426
goto code_error ;
1424
1427
posonlyargcount = (int )r_long (p );
1425
- if (PyErr_Occurred ()) {
1428
+ if (PyErr_Occurred ())
1426
1429
goto code_error ;
1427
- }
1428
1430
kwonlyargcount = (int )r_long (p );
1429
1431
if (PyErr_Occurred ())
1430
1432
goto code_error ;
@@ -1434,10 +1436,10 @@ r_object(RFILE *p)
1434
1436
flags = (int )r_long (p );
1435
1437
if (PyErr_Occurred ())
1436
1438
goto code_error ;
1437
-
1438
1439
firstlineno = (int )r_long (p );
1439
- if (firstlineno == -1 && PyErr_Occurred ())
1440
- break ;
1440
+ if (PyErr_Occurred ())
1441
+ goto code_error ;
1442
+
1441
1443
name = r_object (p );
1442
1444
if (name == NULL )
1443
1445
goto code_error ;
@@ -1461,14 +1463,21 @@ r_object(RFILE *p)
1461
1463
PyCodeObject * to_update = NULL ;
1462
1464
1463
1465
if (p -> ctx != NULL && p -> ctx -> code == NULL ) {
1464
- assert (nrefs == 0 );
1465
- p -> ptr = save_ptr + datasize ;
1466
-
1466
+ // Return a dehydrated code object
1467
1467
con .hydra_context = p -> ctx ;
1468
1468
con .hydra_offset = save_ptr - 1 - p -> ctx -> buf ; // Back up over typecode
1469
+ con .hydra_refs_pos = save_refs_pos ;
1470
+ p -> ptr = save_ptr + datasize ;
1471
+ while (p -> refs_pos < save_refs_pos + nrefs ) {
1472
+ Py_ssize_t ref = r_ref_reserve (FLAG_REF , p );
1473
+ if (first_ref < 0 )
1474
+ first_ref = ref ;
1475
+ }
1476
+ // We'll call r_ref_insert() below
1469
1477
}
1470
1478
else {
1471
1479
if (p -> ctx != NULL && p -> ctx -> code != NULL ) {
1480
+ // Rehydrating
1472
1481
to_update = p -> ctx -> code ;
1473
1482
p -> ctx -> code = NULL ;
1474
1483
}
@@ -1511,7 +1520,7 @@ r_object(RFILE *p)
1511
1520
con .exceptiontable = exceptiontable ;
1512
1521
1513
1522
if (_PyCode_Validate (& con ) < 0 ) {
1514
- printf ("Failed to validate\n" );
1523
+ printf ("Failed to validate code object \n" ); // TODO: delete
1515
1524
goto code_error ;
1516
1525
}
1517
1526
};
@@ -1523,11 +1532,17 @@ r_object(RFILE *p)
1523
1532
v = (PyObject * )_PyCode_New (& con );
1524
1533
}
1525
1534
if (v == NULL ) {
1526
- printf ("Failed to create\n" );
1535
+ printf ("Failed to create code object \n" ); // TODO: delete
1527
1536
goto code_error ;
1528
1537
}
1529
1538
1530
- v = r_ref_insert (v , idx , flag , p ); // TODO: NO
1539
+ if (first_ref >= 0 ) {
1540
+ // Overwrite skipped references with v
1541
+ assert (p -> ctx != NULL && p -> ctx -> code == NULL );
1542
+ while (first_ref < p -> refs_pos ) {
1543
+ r_ref_insert (v , first_ref ++ , FLAG_REF , p );
1544
+ }
1545
+ }
1531
1546
1532
1547
code_error :
1533
1548
Py_XDECREF (code );
@@ -1960,6 +1975,7 @@ _PyCode_Hydrate(PyCodeObject *code)
1960
1975
rf .end = s + n ;
1961
1976
rf .depth = 0 ;
1962
1977
rf .refs = ctx -> refs ;
1978
+ rf .refs_pos = code -> co_hydra_refs_pos ;
1963
1979
rf .ctx = ctx ;
1964
1980
ctx -> code = code ;
1965
1981
0 commit comments