[ python-Bugs-1458017 ] Log._log needs to be more forgiving...

SourceForge.net noreply at sourceforge.net
Sat Apr 1 00:13:01 CEST 2006


Bugs item #1458017, was opened at 2006-03-24 21:17
Message generated for change (Comment added) made by splitscreen
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1458017&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Distutils
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Skip Montanaro (montanaro)
Assigned to: Nobody/Anonymous (nobody)
Summary: Log._log needs to be more forgiving...

Initial Comment:
The _log method of distutils' Log class executes

    print msg % args

where args might be an empty tuple (the methods that
call _log all
have varargs signatures).  If that's the case and msg
happens to
contain a % sign, it barfs.  It should be more
forgiving, for
instance:

    def _log(self, level, msg, args):
        if level >= self.threshold:
            try:
                print msg % args
            except TypeError:
                if not args:
                    print msg
                else:
                    raise
            sys.stdout.flush()

I just corrected this locally for our 2.3 and 2.4
installations.  The
above is a bit ugly, but it does allow the common case
(msg contains a
% but an empty args list) to pass while still catching
most programmer
errors.  Distutils is trying to log this message (wrapped):

  gcc -fno-strict-aliasing -DNDEBUG -g -O3 -Wall
-Wstrict-prototypes
  -fPIC -I/opt/app/mysql-5.0.19/include
  -I/opt/app/mysql-5.0.19/include
-I/opt/lang/python/include/python2.3
  -c _mysql.c -o build/temp.solaris-2.8-i86pc-2.3/_mysql.o
  -I/opt/app/mysql-5.0.19/include -xO3 -mt -fsimple=1
-ftrap=%none
  -nofstore -xbuiltin=%all -xlibmil -xlibmopt
-xtarget=generic

I suppose it would be better if all the places in
distutils which log
compiler commands without extra args escaped their %
signs.  This
seems like an acceptable compromise though.

Skip


----------------------------------------------------------------------

Comment By: splitscreen (splitscreen)
Date: 2006-03-31 22:13

Message:
Logged In: YES 
user_id=1126061

This bug report should be closed. See patch #1462414

----------------------------------------------------------------------

Comment By: splitscreen (splitscreen)
Date: 2006-03-28 17:45

Message:
Logged In: YES 
user_id=1126061

Oh right, I understand now.

Yeah the fix seems fine to me.

Although, would it be more appropriate to check 

if not args:

is True?

So the _log() function would be something more like this:

def _log(self, level, msg, args):
    if level >= self.threshold:
        if not args:
            print msg
        else:
            print msg % args
         sys.stdout.flush()

Just thinking out-loud really.. not sure the check for the
raised exception is really necessary.

Thanks, Matt

----------------------------------------------------------------------

Comment By: Skip Montanaro (montanaro)
Date: 2006-03-28 03:58

Message:
Logged In: YES 
user_id=44345

It barfs if you pass an empty tuple and the msg argument contains a % escape.
For example, try:

    print "%s" % ()

In the particular case that tripped me up, it's trying to print a (bogus) gcc
command which contains % escapes while passing no args to map them.


----------------------------------------------------------------------

Comment By: splitscreen (splitscreen)
Date: 2006-03-27 21:27

Message:
Logged In: YES 
user_id=1126061

I can't reproduce on 2.4.2. Although maybe I misunderstood...

Are you saying that _log() barfs when you pass an empty tuple?

Passing an empty tuple to _log() works fine here..

I can understand it breaking when you pass some format
string to args and the msg contains an '%', that's the
developer's fault for not escaping the precentage sign.

Could you clarify? Or am I totally wrong?

Thanks

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1458017&group_id=5470


More information about the Python-bugs-list mailing list