"Alexandre Vassalotti" <[EMAIL PROTECTED]> writes:
>>> So now I am not sure what OP is proposing. Do you want to replace 21
>>> with EISDIR in the above?
>>
>> Yes, that's what I had in mind.
>>
>
> Then, check out EnvironmentError_str in Objects/exceptions.c. You
> should be able import the errno module and fetch its errorcode
> dictionary.
It wasn't as hard as I expected. It's the first time that I play with
the Python C API; I didn't expect the API to be that high level.
I attached a patch to convert errno to its symbolic value when an
EnvironmentError is printed. Should attach it to a ticket on
bugs.python.org?
I'm sure there is a style guide like PEP-8 for C code, feel free to
point me to it because my patch is probably not fully style compliant.
With Emacs, doing
M-x c-set-style python
doesn't seems to do the right thing. Are you all using a bunch of
shared settings in you .emacs files?
--
Yannick Gingras
Index: Objects/exceptions.c
===================================================================
--- Objects/exceptions.c (revision 63365)
+++ Objects/exceptions.c (working copy)
@@ -604,13 +604,32 @@
EnvironmentError_str(PyEnvironmentErrorObject *self)
{
PyObject *rtnval = NULL;
+ PyObject *errnomod = NULL;
+ PyObject *errorcode_dict = NULL;
+ PyObject *errno_str = NULL;
+ PyObject *printed_errno = NULL;
+ /* Extract the symbolic value for errno.
+ Ex: use 'ENOTDIR' instead of 20 */
+ if (self->myerrno) {
+ errnomod = PyImport_ImportModule("errno");
+ if (errnomod == NULL)
+ Py_FatalError("Can't import errno module.");
+
+ errorcode_dict = PyObject_GetAttrString(errnomod, "errorcode");
+ if (errorcode_dict == NULL)
+ Py_FatalError("Can't access errorcode dict.");
+
+ errno_str = PyDict_GetItem(errorcode_dict, self->myerrno);
+ printed_errno = errno_str ? errno_str : self->myerrno;
+ }
+
if (self->filename) {
PyObject *fmt;
PyObject *repr;
PyObject *tuple;
- fmt = PyString_FromString("[Errno %s] %s: %s");
+ fmt = PyString_FromString("[Errno=%s] %s: %s");
if (!fmt)
return NULL;
@@ -627,8 +646,8 @@
}
if (self->myerrno) {
- Py_INCREF(self->myerrno);
- PyTuple_SET_ITEM(tuple, 0, self->myerrno);
+ Py_INCREF(printed_errno);
+ PyTuple_SET_ITEM(tuple, 0, printed_errno);
}
else {
Py_INCREF(Py_None);
@@ -654,7 +673,7 @@
PyObject *fmt;
PyObject *tuple;
- fmt = PyString_FromString("[Errno %s] %s");
+ fmt = PyString_FromString("[Errno=%s] %s");
if (!fmt)
return NULL;
@@ -665,8 +684,8 @@
}
if (self->myerrno) {
- Py_INCREF(self->myerrno);
- PyTuple_SET_ITEM(tuple, 0, self->myerrno);
+ Py_INCREF(printed_errno);
+ PyTuple_SET_ITEM(tuple, 0, printed_errno);
}
else {
Py_INCREF(Py_None);
@@ -688,7 +707,9 @@
}
else
rtnval = BaseException_str((PyBaseExceptionObject *)self);
-
+
+ Py_XDECREF(errnomod);
+ Py_XDECREF(errorcode_dict);
return rtnval;
}
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com