[Python-Dev] Proposal for Python 3.3: dependence injection

Jesus Cea jcea at jcea.es
Thu Mar 24 17:40:18 CET 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi, everybody.

I want to test the dev community interest in modifying the stdlib to
ease dependence injection.

The seminal idea was in
<http://mail.python.org/pipermail/python-dev/2010-April/099237.html>.

A lot of stdlib modules use, deep inside, other modules to do their
work. The most obvious case would be the socket library, used by ANY
module involving the network.

This internal usage, deep inside, prevents programmers to "mess" with
the networking layer in an easy way. Some examples: transparent
encryption/compression, using alternative network implementations like
shared buffers if the traffic is local, integrating the socket in a
async event loop/greenlet/etc, network logging/sniffing, testing
(simulating a server, with no real network connection), etc. Testing
gets very easy if we can "mock" the internal socket, for instance.

So I propose to add an additional OPTIONAL parameter to quite a few
libraries. If the parameter is not present (compatible with client
current code), the library would instantiate a internal socket, just
like now. But if the parameter is present, it would inject a constructor
for the library to use instead of "socket".

Traditionally this "features" can be used in Python code using monkey
patching, but this approach is fragile (can break when upgrading), not
obvious for newbies and difficult to combine (for instance, trying to
apply multiple "monkey patches" over the socket module).

What do you think?. Should I write a PEP? (I don't think so, but your
opinion matters). I care, for instance, about how to garantee the API
coverage actually needed for the new "socket-like" object. The idea is
that your object should be "socket-like", but how much "socket-like" in
the API sense?. Do you need to implement "getpeername()" for injecting
in smtplib?.

If you agree that this is a nice idea, what other libs do you think that
could benefice of being "injected", beside "socket"?.

- -- 
Jesus Cea Avion                         _/_/      _/_/_/        _/_/_/
jcea at jcea.es - http://www.jcea.es/     _/_/    _/_/  _/_/    _/_/  _/_/
jabber / xmpp:jcea at jabber.org         _/_/    _/_/          _/_/_/_/_/
.                              _/_/  _/_/    _/_/          _/_/  _/_/
"Things are not so easy"      _/_/  _/_/    _/_/  _/_/    _/_/  _/_/
"My name is Dump, Core Dump"   _/_/_/        _/_/_/      _/_/  _/_/
"El amor es poner tu felicidad en la felicidad de otro" - Leibniz
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQCVAwUBTYtz8plgi5GaxT1NAQJCzwQAkvtCwvKDh1QcQ+Qfbhe0qrKcQdI4wMfB
g4jpCmCX3UCkTvAjU1AsnxGGwVjvYZWdL2QVZS5+sJOP5GOmMzm96PBYjWZjVovk
YHIP+HFXU0BtbXlRJMV2+bjrwZf1g8CQFngyxqkQ69WdU+FMyxmDhFbcQ7y667HT
ldkINMLSuTk=
=rrkE
-----END PGP SIGNATURE-----


More information about the Python-Dev mailing list