[Python-Dev] Scope, not context? (was Re: PEP 550 v3 naming)

Eric Snow ericsnowcurrently at gmail.com
Fri Aug 25 11:15:03 EDT 2017


On Fri, Aug 25, 2017 at 8:18 AM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> Another idea:
>
> 1. We alter PyModule to make it possible to add properties (descriptor
> protocol, or we implement custom __getattr__). I think we can make it
> so that only sys module would be able to actually use it, so it's not
> going to be a new feature -- just a hack for CPython.

FWIW, I've been toying with a similar problem and solution for a
while.  I'd like to clean up the sys module, including grouping some
of the attributes (e.g. the import state), turn the get/set pairs into
properties, and deprecate direct usage of some of the attributes.

Though supporting descriptors on module objects would work [1] and be
useful  (particularly deprecating module attrs), it's sufficiently
worthy of a PEP that I haven't taken the time.  Instead, the approach
I settled on was to rename sys to _sys and add a sys written in Python
that proxies _sys.  Here's a rough first pass:

https://github.com/ericsnowcurrently/cpython/tree/sys-module

It's effectively the same thing as ModuleType supporting descriptors,
but specific only to sys.  One problem with both approaches is that
we'd be changing the type of the sys module.  There's a relatively
common idiom in the stdlib (and elsewhere) of using "type(sys)" to get
ModuleType.  Changing the type of the sys module breaks that.

-eric


[1] This is doable with a custom __getattribute__ on ModuleType,
though it will impact attribute lookup on all modules.  I suppose
there could be a subclass that does the right thing...  Anyway, this
is more python-ideas territory.


More information about the Python-Dev mailing list