summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2000-06-07 09:12:09 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2000-06-07 09:12:09 (GMT)
commit990bbe90d5537065d80dac3a1a4d44ee19ad8bdd (patch)
treea701d715a70abfabda264ba0dfb50c1635d65b2b /Lib
parent5431bc36026c9cd61046b65a0bd343f37f42c410 (diff)
downloadcpython-990bbe90d5537065d80dac3a1a4d44ee19ad8bdd.zip
cpython-990bbe90d5537065d80dac3a1a4d44ee19ad8bdd.tar.gz
cpython-990bbe90d5537065d80dac3a1a4d44ee19ad8bdd.tar.bz2
Marc-Andre Lemburg <mal@lemburg.com>:
Added support to set the default encoding of strings at startup time to the values defined by the C locale. The sys.setdefaultencoding() API is deleted after having set up the encoding, so that user code cannot subsequentely change the setting. This effectively means that only site.py may alter the default setting.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/site.py39
1 files changed, 37 insertions, 2 deletions
diff --git a/Lib/site.py b/Lib/site.py
index 81776571..dabd364 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -119,10 +119,45 @@ import __builtin__
__builtin__.quit = __builtin__.exit = exit
del exit
+#
+# Set the string encoding used by the Unicode implementation to the
+# encoding used by the default locale of this system. If the default
+# encoding cannot be determined or is unkown, it defaults to 'ascii'.
+#
+def locale_aware_defaultencoding():
+ import locale
+ code, encoding = locale.get_default()
+ if encoding is None:
+ encoding = 'ascii'
+ try:
+ sys.setdefaultencoding(encoding)
+ except LookupError:
+ sys.setdefaultencoding('ascii')
+
+if 1:
+ # Enable to support locale aware default string encodings.
+ locale_aware_defaultencoding()
+elif 0:
+ # Enable to switch off string to Unicode coercion and implicit
+ # Unicode to string conversion.
+ sys.setdefaultencoding('undefined')
+elif 0:
+ # Enable to hard-code a site specific default string encoding.
+ sys.setdefaultencoding('ascii')
+
+#
+# Run custom site specific code, if available.
+#
try:
- import sitecustomize # Run arbitrary site specific code
+ import sitecustomize
except ImportError:
- pass # No site customization module
+ pass
+
+#
+# Remove sys.setdefaultencoding() so that users cannot change the
+# encoding after initialization.
+#
+del sys.setdefaultencoding
def _test():
print "sys.path = ["