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