getattr and method name

Terry Reedy tjreedy at udel.edu
Sun Oct 2 18:55:46 EDT 2011


On 10/2/2011 6:02 PM, Kevin Walzer wrote:
> I'm seeing a very odd error in an application I'm developing using
> Python 2.7.2, on Mac OS 10.7.
>
> This application uses a wrapper method to look up other method names via
> getattr and then call those methods. I have not previously had an issue
> with this name, but for some reason this functionality no longer works
> as expected.
>
> Here is the relevant code:
>
> #run command with root privileges
> def runCommand(self, cmd, *args):
> try:
> functionstring = args[2]
> callfunction = getattr(self, functionstring.split('.')[1])
> self.passtext = self.tk.call('authorize::getAuthPassword')
> callfunction()
> except:
> try:
> print cmd
> functionstring = cmd.split()[2]
> callfunction = getattr(self, functionstring.split('.')[1])
> self.passtext = self.tk.call('authorize::getAuthPassword')
> callfunction()
> except:
> raise
>
> I use this approach to call the method named in the "cmd" parameter
> because I also have to look up a password name that is returned by an
> underlying Tk package that I use in my application (that's the reason
> for the 'self.tk.call'). What is happening is when I use the
> "callfunction()" call, instead of the method name being called, a string
> like this is being invoked:
>
> <bound method phynchronicityApp.scanPackages of
> <__main__.phynchronicityApp instance at 0x101b232d8>>
>
> The "scanPackages" method (just to use it as an example) uses Popen to
> call an underlying system tool on the OS. However, when invoked via
> callfunction(), the 'bound method...' string is passed to the OS instead
> as a command! As a result, I get this output from the pipe:
>
> /bin/sh: bound: No such file or directory
>
> I am not sure what in my application is causing this kind of breakage,
> as earlier versions of the app ran fine with similar code on earlier
> versions on the OS. Is this the correct way to structure this kind of
> functionality, or am I better off structuring it some other way?

I do not know of any 2.7 changes that would affect such code. Perhaps 
you changed something in the method being invoked (not shown)
.
You did not specify whether you have the problem in the first or second 
try block. I would start with printing the type and value of some to all 
of the variables.

-- 
Terry Jan Reedy




More information about the Python-list mailing list