Skip to content

Commit 4a0c7a1

Browse files
authored
bpo-45316: Move private PyCode C API to internal C API (GH-31576)
Rename private functions (no exported), add an underscore prefix: * PyLineTable_InitAddressRange() => _PyLineTable_InitAddressRange() * PyLineTable_NextAddressRange() => _PyLineTable_NextAddressRange() * PyLineTable_PreviousAddressRange() => _PyLineTable_PreviousAddressRange() Move private functions to the internal C API: * _PyCode_Addr2EndLine() * _PyCode_Addr2EndOffset() * _PyCode_Addr2Offset() * _PyCode_InitAddressRange() * _PyCode_InitEndAddressRange( * _PyLineTable_InitAddressRange() * _PyLineTable_NextAddressRange() * _PyLineTable_PreviousAddressRange() No longer export the following internal functions: * _PyCode_GetVarnames() * _PyCode_GetCellvars() * _PyCode_GetFreevars() * _Py_GetSpecializationStats() Add "extern" to pycore_code.h functions to identify them more easiliy (they are still not exported).
1 parent c579243 commit 4a0c7a1

File tree

4 files changed

+56
-49
lines changed

4 files changed

+56
-49
lines changed

Include/cpython/code.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,6 @@ PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
174174

175175
PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *);
176176

177-
/* Return the ending source code line number from a bytecode index. */
178-
PyAPI_FUNC(int) _PyCode_Addr2EndLine(PyCodeObject *, int);
179-
/* Return the starting source code column offset from a bytecode index. */
180-
PyAPI_FUNC(int) _PyCode_Addr2Offset(PyCodeObject *, int);
181-
/* Return the ending source code column offset from a bytecode index. */
182-
PyAPI_FUNC(int) _PyCode_Addr2EndOffset(PyCodeObject *, int);
183-
184177
/* for internal use only */
185178
struct _opaque {
186179
int computed_line;
@@ -217,15 +210,3 @@ PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index,
217210
void **extra);
218211
PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index,
219212
void *extra);
220-
221-
/** API for initializing the line number tables. */
222-
int _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds);
223-
int _PyCode_InitEndAddressRange(PyCodeObject* co, PyCodeAddressRange* bounds);
224-
225-
/** Out of process API for initializing the line number table. */
226-
void PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range);
227-
228-
/** API for traversing the line number table. */
229-
int PyLineTable_NextAddressRange(PyCodeAddressRange *range);
230-
int PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
231-

Include/internal/pycore_code.h

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,35 @@ PyAPI_FUNC(PyCodeObject *) _PyCode_New(struct _PyCodeConstructor *);
259259
/* Private API */
260260

261261
/* Getters for internal PyCodeObject data. */
262-
PyAPI_FUNC(PyObject *) _PyCode_GetVarnames(PyCodeObject *);
263-
PyAPI_FUNC(PyObject *) _PyCode_GetCellvars(PyCodeObject *);
264-
PyAPI_FUNC(PyObject *) _PyCode_GetFreevars(PyCodeObject *);
262+
extern PyObject* _PyCode_GetVarnames(PyCodeObject *);
263+
extern PyObject* _PyCode_GetCellvars(PyCodeObject *);
264+
extern PyObject* _PyCode_GetFreevars(PyCodeObject *);
265+
266+
/* Return the ending source code line number from a bytecode index. */
267+
extern int _PyCode_Addr2EndLine(PyCodeObject *, int);
268+
269+
/* Return the ending source code line number from a bytecode index. */
270+
extern int _PyCode_Addr2EndLine(PyCodeObject *, int);
271+
/* Return the starting source code column offset from a bytecode index. */
272+
extern int _PyCode_Addr2Offset(PyCodeObject *, int);
273+
/* Return the ending source code column offset from a bytecode index. */
274+
extern int _PyCode_Addr2EndOffset(PyCodeObject *, int);
275+
276+
/** API for initializing the line number tables. */
277+
extern int _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds);
278+
extern int _PyCode_InitEndAddressRange(PyCodeObject* co, PyCodeAddressRange* bounds);
279+
280+
/** Out of process API for initializing the line number table. */
281+
extern void _PyLineTable_InitAddressRange(
282+
const char *linetable,
283+
Py_ssize_t length,
284+
int firstlineno,
285+
PyCodeAddressRange *range);
286+
287+
/** API for traversing the line number table. */
288+
extern int _PyLineTable_NextAddressRange(PyCodeAddressRange *range);
289+
extern int _PyLineTable_PreviousAddressRange(PyCodeAddressRange *range);
290+
265291

266292
#define ADAPTIVE_CACHE_BACKOFF 64
267293

@@ -272,26 +298,26 @@ cache_backoff(_PyAdaptiveEntry *entry) {
272298

273299
/* Specialization functions */
274300

275-
int _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
276-
int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
277-
int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
278-
int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
279-
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
280-
int _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, _Py_CODEUNIT *instr);
281-
int _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
301+
extern int _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
302+
extern int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
303+
extern int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
304+
extern int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
305+
extern int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
306+
extern int _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, _Py_CODEUNIT *instr);
307+
extern int _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
282308
PyObject *kwnames, SpecializedCacheEntry *cache);
283-
int _Py_Specialize_Precall(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
309+
extern int _Py_Specialize_Precall(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
284310
PyObject *kwnames, SpecializedCacheEntry *cache, PyObject *builtins);
285-
void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
311+
extern void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
286312
int oparg);
287-
void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
288-
void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
313+
extern void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
314+
extern void _Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
289315
SpecializedCacheEntry *cache);
290316

