DIRACGrid/DIRAC — Issue #548: DMS ReplicaManager and python exec
Author: KrzysztofCiba
Created: 2012-02-28 13:28:43+00:00
State: open

I've just spotted this in the code:

```
fileCatalog = FileCatalog( catalogs )
# Generate the execution string
if argsDict:
execString = "res = fileCatalog.%s(lfns" % method
for argument, value in argsDict.items():
if type( value ) == types.StringType:
execString = "%s, %s='%s'" % ( execString, argument, value )
else:
execString = "%s, %s=%s" % ( execString, argument, value )
execString = "%s)" % execString
else:
execString = "res = fileCatalog.%s(lfns)" % method
# Execute the execute string
try:
exec( execString )
except AttributeError, errMessage:
```

Huh? What about using ``hasattr( obj, "metod")`` and ```callable( getattr( obj, "method" ) )```? Usage of ```exec``` is quite dangerous, while python is able to figure out if an instance has got the requested method, it is called introspection. ;)

So what about that:

```
fileCatalog = FileCatalog()
if not hasattr( fileCatalog, "method"):
return S_ERROR("attribute %s not known" % method )
method = getattr( fileCatalog, "method" )
if not callable( method ):
return S_ERROR("attribute %s isn't callable" % method )
res = method( lfns, kwargs, args )
return res
````

```