Wandering Thoughts archives


Some words of wisdom for free webmail providers

Vernon Schryver, in news.admin.net-abuse.email:

The buck always stops or is at least duplicated at the SMTP client. It does not matter to rational spam targets whether the operators of a spam relay are paid for their services or forward spam as a charity.

(From Message-ID <dgefme$buk$1@calcite.rhyolite.com>, and let's hope that link stays working.)

spam/WordsForWebmailProviders written at 15:37:22; Add Comment

Two Python import tricks

Here's an extreme example of import and namespace trickery, drawn from the standard Python ftplib module:

  import SOCKS
  socket = SOCKS
  del SOCKS
  from socket import getfqdn
  socket.getfqdn = getfqdn
  del getfqdn
except ImportError:
  import socket

(linebreaks added for clarity)

Modules are objects in Python, so a module's name binding can be manipulated like anything else. This leads to the two things that ftplib is doing here.

First, the ftplib module would like to use the SOCKS module if it exists and otherwise use the standard socket module. Rather than sprinkle conditionals throughout its own code, it just imports SOCKS and renames it to socket, counting on SOCKS to have all the socket routines it needs under the same names as socket does.

Second, SOCKS is apparently missing the getfqdn function. So ftplib yanks a reference to it out of the real socket module and stuffs it into the SOCKS module (that ftplib knows as 'socket'). This lets its code can just call socket.getfqdn without having to worry about whether socket is the real thing or SOCKS.

This works because of bonus trickery: unlike other operations, both 'import' and 'from <blah> import ...' take the names without looking them up in the current namespace. So even though the name 'socket' points to the SOCKS module at this point, 'from socket import getfqdn' goes off to the real socket module, instead of trying to find getfqdn in the SOCKS module.

Because modules are global, the side effect of this is that ftplib has changed SOCKS's namespace for everyone that's imported it. If somewhere in the program another module has also imported SOCKS, they can refer to 'SOCKS.getfqdn' and it will work.

(There are actually practical uses of altering other modules' namespaces. I may cover some of them in a later entry.)

python/ImportTricks written at 01:41:54; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.