Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 990bbe9

Browse files
committed
Marc-Andre Lemburg <[email protected]>:
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.
1 parent 5431bc3 commit 990bbe9

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

Lib/site.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,45 @@ def addpackage(sitedir, name):
119119
__builtin__.quit = __builtin__.exit = exit
120120
del exit
121121

122+
#
123+
# Set the string encoding used by the Unicode implementation to the
124+
# encoding used by the default locale of this system. If the default
125+
# encoding cannot be determined or is unkown, it defaults to 'ascii'.
126+
#
127+
def locale_aware_defaultencoding():
128+
import locale
129+
code, encoding = locale.get_default()
130+
if encoding is None:
131+
encoding = 'ascii'
132+
try:
133+
sys.setdefaultencoding(encoding)
134+
except LookupError:
135+
sys.setdefaultencoding('ascii')
136+
137+
if 1:
138+
# Enable to support locale aware default string encodings.
139+
locale_aware_defaultencoding()
140+
elif 0:
141+
# Enable to switch off string to Unicode coercion and implicit
142+
# Unicode to string conversion.
143+
sys.setdefaultencoding('undefined')
144+
elif 0:
145+
# Enable to hard-code a site specific default string encoding.
146+
sys.setdefaultencoding('ascii')
147+
148+
#
149+
# Run custom site specific code, if available.
150+
#
122151
try:
123-
import sitecustomize # Run arbitrary site specific code
152+
import sitecustomize
124153
except ImportError:
125-
pass # No site customization module
154+
pass
155+
156+
#
157+
# Remove sys.setdefaultencoding() so that users cannot change the
158+
# encoding after initialization.
159+
#
160+
del sys.setdefaultencoding
126161

127162
def _test():
128163
print "sys.path = ["

0 commit comments

Comments
 (0)