Re: [Python-Dev] Symbolic errno values in error messages

2008-05-19 Thread Georg Brandl

Yannick Gingras schrieb:

"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?


For new-style Python C files, this style definition works well:

(c-add-style
 "python-new"
 '((indent-tabs-mode . nil)
   (fill-column  . 78)
   (c-basic-offset   . 4)
   (c-offsets-alist  . ((substatement-open . 0)
(inextern-lang . 0)
(arglist-intro . +)
(knr-argdecl-intro . +)))
   (c-hanging-braces-alist . ((brace-list-open)
  (brace-list-intro)
  (brace-list-close)
  (brace-entry-open)
  (substatement-open after)
  (block-close . c-snug-do-while)))
   (c-block-comment-prefix . "* "))
 )

This is a very crude hook that auto-selects the C style depending on
whether it finds a line starting with tab in the first 3000 characters
in the file:

(defun c-select-style ()
  (save-excursion
(if (re-search-forward "^\t" 3000 t)
(c-set-style "python")
  (c-set-style "python-new"
(add-hook 'c-mode-hook 'c-select-style)

HTH,
Georg

___
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

2008-05-19 Thread Yannick Gingras
"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


Re: [Python-Dev] Symbolic errno values in error messages

2008-05-19 Thread Yannick Gingras
"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

2008-05-16 Thread Martin v. Löwis
> Until recently, python had its own cross-platform implementation of
> strerror, but it was removed because it was deemed redundant.  This
> tells me that it should work on windows.

That conclusion is incorrect. It works on MSVCRT, but for this specific
aspect, using MSVCRT is a bad idea (because it artificially renumbers
system errors, just to provide an illusion for what Microsoft considers
POSIX).

Regards,
Martin
___
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

2008-05-16 Thread Alexandre Vassalotti
On Fri, May 16, 2008 at 10:52 AM, Yannick Gingras <[EMAIL PROTECTED]> wrote:
> "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.
>

Then, check out EnvironmentError_str in Objects/exceptions.c. You
should be able import the errno module and fetch its errorcode
dictionary.

-- Alexandre
___
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

2008-05-16 Thread M.-A. Lemburg

On 2008-05-16 17:02, Alexander Belopolsky wrote:

On Fri, May 16, 2008 at 10:52 AM, Yannick Gingras <[EMAIL PROTECTED]> wrote:


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.



In this case, I have a more drastic proposal.  Lets change
EnvironmentError errno attribute (myerrno in C) to string.  


-1

You never want to change an integer field to a string.


'EXYZ'
strings can be interned, which will make them more efficient than
integers for lookups and comparisons (to literals).  A half-way and
backward compatible solution would be to stick 'EXYZ' code at the end
of the args tuple and add an errnosym attribute.


Actually, you don't have to put it into any tuple. Just add it
to the error object as attribute.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, May 16 2008)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


 Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
___
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

2008-05-16 Thread M.-A. Lemburg

On 2008-05-16 16:15, Nick Coghlan wrote:

Alexander Belopolsky wrote:

Yannick Gingras  ygingras.net> writes:

2) Where can I find the symbolic name in C?


Use standard C library char* strerror(int errnum) function.   You can see
an example usage in Modules/posixmodule.c (posix_strerror).


I don't believe that would provide adequate Windows support.


Well, there's still the idea of a winerror module:

http://bugs.python.org/issue1505257

Perhaps someone can pick it up and turn it into a (generated) C
module ?!

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, May 16 2008)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


 Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
___
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

2008-05-16 Thread Alexander Belopolsky
On Fri, May 16, 2008 at 10:52 AM, Yannick Gingras <[EMAIL PROTECTED]> wrote:

> 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.
>

In this case, I have a more drastic proposal.  Lets change
EnvironmentError errno attribute (myerrno in C) to string.  'EXYZ'
strings can be interned, which will make them more efficient than
integers for lookups and comparisons (to literals).  A half-way and
backward compatible solution would be to stick 'EXYZ' code at the end
of the args tuple and add an errnosym attribute.
___
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

2008-05-16 Thread Yannick Gingras
"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

2008-05-16 Thread Alexander Belopolsky
On Fri, May 16, 2008 at 10:15 AM, Nick Coghlan <[EMAIL PROTECTED]> wrote:
> Alexander Belopolsky wrote:
..
>> Use standard C library char* strerror(int errnum) function.   You can see
>> an example usage in Modules/posixmodule.c (posix_strerror).
>
> I don't believe that would provide adequate Windows support.
>

Until recently, python had its own cross-platform implementation of
strerror, but it was removed because it was deemed redundant.  This
tells me that it should work on windows.   On the other hand, IOError
object and in fact EnvironmentError object already has strerror member
which is printed when available:

>>> 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?
___
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

2008-05-16 Thread Jean-Paul Calderone

On Sat, 17 May 2008 00:15:23 +1000, Nick Coghlan <[EMAIL PROTECTED]> wrote:

Alexander Belopolsky wrote:

Yannick Gingras  ygingras.net> writes:

2) Where can I find the symbolic name in C?


Use standard C library char* strerror(int errnum) function.   You can see
an example usage in Modules/posixmodule.c (posix_strerror).


I don't believe that would provide adequate Windows support.



It's not C, but maybe it's interesting to look at anyway:

http://twistedmatrix.com/trac/browser/trunk/twisted/python/win32.py?rev=21682#L94

However, neither strerror nor the linked code gives out symbolic names for
errnos.  They both produce messages like "Interrupted system call", whereas
the symbolic name would be "EINTR".  Modules/errnomodule.c might be worth
looking at, although its solution is somewhat disappointing.

Jean-Paul
___
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

2008-05-16 Thread Nick Coghlan

Alexander Belopolsky wrote:

Yannick Gingras  ygingras.net> writes:

2) Where can I find the symbolic name in C?


Use standard C library char* strerror(int errnum) function.   You can see
an example usage in Modules/posixmodule.c (posix_strerror).


I don't believe that would provide adequate Windows support.

Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://www.boredomandlaziness.org
___
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

2008-05-16 Thread Nick Coghlan

Yannick Gingras wrote:

1) Should OSError.__str__() print the symbolic name of errno?


+1 (assuming the performance hit for doing so is incurred only when the 
exception is actually printed)



2) Where can I find the symbolic name in C?


Modules/errnomodule.c

Cheers,
Nick.

--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://www.boredomandlaziness.org
___
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

2008-05-16 Thread Alexander Belopolsky
Yannick Gingras  ygingras.net> writes:
..
> 
> 1) Should OSError.__str__() print the symbolic name of errno?
> 

+1 for the change

> 2) Where can I find the symbolic name in C?

Use standard C library char* strerror(int errnum) function.   You can see
an example usage in Modules/posixmodule.c (posix_strerror).




___
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] Symbolic errno values in error messages

2008-05-16 Thread Yannick Gingras

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