On 11 February 2016 at 17:10, Ulli Horlacher <frams...@rus.uni-stuttgart.de> wrote: > > Ulli Horlacher <frams...@rus.uni-stuttgart.de> wrote: > As a hack, I modified the standard library module tarfile.py: > > root@diaspora:/usr/lib/python2.7# vv -d > --- ./.versions/tarfile.py~1~ 2015-06-22 21:59:27.000000000 +0200 > +++ tarfile.py 2016-02-11 18:01:50.185555952 +0100 > @@ -2045,6 +2045,7 @@ > directories.append(tarinfo) > tarinfo = copy.copy(tarinfo) > tarinfo.mode = 0700 > + print('untar "%s"' % tarinfo.name) > self.extract(tarinfo, path) > > # Reverse sort directories. > > > This gives me exact the output I want :-) > > BUT I want to distribute my program and all others will not see the tar > extracting information. > > Now my question: > > How can I substitute the standard module function tarfile.extractall() with > my own function?
import tarfile def new_extractall(self, *args, **kwargs): print("I am a function. Woohoo!") tarfile.TarFile.extractall = new_extractall But bear in mind that that will change tarfile.extractall for every single module that imports it within the same python process. Is that really what you want? Is there a reason you can't subclass TarFile as others have suggested? Perhaps even this is enough: class NoisyTarFile(TarFile): """untested, sorry""" def extract(self, member, *args, **kwargs): print('extracting "%s"' % member.name) super(NoisyTarFile, self).extract(member, *args, **kwargs) As the very next step after your print in extractall is a call to extract anyway? If you must patch the standard library tarfile module then I would suggest patching it to have an extra, default False, argument to enable your printing behaviour, so you don't risk messing up anyone else's use of it. -- Matt Wheeler http://funkyh.at -- https://mail.python.org/mailman/listinfo/python-list