Salome HOME
PN : ajout du ITEM_PARAMETRE pour accepter les parametres de la forme
[tools/eficas.git] / Utilites / execute.py
1 # -*- coding: utf-8 -*-
2 """
3 Module exec
4 -----------
5     le module exec ...
6 """
7
8 try :
9         from developpeur import DEVELOPPEUR
10 except :
11         DEVELOPPEUR=None
12
13 if DEVELOPPEUR :
14
15     import message
16     import scrute
17     import types
18     import developpeur
19     developpeur.sortie.write( "import de "+__name__+" : $Id: execute.py,v 1.1 2003/03/06 14:36:11 eficas Exp $" )
20     developpeur.sortie.write( "\n" )
21     developpeur.sortie.flush()
22
23
24     class EXEC :
25
26         def __init__ ( self, texte, contexte=None, verbeux=1 ) :
27
28             assert( type(texte) == types.StringType )
29             if contexte == None :
30                 contexte = globals()
31
32             if verbeux :
33                 message.MESSAGE( "execution de "+texte )
34             try :
35                 exec texte in contexte
36             except Exception,e :
37                 if verbeux :
38                     import traceback
39                     traceback.print_exc()
40                     developpeur.sortie.write( "\n\n\n" )
41                     message.MESSAGE( "Exception interceptee" )
42                     scrute.SCRUTE( texte )
43                     scrute.SCRUTE( contexte )
44                     scrute.SCRUTE( e.__class__.__name__ )
45                     scrute.SCRUTE( str(e) )
46                     developpeur.sortie.write( "\n\n\n" )
47                     developpeur.sortie.flush()
48                 raise 
49
50
51 else :
52     class EXEC : pass
53
54
55
56
57 if __name__ == "__main__" :
58     class Ex(Exception) : pass
59     def toto() :
60         print "toto"
61         raise Ex( "y a bel et bien un erreur" )
62
63     def tutu() :
64         s = "toto()"
65         EXEC( s , verbeux=1)
66
67     try :
68         tutu()
69     except Exception,ee :
70         scrute.SCRUTE(str(ee))
71         scrute.SCRUTE(ee.__class__)
72         pass