[Python-Dev] deleting setdefaultencoding iin site.py is evil

Chris Withers chris at simplistix.co.uk
Thu Aug 27 01:59:35 CEST 2009


M.-A. Lemburg wrote:
> Let's look at this from another angle: sys.setdefaultencoding()
> is only made available for use in site.py. 

...see this:

http://mail.python.org/pipermail/python-dev/2009-August/091391.html

I would like to use sitecustomize.py for all the very good reasons given 
in this thread:

- I don't want to change the default encoding for every project that 
uses the python installation in question

- I don't even want to change the default encoding for every python 
script run by the current user

- I only want to change the default encoding for one particular project.

Sadly, for the reasons I describe in the thread, site.py won't find a 
sitecustomize.py in this situation...

> If you use it anywhere else, you're on your own. 

No problem with that. To be specific, this is a Zope 2.12 instance 
driven by this buildout:

[instance]
recipe = zc.recipe.egg
eggs = ${buildout:eggs}
interpreter = py
entry-points=
    runzope=Zope2.Startup.run:run
    zopectl=Zope2.Startup.zopectl:main
scripts = runzope zopectl
initialization =
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    sys.argv[1:1] = ['-C','${buildout:directory}/etc/instance.conf']

The call to sys.setdefaultencoding is *very* early in the scheme of 
things... The runzope script that gets run only has some sys.path 
manipulation before sys.setdefaultencoding gets called. What problems 
could there be by calling sys.setdefaultencoding there?

> Such usage
> is not supported and may very well break your interpreter 

Can you give an example?

> or
> cause data corruption (the default encoded versions of Unicode
> objects are cached inside the objects).

When called as early as in the above script, what objects would have 
encoded strings cached in them?

> Now, in your particular case, you're probably better off just
> tweaking site.py directly in your custom Python interpreter
> rather than relying on sitecustomize.py (see setencoding() in
> site.py).

Why?

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


More information about the Python-Dev mailing list