<[EMAIL PROTECTED]>
<[EMAIL PROTECTED]> <[EMAIL PROTECTED]>
Message-ID: <[EMAIL PROTECTED]>
X-Sender: [EMAIL PROTECTED]
Received: from 82.111.147.97 [82.111.147.97] with HTTP/1.1 (POST); Mon, 30 Jun
        2008 18:02:18 +0200
User-Agent: RoundCube Webmail/0.1
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit



On Mon, 30 Jun 2008 17:14:45 +0200, Alessandro Dentella <[EMAIL PROTECTED]>
wrote:
> c'è un altro aspetto che non mi piace di execfile. Proprio il mese
scorso
> sono passato da execfile a __import__ in un set di test il cui principio
> è
> uguale a quello dei plugin.
> 
> il "problema" è negli import. Supponi di avere un plugin così::
> 
>     import sys
>     
>     class A(object):
>         def test(self):
>             print sys.path
>     
>     
> e di volerlo usare così
> 
>     LOCS = {}
>     execfile('plugin_A.py', {}, LOCS)
>     
>     a = LOCS['A']()
>     a.test()
>     
> 
> ti verrà sollevato l'errore che perchè 'a' non vede __più__ il modulo
> sys.

Il problema e' che usi un dizionario per ambiente globale che pero' poi
butti via.

Il codice seguente funziona come ci si aspetta:

In [1]: ENV = {}
In [3]: execfile('plugin_A.py', ENV)
In [5]: a = ENV['A']()
In [6]: a.test()

-- 
Daniele Varrazzo - Develer S.r.l.
http://www.develer.com

_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a