Enabling shlex in debug mode

I wanted to see if shlex good choice for something that I was trying to build, so I thought it was in debug mode to play with it. Only the shlex constructor has the weird thing it does : it sets self.debug to 0 , and then immediately checks to see if it really is.

 self.debug = 0 self.token = '' self.filestack = deque() self.source = None if self.debug: print 'shlex: reading from %s, line %d' \ % (self.instream, self.lineno) 

I know that Python has some powerful metaprogramming functions, but I can't figure out how this works, even if I override the constructor, there is no programmatic way to get between setting the value and using it.

Should there be a way to output the instruction in if self.debug (and if so, how?), Is this an error, or is there some third possibility that I have not considered?

+7
python shlex
source share
1 answer

Firstly, I am sure that you have found a mistake, and you should go report it . (You have to make sure that it is still present in the latest 3.x code , but I just checked, and it is.) I don’t look for anything unreasonable in shlex objects that do not allow setting debug=1 until they will not be initialized ... but in this case they should not check self.debug in the initializer, since there is no way to set it.

But it is not so difficult to find. The only thing you lose is the first message that displays only public attributes that you can print yourself. So for example:

 class debugging_shlex(shlex): def __init__(self, *args, **kwargs): # shlex is an old-style class in 2.7, so no super shlex.__init__(self, *args, **kwargs) self.debug = 1 print('shlex: reading from %s, line %d' \ % (self.instream, self.lineno)) 

Additional information for the error report:

  • An invalid code was added to this 2000 change , and since then the only change has been to match its 80 columns.
  • There are no unit tests for the debug argument (not surprising considering it is barely documented and just says, “If you want to use this, read the source” ... but you might want to add it anyway).
+2
source share

All Articles