[issue11959] smtpd cannot be used without affecting global state

Vinay Sajip report at bugs.python.org
Sat Apr 30 17:24:07 CEST 2011


Vinay Sajip <vinay_sajip at yahoo.co.uk> added the comment:

I don't want to use two different maps - I just want to use a single map which is not the global "socket_map" in asyncore.

asyncore.dispatcher and asynchat.async_chat allow for a map to be passed in so that the default global is not used, but smtpd does not allow this. Note that asyncore.loop() also allows a map to be passed in, so I'm sure this functionality is by design.

I mentioned two places where the map is to be used - passed to SMTPServer constructor (and saved in SMPTServer instance) and the *same* map used to initialise the SMTPChannel from SMTPServer.handle_accepted().

Since asyncore and asynchat support using a passed-in map to avoid using a global, it's not unreasonable to expect smtpd to support it too. After all, using it relies on asyncore.loop(), and passing an explicit map is allowed here.

I initially came across this because I got some warnings from regrtest.py about changed state, when I was trying to implement a TestSMTPServer class (derived from smtpd.SMTPServer) to test the SMTPHandler in logging.

I've taken out the functionality from test_logging for now, but I have a test script here:

https://gist.github.com/949744

This successfully uses a non-global map ("my_map"), but notice how much I had to resort to copypasta.

If I've missed some neat solution which avoids this hackery, please let me know! This is my first use of the smtpd module :-)

As I say, what I'm trying to do is to avoid changing global state in the unit test suite.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11959>
_______________________________________


More information about the Python-bugs-list mailing list