]> granicus.if.org Git - python/commitdiff
Issue 5954, PyFrame_GetLineNumber:
authorJeffrey Yasskin <jyasskin@gmail.com>
Fri, 8 May 2009 22:23:21 +0000 (22:23 +0000)
committerJeffrey Yasskin <jyasskin@gmail.com>
Fri, 8 May 2009 22:23:21 +0000 (22:23 +0000)
Most uses of PyCode_Addr2Line
(http://www.google.com/codesearch?q=PyCode_Addr2Line) are just trying to get
the line number of a specified frame, but there's no way to do that directly.
Forcing people to go through the code object makes them know more about the
guts of the interpreter than they should need.

The remaining uses of PyCode_Addr2Line seem to be getting the line from a
traceback (for example,
http://www.google.com/codesearch/p?hl=en#u_9_nDrchrw/pygame-1.7.1release/src/base.c&q=PyCode_Addr2Line),
which is replaced by the tb_lineno field.  So we may be able to deprecate
PyCode_Addr2Line entirely for external use.

Doc/c-api/reflection.rst
Include/code.h
Include/frameobject.h
Misc/NEWS
Objects/frameobject.c
Python/_warnings.c
Python/ceval.c
Python/traceback.c

index 822c59343ca1b2b8a31e03e13554e4d9b24e4acb..3996c1f18d63d0aac97983a71564034bfba92e93 100644 (file)
@@ -29,6 +29,11 @@ Reflection
    currently executing.
 
 
+.. cfunction:: int PyFrame_GetLineNumber(PyFrameObject *frame)
+
+   Return the line number that *frame* is currently executing.
+
+
 .. cfunction:: int PyEval_GetRestricted()
 
    If there is a current frame and it is executing in restricted mode, return true,
index 0167ad4a878d2dfa98a1bfb918cd35ec23574328..cbf00d8d01d60e78d0948ece712ad57cd9ab7aa8 100644 (file)
@@ -75,6 +75,9 @@ PyAPI_FUNC(PyCodeObject *) PyCode_New(
 PyAPI_FUNC(PyCodeObject *)
 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
 
+/* Return the line number associated with the specified bytecode index
+   in this code object.  If you just need the line number of a frame,
+   use PyFrame_GetLineNumber() instead. */
 PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
 
 /* for internal use only */
index 297b66ddb2d465f2beeadf03e4f175d38da01a0d..17e7679ac870f0cabe67d05b85fc5a42e230f1d1 100644 (file)
@@ -38,8 +38,11 @@ typedef struct _frame {
 
     PyThreadState *f_tstate;
     int f_lasti;               /* Last instruction if called */
-    /* As of 2.3 f_lineno is only valid when tracing is active (i.e. when
-       f_trace is set) -- at other times use PyCode_Addr2Line instead. */
+    /* Call PyFrame_GetLineNumber() instead of reading this field
+       directly.  As of 2.3 f_lineno is only valid when tracing is
+       active (i.e. when f_trace is set).  At other times we use
+       PyCode_Addr2Line to calculate the line from the current
+       bytecode index. */
     int f_lineno;              /* Current line number */
     int f_iblock;              /* index in f_blockstack */
     PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */
@@ -77,6 +80,9 @@ PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
 
 PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
 
+/* Return the line of code the frame is currently executing. */
+PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *);
+
 #ifdef __cplusplus
 }
 #endif
index ba721e729e4698ff0d6ec62c4c19e9b7457c2e01..fa362fd929b458aa7fdc5cde11069bbd2ebede14 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -927,6 +927,9 @@ Build
 C-API
 -----
 
+- Issue #5954: Add a PyFrame_GetLineNumber() function to replace most uses of
+  PyCode_Addr2Line().
+
 - Issue #5959: Add a PyCode_NewEmpty() function to create a new empty code
   object at a specified file, function, and line number.
 
index 4ac1ab02ddf5f9700dfe32315feaba2b5e01f326..7a9d40d8965a0f6513a2dc803b5d86a15335a299 100644 (file)
@@ -60,17 +60,19 @@ frame_getlocals(PyFrameObject *f, void *closure)
        return f->f_locals;
 }
 
-static PyObject *
-frame_getlineno(PyFrameObject *f, void *closure)
+int
+PyFrame_GetLineNumber(PyFrameObject *f)
 {
-       int lineno;
-
        if (f->f_trace)
-               lineno = f->f_lineno;
+               return f->f_lineno;
        else
-               lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+               return PyCode_Addr2Line(f->f_code, f->f_lasti);
+}
 
-       return PyInt_FromLong(lineno);
+static PyObject *
+frame_getlineno(PyFrameObject *f, void *closure)
+{
+       return PyInt_FromLong(PyFrame_GetLineNumber(f));
 }
 
 /* Setter for f_lineno - you can set f_lineno from within a trace function in
@@ -351,16 +353,14 @@ frame_gettrace(PyFrameObject *f, void *closure)
 static int
 frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
 {
-       /* We rely on f_lineno being accurate when f_trace is set. */
+       PyObject* old_value;
 
-       PyObject* old_value = f->f_trace;
+       /* We rely on f_lineno being accurate when f_trace is set. */
+       f->f_lineno = PyFrame_GetLineNumber(f);
 
+       old_value = f->f_trace;
        Py_XINCREF(v);
        f->f_trace = v;
-
-       if (v != NULL)
-               f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
-
        Py_XDECREF(old_value);
 
        return 0;
index 1a7c2d51e6dcd0b36a1f031e3d14731e68a4a1f7..219cfc6ee1909d99b557437f1416469f41005420 100644 (file)
@@ -454,7 +454,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
     }
     else {
         globals = f->f_globals;
-        *lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
+        *lineno = PyFrame_GetLineNumber(f);
     }
 
     *module = NULL;
index 88b9d0e8882bcfdec958c69e6d11d3d6ec51a9aa..474a8851db20d192f5e3eb0977537d1ec62d8523 100644 (file)
@@ -2698,7 +2698,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
                default:
                        fprintf(stderr,
                                "XXX lineno: %d, opcode: %d\n",
-                               PyCode_Addr2Line(f->f_code, f->f_lasti),
+                               PyFrame_GetLineNumber(f),
                                opcode);
                        PyErr_SetString(PyExc_SystemError, "unknown opcode");
                        why = WHY_EXCEPTION;
index c2d7e77a3e563f6659cae565f5d4653b06cd3d2c..1c26ba271abcf75268a34b5cad2b2a224de8df7d 100644 (file)
@@ -96,8 +96,7 @@ newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
                Py_XINCREF(frame);
                tb->tb_frame = frame;
                tb->tb_lasti = frame->f_lasti;
-               tb->tb_lineno = PyCode_Addr2Line(frame->f_code, 
-                                                frame->f_lasti);
+               tb->tb_lineno = PyFrame_GetLineNumber(frame);
                PyObject_GC_Track(tb);
        }
        return tb;