[Tutor] questions about debugging program

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Aug 19 14:48:41 EDT 2003



On Tue, 19 Aug 2003, gerard henry wrote:

> i write a little module: debug.py
> ##################### debug.py #########################################
> import logging, logging.handlers	# pour logger les messages
>
> logger = logging.getLogger('')
> logger.setLevel(logging.DEBUG)
> handler_log = logging.FileHandler("LGD.log", 'a')
> format = logging.Formatter("%(asctime)s %(filename)s:%(lineno)d
> %(levelname)-5s - %(message)s")
> handler_log.setFormatter(format)
> logger.addHandler(handler_log)
> ##################### end of debug.py ##################################
>
> and in others modules, i have to add:
>
> import debug
>
> and     debug.logger.info(any_data)
>
> it's great, yes, but if i want to suppress this last line, how can i do
> to not forget to suppress "import debug" also?

[warning: evil kludgy technique exposed in this post: if you're just
learning Python, do not try this as home.  *grin*]



Hi Gerard,

If you're trying to use your 'debug' module without importing it first,
it's possible, but may not be a good idea.  You can probably try smuggling
the contents of 'debug' into some other module that you import from the
rest of your other program.


In particular, it's possible to smuggle something by subverting Python's
__builtins__ global dictionary:

###
[dyoo at tesuque dyoo]$ cat test_smuggling.py
answer = 42
__builtins__['answer'] = answer
[dyoo at tesuque dyoo]$
[dyoo at tesuque dyoo]$
[dyoo at tesuque dyoo]$ cat test1.py
import test_smuggling

print answer
[dyoo at tesuque dyoo]$
[dyoo at tesuque dyoo]$
[dyoo at tesuque dyoo]$ python test1.py
42
###


__builtins__ is automatically a part of every module, so pushing that
logger into __builtins__ there should do the trick.  But, in general, this
technique is a very bad, disgusting, and evil idea, because it suffers the
same problems that globals variables present.  Subverting __builtins__
also a maintenence nightmare, since now it becomes much harder to track
down how in the world something got into it.


With all the dangers of modifying __builtins__, it's often best to be
explicit, bite down, and just do the 'import debug' instead.  *grin*




> in C/C++, i know if something is not used, and i can lite source code,
> but how can i do the same in python? in my 60 files, there is a lot of
> import, and i m sure there are not all necessary

If you're worried about the cost of import, you don't have to worry so
much: imports are cached in Python: once you import a module, it stays in
memory just once.  If that's your concern, you don't have to worry about
it: it's not an issue.


If you're worried about having to visit every file out of your project to
add that 'import debug' statement, then you may want to try automating the
process with another program!  It's a valid programming technique to use
programs to write and maintain source files.  Such a program might look
something like:

###
for filename in all_my_python_project_files:
    if is_missing_debug_import(filename):
        add_debug_import_in(filename)
###

where is_missing_debug_import() can be as simple as a string search for
the words 'import debug'.


If you have more questions, or if I've completely misinterpreted part of
your question, please feel free to reply to the list; we'll be happy to
clarify.  Good luck to you!





More information about the Tutor mailing list