Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7169dbb

Browse files
committed
Move printing of filename and lineno to tb_displayline.
Search sys.path if the filename isn't found. Include osdefs.h.
1 parent 0f61f8a commit 7169dbb

1 file changed

Lines changed: 45 additions & 20 deletions

File tree

Python/traceback.c

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2626

2727
#include "allobjects.h"
2828

29+
#include "sysmodule.h"
2930
#include "compile.h"
3031
#include "frameobject.h"
3132
#include "traceback.h"
3233
#include "structmember.h"
34+
#include "osdefs.h"
3335

3436
typedef struct _tracebackobject {
3537
OB_HEAD
@@ -154,21 +156,56 @@ tb_displayline(fp, filename, lineno)
154156
int lineno;
155157
{
156158
FILE *xfp;
157-
char buf[1000];
159+
char linebuf[1000];
158160
int i;
159-
if (filename[0] == '<' && filename[strlen(filename)-1] == '>')
160-
return;
161161
xfp = fopen(filename, "r");
162162
if (xfp == NULL) {
163-
fprintf(fp, " (cannot open \"%s\")\n", filename);
164-
return;
163+
/* Search tail of filename in sys.path before giving up */
164+
object *path;
165+
char *tail = strrchr(filename, SEP);
166+
if (tail == NULL)
167+
tail = filename;
168+
else
169+
tail++;
170+
path = sysget("path");
171+
if (path != NULL && is_listobject(path)) {
172+
int npath = getlistsize(path);
173+
char namebuf[MAXPATHLEN+1];
174+
for (i = 0; i < npath; i++) {
175+
object *v = getlistitem(path, i);
176+
if (is_stringobject(v)) {
177+
int len;
178+
strcpy(namebuf, getstringvalue(v));
179+
len = getstringsize(v);
180+
if (len > 0 && namebuf[len-1] != SEP)
181+
namebuf[len++] = SEP;
182+
strcpy(namebuf+len, tail);
183+
xfp = fopen(namebuf, "r");
184+
if (xfp != NULL) {
185+
filename = namebuf;
186+
break;
187+
}
188+
}
189+
}
190+
}
165191
}
192+
fprintf(fp, " File \"%s\"", filename);
193+
#ifdef applec /* MPW */
194+
/* This is needed by MPW's File and Line commands */
195+
fprintf(fp, "; ");
196+
#else
197+
/* This is needed by Emacs' compile command */
198+
fprintf(fp, ", ");
199+
#endif
200+
fprintf(fp, "line %d\n", lineno);
201+
if (xfp == NULL)
202+
return;
166203
for (i = 0; i < lineno; i++) {
167-
if (fgets(buf, sizeof buf, xfp) == NULL)
204+
if (fgets(linebuf, sizeof linebuf, xfp) == NULL)
168205
break;
169206
}
170207
if (i == lineno) {
171-
char *p = buf;
208+
char *p = linebuf;
172209
while (*p == ' ' || *p == '\t')
173210
p++;
174211
fprintf(fp, " %s", p);
@@ -183,19 +220,7 @@ tb_printinternal(tb, fp)
183220
tracebackobject *tb;
184221
FILE *fp;
185222
{
186-
while (tb != NULL) {
187-
if (intrcheck())
188-
break;
189-
fprintf(fp, " File \"%s\"",
190-
getstringvalue(tb->tb_frame->f_code->co_filename));
191-
#ifdef applec /* MPW */
192-
/* This is needed by MPW's File and Line commands */
193-
fprintf(fp, "; ");
194-
#else
195-
/* This is needed by Emacs' compile command */
196-
fprintf(fp, ", ");
197-
#endif
198-
fprintf(fp, "line %d\n", tb->tb_lineno);
223+
while (tb != NULL && !intrcheck()) {
199224
tb_displayline(fp,
200225
getstringvalue(tb->tb_frame->f_code->co_filename),
201226
tb->tb_lineno);

0 commit comments

Comments
 (0)