Modules that provide differing functionality amongst different Python versions...

holger krekel pyth at devel.trillke.net
Mon Apr 22 12:21:26 EDT 2002


On Mon, Apr 22, 2002 at 07:25:47AM -0700, Allan Crooks wrote:
> holger krekel <pyth at devel.trillke.net> wrote in message news:<mailman.1019403879.24573.python-list at python.org>...
> 
> > exec forversion("2.2",
> 
> <snip>
> 
> I had considered using exec as a way of getting round it, but it seems
> like quite a hack to get what I want (wrapping code in a string to
> prevent Python accessing it properly).
> Mind you, considering what I want to do, I suppose hacking is really
> the only feasible solution. :)

:-)

> Doing things via exec would ultimately mean that I couldn't take
> advantage of PyChecker...

I don't know PyChecker enough but judging from their README it seems
to work on modules. So it should be conceptionally possible to 
do the following:

import pychecker # at the start of your program

# in forversion:
    involve_pychecker(string)
    exec string

def involve_pychecker(string):
    import os,sys
    if sys.modules.has_key('pychecker'):
        fn=os.tmpnam()
        f=open(fn,'w')
        f.write(string) 
        f.close()
        sys.path.append('/tmp')
        exec "import "+fn # pychecker generates warnings, if any
        sys.path.pop()  # cleanup
	del sys.modules[fn] # kill the temporary module

don't call it hack, call it "adapter to pychecker" :-)
Guess that for debugging purposes the use of os.tmpnam()
is endurable.

	holger





More information about the Python-list mailing list