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


Groups > comp.lang.python > #111117

Improved nestedmodule decorator implementation

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Gregory Ewing <greg.ewing@canterbury.ac.nz>
Newsgroups comp.lang.python
Subject Improved nestedmodule decorator implementation
Date Tue, 05 Jul 2016 21:31:52 +1200
Lines 70
Message-ID <du1d4aFebp4U1@mid.individual.net> (permalink)
References <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> <nldgv7$2e0$1@ger.gmane.org> <mailman.65.1467631737.2295.python-list@python.org> <577b1cc6$0$1584$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmp5-T+qnOY-1K_txs+bC5gofNBp1EVqmaKW7dnsOD5Whw@mail.gmail.com> <mailman.81.1467687507.2295.python-list@python.org> <577b2af3$0$1611$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmoU=iRkn3PmeOxk9+=vuuvVzO-CQrd_TtXF52Bjq-Vr9Q@mail.gmail.com> <mailman.82.1467690470.2295.python-list@python.org> <577b4f06$0$2752$c3e8da3$76491128@news.astraweb.com> <du1brpFe32qU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net pD9RH3jk22H/uUgIOtmpYw2pZ6qcmcB3UPFCPtSRZ17TcNS2eT
Cancel-Lock sha1:chup1oDozNZOfvPErv5Y0Iopsr8=
User-Agent Mozilla Thunderbird 1.0.5 (Macintosh/20050711)
X-Accept-Language en-us, en
In-Reply-To <du1brpFe32qU1@mid.individual.net>
Xref csiph.com comp.lang.python:111117

Show key headers only | View raw


I wrote:
> The only shortcoming I can think of is that a nestedmodule
> inside another nestedmodule won't be able to see the names
> in the outer nestedmodule

Actually, that's not correct -- the version I posted before
actually crashes if you try to refer to a name in an outer
nestedmodule from an inner nestedmodule.

Here's an improved version that fully supports nesting to
any depth.

% python3 test_nested_nested.py
foo
blarg

#------------------------------------------
#
#   test_nested_nested.py
#
#------------------------------------------

from nestedmodule import nestedmodule

@nestedmodule
def m1():

     def foo():
         print("foo")

     @nestedmodule
     def m2():

         def blarg():
             foo()
             print("blarg")

m1.m2.blarg()

#------------------------------------------
#
#   nestedmodule.py
#
#------------------------------------------

from types import CodeType, FunctionType, ModuleType
from dis import dis

def hack_code(f):
     """Hack 'return locals()' onto the end of the bytecode of f."""
     code1 = f.__code__
     bytes1 = code1.co_code
     names1 = code1.co_names
     n = len(names1)
     names2 = names1 + ('locals',)
     bytes2 = bytes1[:-4] + bytes([116, n, 0, 131, 0, 0, 83])
     code2 = CodeType(code1.co_argcount, code1.co_kwonlyargcount, code1.co_nlocals,
         code1.co_stacksize, code1.co_flags, bytes2, code1.co_consts, names2,
         code1.co_varnames, code1.co_filename, code1.co_name, code1.co_firstlineno,
         code1.co_lnotab, code1.co_freevars, code1.co_cellvars)
     f2 = FunctionType(code2, f.__globals__, f.__name__, f.__kwdefaults__, 
f.__closure__)
     return f2

def nestedmodule(f):
     f2 = hack_code(f)
     l = f2()
     m = ModuleType(f.__name__)
     m.__dict__.update(l)
     return m

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


Thread

Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 00:13 +1000
  Re: Namespaces are one honking great idea Random832 <random832@fastmail.com> - 2016-07-01 10:40 -0400
  Re: Namespaces are one honking great idea BartC <bc@freeuk.com> - 2016-07-01 15:49 +0100
    Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-02 03:13 +1000
    Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 03:46 +1000
    Re: Namespaces are one honking great idea Rustom Mody <rustompmody@gmail.com> - 2016-07-01 19:26 -0700
  Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-01 09:00 -0700
    Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 03:10 +1000
      Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-01 12:29 -0700
        Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-02 11:07 +1000
          Re: Namespaces are one honking great idea Kevin Conway <kevinjacobconway@gmail.com> - 2016-07-02 01:50 +0000
            Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-01 19:55 -0700
            Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-03 13:22 +1000
          Re: Namespaces are one honking great idea Random832 <random832@fastmail.com> - 2016-07-02 00:59 -0400
          Re: Namespaces are one honking great idea Kevin Conway <kevinjacobconway@gmail.com> - 2016-07-02 15:34 +0000
            Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-03 13:44 +1000
              Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-02 22:14 -0700
              Re: Namespaces are one honking great idea Kevin Conway <kevinjacobconway@gmail.com> - 2016-07-03 22:02 +0000
                Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-03 16:21 -0700
                Re: Namespaces are one honking great idea BartC <bc@freeuk.com> - 2016-07-04 01:19 +0100
              Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-03 21:01 -0700
          Re: Namespaces are one honking great idea Ethan Furman <ethan@stoneleaf.us> - 2016-07-02 16:28 -0700
  Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-04 21:37 +1000
    Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-04 15:09 -0700
      Re: Namespaces are one honking great idea Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-04 15:11 -0700
  Re: Namespaces are one honking great idea jmp <jeanmichel@sequans.com> - 2016-07-04 17:58 +0200
  Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-05 12:58 +1000
    Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-05 13:35 +1000
      Re: Namespaces are one honking great idea Chris Angelico <rosuav@gmail.com> - 2016-07-05 13:47 +1000
        Re: Namespaces are one honking great idea Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-07-05 16:09 +1000
          A nestedmodule decorator (Re: Namespaces are one honking great idea) Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-07-05 21:10 +1200
            Improved nestedmodule decorator implementation Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-07-05 21:31 +1200
              Re: Improved nestedmodule decorator implementation Steven D'Aprano <steve@pearwood.info> - 2016-07-05 20:54 +1000
  Re: Namespaces are one honking great idea Steven D'Aprano <steve@pearwood.info> - 2016-07-05 12:34 +1000
  Re: Namespaces are one honking great idea jmp <jeanmichel@sequans.com> - 2016-07-04 13:23 +0200
  Re: Namespaces are one honking great idea carlosjosepita@gmail.com - 2016-07-09 07:05 -0700

csiph-web