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

Skip to content

Commit d58d764

Browse files
committed
Several changes to support inclusion of filename in relevent
exceptions: posix_error_with_filename(): New function which calls PyErr_SetFromErrnoWithFilename() The following methods have been changed to call posix_error_with_filename(): posix_1str() posix_strint() posix_strintint() posix_do_stat() posix_mkdir() posix_utime() posix_readlink() posix_open() INITFUNC(): os.error (nee PosixError) is PyExc_OSError
1 parent 52ddc0e commit d58d764

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

Modules/posixmodule.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,18 @@ static PyObject *PosixError; /* Exception posix.error */
325325

326326
/* Set a POSIX-specific error from errno, and return NULL */
327327

328-
static PyObject * posix_error()
328+
static PyObject *
329+
posix_error()
329330
{
330331
return PyErr_SetFromErrno(PosixError);
331332
}
333+
static PyObject *
334+
posix_error_with_filename(name)
335+
char* name;
336+
{
337+
return PyErr_SetFromErrnoWithFilename(PosixError, name);
338+
}
339+
332340

333341
#if defined(PYOS_OS2)
334342
/**********************************************************************
@@ -420,7 +428,7 @@ posix_1str(args, func)
420428
res = (*func)(path1);
421429
Py_END_ALLOW_THREADS
422430
if (res < 0)
423-
return posix_error();
431+
return posix_error_with_filename(path1);
424432
Py_INCREF(Py_None);
425433
return Py_None;
426434
}
@@ -438,6 +446,7 @@ posix_2str(args, func)
438446
res = (*func)(path1, path2);
439447
Py_END_ALLOW_THREADS
440448
if (res != 0)
449+
/* XXX how to report both path1 and path2??? */
441450
return posix_error();
442451
Py_INCREF(Py_None);
443452
return Py_None;
@@ -457,7 +466,7 @@ posix_strint(args, func)
457466
res = (*func)(path, i);
458467
Py_END_ALLOW_THREADS
459468
if (res < 0)
460-
return posix_error();
469+
return posix_error_with_filename(path);
461470
Py_INCREF(Py_None);
462471
return Py_None;
463472
}
@@ -476,7 +485,7 @@ posix_strintint(args, func)
476485
res = (*func)(path, i, i2);
477486
Py_END_ALLOW_THREADS
478487
if (res < 0)
479-
return posix_error();
488+
return posix_error_with_filename(path);
480489
Py_INCREF(Py_None);
481490
return Py_None;
482491
}
@@ -496,7 +505,7 @@ posix_do_stat(self, args, statfunc)
496505
res = (*statfunc)(path, &st);
497506
Py_END_ALLOW_THREADS
498507
if (res != 0)
499-
return posix_error();
508+
return posix_error_with_filename(path);
500509
return Py_BuildValue("(llllllllll)",
501510
(long)st.st_mode,
502511
(long)st.st_ino,
@@ -868,7 +877,7 @@ posix_mkdir(self, args)
868877
#endif
869878
Py_END_ALLOW_THREADS
870879
if (res < 0)
871-
return posix_error();
880+
return posix_error_with_filename(path);
872881
Py_INCREF(Py_None);
873882
return Py_None;
874883
}
@@ -1056,7 +1065,7 @@ posix_utime(self, args)
10561065
res = utime(path, UTIME_ARG);
10571066
Py_END_ALLOW_THREADS
10581067
if (res < 0)
1059-
return posix_error();
1068+
return posix_error_with_filename(path);
10601069
Py_INCREF(Py_None);
10611070
return Py_None;
10621071
#undef UTIME_ARG
@@ -1772,7 +1781,7 @@ posix_readlink(self, args)
17721781
n = readlink(path, buf, (int) sizeof buf);
17731782
Py_END_ALLOW_THREADS
17741783
if (n < 0)
1775-
return posix_error();
1784+
return posix_error_with_filename(path);
17761785
return PyString_FromStringAndSize(buf, n);
17771786
}
17781787
#endif /* HAVE_READLINK */
@@ -1985,7 +1994,7 @@ posix_open(self, args)
19851994
fd = open(file, flag, mode);
19861995
Py_END_ALLOW_THREADS
19871996
if (fd < 0)
1988-
return posix_error();
1997+
return posix_error_with_filename(file);
19891998
return PyInt_FromLong((long)fd);
19901999
}
19912000

@@ -2768,8 +2777,7 @@ INITFUNC()
27682777
if (all_ins(d))
27692778
return;
27702779

2771-
/* Initialize exception */
2772-
PosixError = PyErr_NewException("os.error", NULL, NULL);
2773-
if (PosixError != NULL)
2774-
PyDict_SetItemString(d, "error", PosixError);
2780+
Py_INCREF(PyExc_OSError);
2781+
PosixError = PyExc_OSError;
2782+
PyDict_SetItemString(d, "error", PosixError);
27752783
}

0 commit comments

Comments
 (0)