[Python-Dev] Symbolic errno values in error messages
Hi, I spent some time googleing for "OSError 4" before it occurred to me that "4" was actually an irrelevant implementation detail. As soon as I searched for "EINTR", I found exactly what I was looking for. (not really but this is another story) I jumped to the conclusion that OSError.__str__() should return the symbolic error number instead of the integer value. I was about to write a patch but I just noticed that OSError and friends are implemented in C so I guess it will be a bit harder than expected. The error message comes from EnvironmentError.__str__() which is implemented by EnvironmentError_str() in Objects/exceptions.c. A Python implementation could have replaced self.errno in the format string args by errno.errorcode[self.errno]. However, it's not clear to me if there is such a mapping available in C. Since my fix is not as trivial as I expected, I ask for advice regarding the following questions: 1) Should OSError.__str__() print the symbolic name of errno? 2) Where can I find the symbolic name in C? Best regards, -- Yannick Gingras ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Symbolic errno values in error messages
"Alexander Belopolsky" <[EMAIL PROTECTED]> writes: >>>> try: > ...open('/') > ... except Exception,e: > ...pass > ... >>>> print e > [Errno 21] Is a directory > > 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. -- Yannick Gingras ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Symbolic errno values in error messages
"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 Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Symbolic errno values in error messages
"Guido van Rossum" <[EMAIL PROTECTED]> writes: > Have you considered whether this works on all platforms? (E.g. > Windows, or embedded non-Unix-based.) Yes but I guess I didn't comment it properly. The line printed_errno = errno_str ? errno_str : self->myerrno; ensures that we gracefully fallback to numeric errno when here is no symbolic value available. The code is only invoked if there is an errno value, which takes care of most non-Unix platforms. -- Yannick Gingras ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] [patch] Duplicate sections detection in ConfigParser
Greetings Pythonistas, ConfigParser as you most certainly know is a .ini parser included in the Python standard library. It's documentation mentions a DuplicateSectionError but I was puzzled after hunting a bug in our application that this error was not raised when parsing a file with duplicate sections. After looking at the code, it turns out that DuplicateSectionError is only raised when using the builder interface; the parser interface will never throw it. The attached patch is compatible with both the 2.x and the 3.x branches; it adds a `unique_sects` parameter to the constructor of RawConfigParser and a test in the parser loop that raises DuplicateSectionError if a section is seen more then once and that unique_sects is True. This is just a proof of concept and I'd like your opinion on it before working on the final version. I see two main issues regarding backward compatibility and uniformity. For uniformity `unique_sects` should also apply to the builder interface. However, if it does, it should default to True since it was the default behavior for the builder interface to raise on duplicate sections. On the other hand, the default behavior for the parser interface was to ignore duplicate sections so making `unique_sects` default to True might break existing code. To summarize, I ask you all: * Is the lack of duplicates detection in the parser a problem worth addressing? * Should `unique_sects` control the builder interface? * Should it default to True? Best regards, -- Yannick Gingras http://ygingras.net Index: Lib/ConfigParser.py === --- Lib/ConfigParser.py (revision 68546) +++ Lib/ConfigParser.py (working copy) @@ -215,13 +215,14 @@ class RawConfigParser: -def __init__(self, defaults=None, dict_type=dict): +def __init__(self, defaults=None, dict_type=dict, unique_sects=False): self._dict = dict_type self._sections = self._dict() self._defaults = self._dict() if defaults: for key, value in defaults.items(): self._defaults[self.optionxform(key)] = value +self._unique_sects = unique_sects def defaults(self): return self._defaults @@ -468,7 +469,10 @@ if mo: sectname = mo.group('header') if sectname in self._sections: -cursect = self._sections[sectname] +if self._unique_sects: +raise DuplicateSectionError(sectname) +else: +cursect = self._sections[sectname] elif sectname == DEFAULTSECT: cursect = self._defaults else: signature.asc Description: This is a digitally signed message part. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] [patch] Duplicate sections detection in ConfigParser
On Tuesday 03 February 2009, Raghuram Devarakonda wrote: > http://bugs.python.org/issue2204 refers to the same issue. Perhaps, > you can upload your patch there in addition to adding any comments. I attached the patch to the ticket. Do you have recommendations on how to solve the uniformity issue with the builder interface? -- Yannick Gingras http://ygingras.net signature.asc Description: This is a digitally signed message part. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com