Skip to content

Commit 3848c2e

Browse files
author
mwh
committed
This is my patch
[ 587993 ] SET_LINENO killer Remove SET_LINENO. Tracing is now supported by inspecting co_lnotab. Many sundry changes to document and adapt to this change. git-svn-id: http://svn.python.org/projects/python/trunk@28249 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 2279986 commit 3848c2e

19 files changed

Lines changed: 341 additions & 187 deletions

File tree

Doc/lib/libdis.tex

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ \section{\module{dis} ---
2323

2424
\begin{verbatim}
2525
>>> dis.dis(myfunc)
26-
0 SET_LINENO 1
27-
28-
3 SET_LINENO 2
29-
6 LOAD_GLOBAL 0 (len)
30-
9 LOAD_FAST 0 (alist)
31-
12 CALL_FUNCTION 1
32-
15 RETURN_VALUE
33-
16 LOAD_CONST 0 (None)
34-
19 RETURN_VALUE
26+
2 0 LOAD_GLOBAL 0 (len)
27+
3 LOAD_FAST 0 (alist)
28+
6 CALL_FUNCTION 1
29+
9 RETURN_VALUE
30+
10 RETURN_NONE
3531
\end{verbatim}
3632

33+
(The ``2'' is a line number).
34+
3735
The \module{dis} module defines the following functions and constants:
3836

3937
\begin{funcdesc}{dis}{\optional{bytesource}}
@@ -56,6 +54,7 @@ \section{\module{dis} ---
5654
was provided. The output is divided in the following columns:
5755

5856
\begin{enumerate}
57+
\item the line number, for the first instruction of each line
5958
\item the current instruction, indicated as \samp{-->},
6059
\item a labelled instruction, indicated with \samp{>\code{>}},
6160
\item the address of the instruction,
@@ -402,6 +401,14 @@ \subsection{Python Byte Code Instructions}
402401
Returns with TOS to the caller of the function.
403402
\end{opcodedesc}
404403

404+
\begin{opcodedesc}{RETURN_NONE}{}
405+
Returns \constant{None} to the caller of the function. This opcode is
406+
generated as the last opcode of every function and only then, for
407+
reasons to do with tracing support. See the comments in the function
408+
\cfunction{maybe_call_line_trace} in \file{Python/ceval.c} for the
409+
gory details. \versionadded{2.3}.
410+
\end{opcodedesc}
411+
405412
\begin{opcodedesc}{YIELD_VALUE}{}
406413
Pops \code{TOS} and yields it from a generator.
407414
\end{opcodedesc}
@@ -621,7 +628,7 @@ \subsection{Python Byte Code Instructions}
621628
\end{opcodedesc}
622629

623630
\begin{opcodedesc}{SET_LINENO}{lineno}
624-
Sets the current line number to \var{lineno}.
631+
This opcode is obsolete.
625632
\end{opcodedesc}
626633

627634
\begin{opcodedesc}{RAISE_VARARGS}{argc}

Doc/lib/libtraceback.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ \section{\module{traceback} ---
118118

119119
\begin{funcdesc}{tb_lineno}{tb}
120120
This function returns the current line number set in the traceback
121-
object. This is normally the same as the \code{\var{tb}.tb_lineno}
122-
field of the object, but when optimization is used (the -O flag) this
123-
field is not updated correctly; this function calculates the correct
124-
value.
121+
object. This function was necessary because in versions of Python
122+
prior to 2.3 when the \programopt{O} flag was passed to Python the
123+
\code{\var{tb}.tb_lineno} was not updated correctly. This function
124+
has no use in versions past 2.3.
125125
\end{funcdesc}
126126

127127

Doc/tut/tut.tex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,12 +2340,11 @@ \subsection{``Compiled'' Python files}
23402340

23412341
\item
23422342
When the Python interpreter is invoked with the \programopt{-O} flag,
2343-
optimized code is generated and stored in \file{.pyo} files.
2344-
The optimizer currently doesn't help much; it only removes
2345-
\keyword{assert} statements and \code{SET_LINENO} instructions.
2346-
When \programopt{-O} is used, \emph{all} bytecode is optimized;
2347-
\code{.pyc} files are ignored and \code{.py} files are compiled to
2348-
optimized bytecode.
2343+
optimized code is generated and stored in \file{.pyo} files. The
2344+
optimizer currently doesn't help much; it only removes
2345+
\keyword{assert} statements. When \programopt{-O} is used, \emph{all}
2346+
bytecode is optimized; \code{.pyc} files are ignored and \code{.py}
2347+
files are compiled to optimized bytecode.
23492348

23502349
\item
23512350
Passing two \programopt{-O} flags to the Python interpreter

Doc/whatsnew/whatsnew23.tex

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ \section{Other Language Changes}
658658

659659
\end{itemize}
660660

661-
662661
%======================================================================
663662
\section{New and Improved Modules}
664663

@@ -987,9 +986,9 @@ \section{Build and C API Changes}
987986
when running Python's \file{configure} script. (Contributed by Ondrej
988987
Palkovsky.)
989988

990-
\item The \csimplemacro{DL_EXPORT} and \csimplemacro{DL_IMPORT} macros are now
991-
deprecated. Initialization functions for Python extension modules
992-
should now be declared using the new macro
989+
\item The \csimplemacro{DL_EXPORT} and \csimplemacro{DL_IMPORT} macros
990+
are now deprecated. Initialization functions for Python extension
991+
modules should now be declared using the new macro
993992
\csimplemacro{PyMODINIT_FUNC}, while the Python core will generally
994993
use the \csimplemacro{PyAPI_FUNC} and \csimplemacro{PyAPI_DATA}
995994
macros.
@@ -1076,6 +1075,29 @@ \section{Other Changes and Fixes}
10761075
\item The tools used to build the documentation now work under Cygwin
10771076
as well as \UNIX.
10781077

1078+
\item The \code{SET_LINENO} opcode has been removed. Back in the
1079+
mists of time, this opcode was needed to produce line numbers in
1080+
tracebacks and support trace functions (for, e.g., \module{pdb}).
1081+
Since Python 1.5, the line numbers in tracebacks have been computed
1082+
using a different mechanism that works with ``python -O''. For Python
1083+
2.3 Michael Hudson implemented a similar scheme to determine when to
1084+
call the trace function, removing the need for \code{SET_LINENO}
1085+
entirely.
1086+
1087+
Python code will be hard pushed to notice a difference from this
1088+
change, apart from a slight speed up when python is run without
1089+
\programopt{-O}.
1090+
1091+
C extensions that access the \member{f_lineno} field of frame objects
1092+
should instead call \code{PyCode_Addr2Line(f->f_code, f->f_lasti)}.
1093+
This will have the added effect of making the code work as desired
1094+
under ``python -O'' in earlier versions of Python.
1095+
1096+
To make tracing work as expected, it was found necessary to add a new
1097+
opcode, \cdata{RETURN_NONE}, to the VM. If you want to know why, read
1098+
the comments in the function \cfunction{maybe_call_line_trace} in
1099+
\file{Python/ceval.c}.
1100+
10791101
\end{itemize}
10801102

10811103

Include/opcode.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ extern "C" {
7171
#define INPLACE_OR 79
7272
#define BREAK_LOOP 80
7373

74+
#define RETURN_NONE 81 /* *only* for function epilogues
75+
-- see comments in
76+
ceval.c:maybe_call_line_trace for why */
7477
#define LOAD_LOCALS 82
7578
#define RETURN_VALUE 83
7679
#define IMPORT_STAR 84
@@ -119,8 +122,6 @@ extern "C" {
119122
#define STORE_FAST 125 /* Local variable number */
120123
#define DELETE_FAST 126 /* Local variable number */
121124

122-
#define SET_LINENO 127 /* Current line number */
123-
124125
#define RAISE_VARARGS 130 /* Number of raise arguments (1, 2 or 3) */
125126
/* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
126127
#define CALL_FUNCTION 131 /* #args + (#kwargs<<8) */

Lib/dis.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ def distb(tb=None):
5555
def disassemble(co, lasti=-1):
5656
"""Disassemble a code object."""
5757
code = co.co_code
58+
59+
byte_increments = [ord(c) for c in co.co_lnotab[0::2]]
60+
line_increments = [ord(c) for c in co.co_lnotab[1::2]]
61+
table_length = len(byte_increments) # == len(line_increments)
62+
63+
lineno = co.co_firstlineno
64+
table_index = 0
65+
while (table_index < table_length
66+
and byte_increments[table_index] == 0):
67+
lineno += line_increments[table_index]
68+
table_index += 1
69+
addr = 0
70+
line_incr = 0
71+
5872
labels = findlabels(code)
5973
n = len(code)
6074
i = 0
@@ -63,7 +77,23 @@ def disassemble(co, lasti=-1):
6377
while i < n:
6478
c = code[i]
6579
op = ord(c)
66-
if op == SET_LINENO and i > 0: print # Extra blank line
80+
81+
if i >= addr:
82+
lineno += line_incr
83+
while table_index < table_length:
84+
addr += byte_increments[table_index]
85+
line_incr = line_increments[table_index]
86+
table_index += 1
87+
if line_incr:
88+
break
89+
else:
90+
addr = sys.maxint
91+
if i > 0:
92+
print
93+
print "%3d"%lineno,
94+
else:
95+
print ' ',
96+
6797
if i == lasti: print '-->',
6898
else: print ' ',
6999
if i in labels: print '>>',
@@ -224,6 +254,7 @@ def jabs_op(name, op):
224254
def_op('INPLACE_OR', 79)
225255
def_op('BREAK_LOOP', 80)
226256

257+
def_op('RETURN_NONE', 81)
227258
def_op('LOAD_LOCALS', 82)
228259
def_op('RETURN_VALUE', 83)
229260
def_op('IMPORT_STAR', 84)
@@ -277,9 +308,6 @@ def jabs_op(name, op):
277308
def_op('DELETE_FAST', 126) # Local variable number
278309
haslocal.append(126)
279310

280-
def_op('SET_LINENO', 127) # Current line number
281-
SET_LINENO = 127
282-
283311
def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
284312
def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
285313
def_op('MAKE_FUNCTION', 132) # Number of args with default values

Lib/inspect.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def getframeinfo(frame, context=1):
711711
raise TypeError, 'arg is not a frame or traceback object'
712712

713713
filename = getsourcefile(frame) or getfile(frame)
714-
lineno = getlineno(frame)
714+
lineno = frame.f_lineno
715715
if context > 0:
716716
start = lineno - 1 - context//2
717717
try:
@@ -730,18 +730,8 @@ def getframeinfo(frame, context=1):
730730

731731
def getlineno(frame):
732732
"""Get the line number from a frame object, allowing for optimization."""
733-
# Written by Marc-André Lemburg; revised by Jim Hugunin and Fredrik Lundh.
734-
lineno = frame.f_lineno
735-
code = frame.f_code
736-
if hasattr(code, 'co_lnotab'):
737-
table = code.co_lnotab
738-
lineno = code.co_firstlineno
739-
addr = 0
740-
for i in range(0, len(table), 2):
741-
addr = addr + ord(table[i])
742-
if addr > frame.f_lasti: break
743-
lineno = lineno + ord(table[i+1])
744-
return lineno
733+
# FrameType.f_lineno is now a descriptor that grovels co_lnotab
734+
return frame.f_lineno
745735

746736
def getouterframes(frame, context=1):
747737
"""Get a list of records for a frame and all higher (calling) frames.

Lib/pdb.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ def execRcLines(self):
105105
if len(line) > 0 and line[0] != '#':
106106
self.onecmd(line)
107107

108-
# Override Bdb methods (except user_call, for now)
108+
# Override Bdb methods
109+
110+
def user_call(self, frame, argument_list):
111+
"""This method is called when there is the remote possibility
112+
that we ever need to stop in this function."""
113+
print '--Call--'
114+
self.interaction(frame, None)
109115

110116
def user_line(self, frame):
111117
"""This function is called when we stop or break at this line."""

Lib/test/test_hotshot.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ def g():
9191
f_lineno = f.func_code.co_firstlineno
9292
g_lineno = g.func_code.co_firstlineno
9393
events = [(ENTER, ("test_hotshot", g_lineno, "g")),
94-
(LINE, ("test_hotshot", g_lineno, "g")),
9594
(LINE, ("test_hotshot", g_lineno+1, "g")),
9695
(ENTER, ("test_hotshot", f_lineno, "f")),
97-
(LINE, ("test_hotshot", f_lineno, "f")),
9896
(LINE, ("test_hotshot", f_lineno+1, "f")),
9997
(LINE, ("test_hotshot", f_lineno+2, "f")),
10098
(EXIT, ("test_hotshot", f_lineno, "f")),

Lib/traceback.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def print_tb(tb, limit=None, file=None):
5959
n = 0
6060
while tb is not None and (limit is None or n < limit):
6161
f = tb.tb_frame
62-
lineno = tb_lineno(tb)
62+
lineno = tb.tb_lineno
6363
co = f.f_code
6464
filename = co.co_filename
6565
name = co.co_name
@@ -92,7 +92,7 @@ def extract_tb(tb, limit = None):
9292
n = 0
9393
while tb is not None and (limit is None or n < limit):
9494
f = tb.tb_frame
95-
lineno = tb_lineno(tb)
95+
lineno = tb.tb_lineno
9696
co = f.f_code
9797
filename = co.co_filename
9898
name = co.co_name
@@ -263,7 +263,7 @@ def extract_stack(f=None, limit = None):
263263
list = []
264264
n = 0
265265
while f is not None and (limit is None or n < limit):
266-
lineno = f.f_lineno # XXX Too bad if -O is used
266+
lineno = f.f_lineno
267267
co = f.f_code
268268
filename = co.co_filename
269269
name = co.co_name
@@ -279,23 +279,6 @@ def extract_stack(f=None, limit = None):
279279
def tb_lineno(tb):
280280
"""Calculate correct line number of traceback given in tb.
281281
282-
Even works with -O on.
282+
Obsolete in 2.3.
283283
"""
284-
# Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
285-
# in compile.c.
286-
# Revised version by Jim Hugunin to work with JPython too.
287-
288-
c = tb.tb_frame.f_code
289-
if not hasattr(c, 'co_lnotab'):
290-
return tb.tb_lineno
291-
292-
tab = c.co_lnotab
293-
line = c.co_firstlineno
294-
stopat = tb.tb_lasti
295-
addr = 0
296-
for i in range(0, len(tab), 2):
297-
addr = addr + ord(tab[i])
298-
if addr > stopat:
299-
break
300-
line = line + ord(tab[i+1])
301-
return line
284+
return tb.tb_lineno

0 commit comments

Comments
 (0)