291317
/* Deallocator function for static codeobjects used in deepfreeze.py */
292-
void _PyStaticCode_Dealloc(PyCodeObject *co);
318+
extern void _PyStaticCode_Dealloc(PyCodeObject *co);
293319
/* Function to intern strings of codeobjects */
294-
void _PyStaticCode_InternStrings(PyCodeObject *co);
320+
extern void _PyStaticCode_InternStrings(PyCodeObject *co);
295321

296322
#ifdef Py_STATS
297323

@@ -343,9 +369,9 @@ extern PyStats _py_stats;
343369
#define CALL_STAT_INC(name) _py_stats.call_stats.name++
344370
#define OBJECT_STAT_INC(name) _py_stats.object_stats.name++
345371

346-
void _Py_PrintSpecializationStats(int to_file);
372+
extern void _Py_PrintSpecializationStats(int to_file);
347373

348-
PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
374+
extern PyObject* _Py_GetSpecializationStats(void);
349375

350376
#else
351377
#define STAT_INC(opname, name) ((void)0)

Objects/codeobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ _PyCode_Addr2EndOffset(PyCodeObject* co, int addrq)
676676
}
677677

678678
void
679-
PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range)
679+
_PyLineTable_InitAddressRange(const char *linetable, Py_ssize_t length, int firstlineno, PyCodeAddressRange *range)
680680
{
681681
range->opaque.lo_next = linetable;
682682
range->opaque.limit = range->opaque.lo_next + length;
@@ -691,7 +691,7 @@ _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds)
691691
{
692692
const char *linetable = PyBytes_AS_STRING(co->co_linetable);
693693
Py_ssize_t length = PyBytes_GET_SIZE(co->co_linetable);
694-
PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
694+
_PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
695695
return bounds->ar_line;
696696
}
697697

@@ -700,7 +700,7 @@ _PyCode_InitEndAddressRange(PyCodeObject* co, PyCodeAddressRange* bounds)
700700
{
701701
char* linetable = PyBytes_AS_STRING(co->co_endlinetable);
702702
Py_ssize_t length = PyBytes_GET_SIZE(co->co_endlinetable);
703-
PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
703+
_PyLineTable_InitAddressRange(linetable, length, co->co_firstlineno, bounds);
704704
return bounds->ar_line;
705705
}
706706

@@ -710,12 +710,12 @@ int
710710
_PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds)
711711
{
712712
while (bounds->ar_end <= lasti) {
713-
if (!PyLineTable_NextAddressRange(bounds)) {
713+
if (!_PyLineTable_NextAddressRange(bounds)) {
714714
return -1;
715715
}
716716
}
717717
while (bounds->ar_start > lasti) {
718-
if (!PyLineTable_PreviousAddressRange(bounds)) {
718+
if (!_PyLineTable_PreviousAddressRange(bounds)) {
719719
return -1;
720720
}
721721
}
@@ -765,7 +765,7 @@ at_end(PyCodeAddressRange *bounds) {
765765
}
766766

767767
int
768-
PyLineTable_PreviousAddressRange(PyCodeAddressRange *range)
768+
_PyLineTable_PreviousAddressRange(PyCodeAddressRange *range)
769769
{
770770
if (range->ar_start <= 0) {
771771
return 0;
@@ -779,7 +779,7 @@ PyLineTable_PreviousAddressRange(PyCodeAddressRange *range)
779779
}
780780

781781
int
782-
PyLineTable_NextAddressRange(PyCodeAddressRange *range)
782+
_PyLineTable_NextAddressRange(PyCodeAddressRange *range)
783783
{
784784
if (at_end(range)) {
785785
return 0;
@@ -847,7 +847,7 @@ decode_linetable(PyCodeObject *code)
847847
return NULL;
848848
}
849849
_PyCode_InitAddressRange(code, &bounds);
850-
while (PyLineTable_NextAddressRange(&bounds)) {
850+
while (_PyLineTable_NextAddressRange(&bounds)) {
851851
if (bounds.opaque.computed_line != line) {
852852
int bdelta = bounds.ar_start - code_offset;
853853
int ldelta = bounds.opaque.computed_line - line;
@@ -883,7 +883,7 @@ static PyObject *
883883
lineiter_next(lineiterator *li)
884884
{
885885
PyCodeAddressRange *bounds = &li->li_line;
886-
if (!PyLineTable_NextAddressRange(bounds)) {
886+
if (!_PyLineTable_NextAddressRange(bounds)) {
887887
return NULL;
888888
}
889889
PyObject *start = NULL;

Objects/frameobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,9 @@ marklines(PyCodeObject *code, int len)
377377
linestarts[i] = -1;
378378
}
379379

380-
while (PyLineTable_NextAddressRange(&bounds)) {
381-
assert(bounds.ar_start/(int)sizeof(_Py_CODEUNIT) < len);
382-
linestarts[bounds.ar_start/sizeof(_Py_CODEUNIT)] = bounds.ar_line;
380+
while (_PyLineTable_NextAddressRange(&bounds)) {
381+
assert(bounds.ar_start / (int)sizeof(_Py_CODEUNIT) < len);
382+
linestarts[bounds.ar_start / sizeof(_Py_CODEUNIT)] = bounds.ar_line;
383383
}
384384
return linestarts;
385385
}

0 commit comments

Comments
 (0)