(solved) smtplib, capturing output from set_debuglevel ??

Josiah Carlson jcarlson at uci.edu
Fri Oct 8 20:56:57 EDT 2004


> There's no way to get what the OP wanted through subclassing.
> Rather, any subclassing would end up rewriting non-trivial
> amount of smtp logic so the subclass would break whenever a
> copy would break.

I believe smtplib is fairly stable.  What I meant by it breaking, is in
the case of editing smtplib.py in-place, and leaving it in the
platform's standard library, then later upgrading Python.  I have seen
some newbs do this and wonder where their changes had gone.

After perusing it (smtplib) for a bit, I notice how often the debug
notices are peppered through.  Thankfully for the most recent CVS, they
include 'print >>stderr, <stuff>' rather than the previous 'print
<stuff>'.  At least it allows doing smtplib.stderr = <anything>, without
affecting other modules that rely on a reasonable stderr.

Subclassing smtplib (as I originally suggested) doesn't look like a
viable option.

> The choice is to copy into new file with different name or
> do your solution.  The new thread-specific data storage in
> 2.4 should also make that much easier to implement.

Thread local storage isn't necessary, and actually makes it harder.  You
have to write custom code to get back your per-thread storage, whether
that is remembering to write the log when the thread dies, or using a
weakref to do it for you; neither of which are necessarily a newbie
thing to do.


> What the OP wants is handy for things like email programs
> that want to give feedback to the user about what's going on.
> It captures some steps that can't otherwise be done via
> subclassing.

Having written something similar, I have noticed that just the return
codes and exceptions returned by smtplib calls to be sufficient for
nearly everything.  I don't think I've ever set the debug level on the
object.


> This approach may be better, though I'm not sure.  I
> would rather use a callback handler.  In any case it's
> more complicated than I would suggest for a self-described
> new Python programmer.

Callbacks can get ugly.  I tend to prefer reactor-style/event-based
stuff. It can take a while to wrap your brain around it for any
moderately complex protocol, but you can get pretty high concurrency
with just a single thread; on the order of Python's file handle limit
with relative ease.

 - Josiah




More information about the Python-list mailing list