Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102871

Re: modifying a standard module? (was: Re: tarfile : read from a socket?)

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Matt Wheeler <m@funkyhat.org>
Newsgroups comp.lang.python
Subject Re: modifying a standard module? (was: Re: tarfile : read from a socket?)
Date Fri, 12 Feb 2016 20:53:16 +0000
Lines 63
Message-ID <mailman.85.1455310425.22075.python-list@python.org> (permalink)
References <n9hgtb$rls$1@news2.informatik.uni-stuttgart.de> <mailman.43.1455187713.22075.python-list@python.org> <n9i07l$vgl$1@news2.informatik.uni-stuttgart.de> <n9i246$vva$1@news2.informatik.uni-stuttgart.de> <n9idk7$37f$1@news2.informatik.uni-stuttgart.de> <n9ifak$3l7$1@news2.informatik.uni-stuttgart.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de GAOvkrHQhXHklDkz7Xu/uQiq2bQiOg5TarPwPvn/8jNw==
Return-Path <m@funkyhat.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '+++': 0.03; 'modified': 0.05; '*args,': 0.07; 'false,': 0.07; 'cc:addr:python-list': 0.09; '"%s"\'': 0.09; 'imports': 0.09; 'messing': 0.09; 'path)': 0.09; 'subclass': 0.09; 'subject:module': 0.09; 'python': 0.10; ':-)': 0.12; 'output': 0.13; 'def': 0.13; 'suggest': 0.15; 'argument': 0.15; '2016': 0.16; 'enough:': 0.16; 'function?': 0.16; 'hack,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'substitute': 0.16; 'tarfile': 0.16; 'wrote:': 0.16; 'library': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '+0200': 0.20; 'cc:no real name:2**0': 0.22; 'import': 0.24; 'patch': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'module': 0.25; 'distribute': 0.27; 'message-id:@mail.gmail.com': 0.27; '+0100': 0.27; 'function': 0.28; '---': 0.28; '**kwargs)': 0.29; '**kwargs):': 0.29; 'question:': 0.29; 'tar': 0.29; 'print': 0.30; 'subject: : ': 0.30; 'anyone': 0.32; "can't": 0.32; 'class': 0.33; 'extract': 0.33; 'skip:d 20': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'next': 0.35; 'skip:. 20': 0.35; 'received:74.125.82': 0.35; 'step': 0.36; 'but': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'subject:from': 0.39; 'your': 0.60; 'default': 0.61; 'skip:n 10': 0.62; 'within': 0.64; 'reverse': 0.66; 'member,': 0.67; 'risk': 0.68; "else's": 0.84; 'from:addr:m': 0.84; 'received:74.125.82.41': 0.84; 'subject:read': 0.84
X-Virus-Scanned Debian amavisd-new at membrane.funkyhat.net
X-Gm-Message-State AG10YORVWWxnXYFLMhvIDBtN/Nb1CSaNN0gimK3aRYfqo+nKYghLH0rcK7o/uPyyd3/jmIcJrv7zoz0euCl/4w==
X-Received by 10.194.184.171 with SMTP id ev11mr3675472wjc.44.1455310416580; Fri, 12 Feb 2016 12:53:36 -0800 (PST)
In-Reply-To <n9ifak$3l7$1@news2.informatik.uni-stuttgart.de>
X-Gmail-Original-Message-ID <CAG93HwEe=gmdR91dtFpdCqsmso1gK=XLTczkJneTAuO5nBEMTw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102871

Show key headers only | View raw


On 11 February 2016 at 17:10, Ulli Horlacher
<framstag@rus.uni-stuttgart.de> wrote:
>
> Ulli Horlacher <framstag@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

tarfile : read from a socket? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-11 08:31 +0000
  Re: tarfile : read from a socket? INADA Naoki <songofacandy@gmail.com> - 2016-02-11 19:26 +0900
  Re: tarfile : read from a socket? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-02-11 11:47 +0100
    Re: tarfile : read from a socket? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-11 12:53 +0000
      Re: tarfile : read from a socket? MRAB <python@mrabarnett.plus.com> - 2016-02-11 13:16 +0000
      Re: tarfile : read from a socket? Chris Angelico <rosuav@gmail.com> - 2016-02-12 00:18 +1100
        Re: tarfile : read from a socket? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-11 13:34 +0000
      Re: tarfile : read from a socket? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-11 13:25 +0000
        Re: tarfile : read from a socket? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-11 16:41 +0000
          Re: tarfile : read from a socket? MRAB <python@mrabarnett.plus.com> - 2016-02-11 17:10 +0000
          modifying a standard module? (was: Re: tarfile : read from a socket?) Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-11 17:10 +0000
            Re: modifying a standard module? (was: Re: tarfile : read from a socket?) Matt Wheeler <m@funkyhat.org> - 2016-02-12 20:53 +0000
              Re: modifying a standard module? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-12 22:46 +0000
                Re: modifying a standard module? Paul Rubin <no.email@nospam.invalid> - 2016-02-12 16:21 -0800
          Re: tarfile : read from a socket? Peter Otten <__peter__@web.de> - 2016-02-11 18:18 +0100
          Re: tarfile : read from a socket? Lars Gustäbel <lars@gustaebel.de> - 2016-02-11 18:27 +0100
          Re: tarfile : read from a socket? Random832 <random832@fastmail.com> - 2016-02-11 16:23 -0500
          Re: tarfile : read from a socket? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-02-12 09:35 +0100
          Re: tarfile : read from a socket? Lars Gustäbel <lars@gustaebel.de> - 2016-02-12 09:40 +0100
            Re: tarfile : read from a socket? Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-02-12 12:48 +0000

csiph-web