1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
""" The rpython-level part of locale module
"""
import sys
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.translator.tool.cbuild import ExternalCompilationInfo
from rpython.rtyper.tool import rffi_platform as platform
from rpython.rtyper.extfunc import register_external
class LocaleError(Exception):
def __init__(self, message):
self.message = message
HAVE_LANGINFO = sys.platform != 'win32'
HAVE_LIBINTL = sys.platform != 'win32'
libraries = []
if HAVE_LIBINTL:
try:
platform.verify_eci(ExternalCompilationInfo(includes=['libintl.h'],
libraries=['intl']))
libraries.append('intl')
except platform.CompilationError:
try:
platform.verify_eci(ExternalCompilationInfo(includes=['libintl.h']))
except platform.CompilationError:
HAVE_LIBINTL = False
class CConfig:
includes = ['locale.h', 'limits.h', 'ctype.h', 'wchar.h']
libraries = libraries
if HAVE_LANGINFO:
includes += ['langinfo.h']
if HAVE_LIBINTL:
includes += ['libintl.h']
if sys.platform == 'win32':
includes += ['windows.h']
_compilation_info_ = ExternalCompilationInfo(
includes=includes, libraries=libraries
)
HAVE_BIND_TEXTDOMAIN_CODESET = platform.Has('bind_textdomain_codeset')
lconv = platform.Struct("struct lconv", [
# Numeric (non-monetary) information.
("decimal_point", rffi.CCHARP), # Decimal point character.
("thousands_sep", rffi.CCHARP), # Thousands separator.
## Each element is the number of digits in each group;
## elements with higher indices are farther left.
## An element with value CHAR_MAX means that no further grouping is done.
## An element with value 0 means that the previous element is used
## for all groups farther left. */
("grouping", rffi.CCHARP),
## Monetary information.
## First three chars are a currency symbol from ISO 4217.
## Fourth char is the separator. Fifth char is '\0'.
("int_curr_symbol", rffi.CCHARP),
("currency_symbol", rffi.CCHARP), # Local currency symbol.
("mon_decimal_point", rffi.CCHARP), # Decimal point character.
("mon_thousands_sep", rffi.CCHARP), # Thousands separator.
("mon_grouping", rffi.CCHARP), # Like `grouping' element (above).
("positive_sign", rffi.CCHARP), # Sign for positive values.
("negative_sign", rffi.CCHARP), # Sign for negative values.
("int_frac_digits", rffi.UCHAR), # Int'l fractional digits.
("frac_digits", rffi.UCHAR), # Local fractional digits.
## 1 if currency_symbol precedes a positive value, 0 if succeeds.
("p_cs_precedes", rffi.UCHAR),
## 1 iff a space separates currency_symbol from a positive value.
("p_sep_by_space", rffi.UCHAR),
## 1 if currency_symbol precedes a negative value, 0 if succeeds.
("n_cs_precedes", rffi.UCHAR),
## 1 iff a space separates currency_symbol from a negative value.
("n_sep_by_space", rffi.UCHAR),
## Positive and negative sign positions:
## 0 Parentheses surround the quantity and currency_symbol.
## 1 The sign string precedes the quantity and currency_symbol.
## 2 The sign string follows the quantity and currency_symbol.
## 3 The sign string immediately precedes the currency_symbol.
## 4 The sign string immediately follows the currency_symbol.
("p_sign_posn", rffi.UCHAR),
("n_sign_posn", rffi.UCHAR),
])
constants = {}
constant_names = (
'LC_CTYPE',
'LC_NUMERIC',
'LC_TIME',
'LC_COLLATE',
'LC_MONETARY',
'LC_MESSAGES',
'LC_ALL',
'LC_PAPER',
'LC_NAME',
'LC_ADDRESS',
'LC_TELEPHONE',
'LC_MEASUREMENT',
'LC_IDENTIFICATION',
'LC_MIN',
'LC_MAX',
# from limits.h
'CHAR_MAX',
)
for name in constant_names:
setattr(CConfig, name, platform.DefinedConstantInteger(name))
langinfo_names = []
if HAVE_LANGINFO:
# some of these consts have an additional #ifdef directives
# should we support them?
langinfo_names.extend('RADIXCHAR THOUSEP CRNCYSTR D_T_FMT D_FMT T_FMT '
'AM_STR PM_STR CODESET T_FMT_AMPM ERA ERA_D_FMT '
'ERA_D_T_FMT ERA_T_FMT ALT_DIGITS YESEXPR NOEXPR '
'_DATE_FMT'.split())
for i in range(1, 8):
langinfo_names.append("DAY_%d" % i)
langinfo_names.append("ABDAY_%d" % i)
for i in range(1, 13):
langinfo_names.append("MON_%d" % i)
langinfo_names.append("ABMON_%d" % i)
if sys.platform == 'win32':
langinfo_names.extend('LOCALE_USER_DEFAULT LOCALE_SISO639LANGNAME '
'LOCALE_SISO3166CTRYNAME LOCALE_IDEFAULTLANGUAGE '
''.split())
for name in langinfo_names:
setattr(CConfig, name, platform.DefinedConstantInteger(name))
class cConfig(object):
pass
for k, v in platform.configure(CConfig).items():
setattr(cConfig, k, v)
# needed to export the constants inside and outside. see __init__.py
for name in constant_names:
value = getattr(cConfig, name)
if value is not None:
constants[name] = value
for name in langinfo_names:
value = getattr(cConfig, name)
if value is not None and sys.platform != 'win32':
constants[name] = value
locals().update(constants)
HAVE_BIND_TEXTDOMAIN_CODESET = cConfig.HAVE_BIND_TEXTDOMAIN_CODESET
def external(name, args, result, calling_conv='c', **kwds):
return rffi.llexternal(name, args, result,
compilation_info=CConfig._compilation_info_,
calling_conv=calling_conv,
sandboxsafe=True, **kwds)
_lconv = lltype.Ptr(cConfig.lconv)
localeconv = external('localeconv', [], _lconv)
def numeric_formatting():
"""Specialized function to get formatting for numbers"""
return numeric_formatting_impl()
def numeric_formatting_impl():
conv = localeconv()
decimal_point = rffi.charp2str(conv.c_decimal_point)
thousands_sep = rffi.charp2str(conv.c_thousands_sep)
grouping = rffi.charp2str(conv.c_grouping)
return decimal_point, thousands_sep, grouping
register_external(numeric_formatting, [], (str, str, str),
llimpl=numeric_formatting_impl,
sandboxsafe=True)
_setlocale = external('setlocale', [rffi.INT, rffi.CCHARP], rffi.CCHARP)
def setlocale(category, locale):
if cConfig.LC_MAX is not None:
if not cConfig.LC_MIN <= category <= cConfig.LC_MAX:
raise LocaleError("invalid locale category")
ll_result = _setlocale(rffi.cast(rffi.INT, category), locale)
if not ll_result:
raise LocaleError("unsupported locale setting")
return rffi.charp2str(ll_result)
isalpha = external('isalpha', [rffi.INT], rffi.INT)
isupper = external('isupper', [rffi.INT], rffi.INT)
toupper = external('toupper', [rffi.INT], rffi.INT)
islower = external('islower', [rffi.INT], rffi.INT)
tolower = external('tolower', [rffi.INT], rffi.INT)
isalnum = external('isalnum', [rffi.INT], rffi.INT)
if HAVE_LANGINFO:
_nl_langinfo = external('nl_langinfo', [rffi.INT], rffi.CCHARP)
def nl_langinfo(key):
if key in constants.values():
return rffi.charp2str(_nl_langinfo(rffi.cast(rffi.INT, key)))
raise ValueError
#___________________________________________________________________
# getdefaultlocale() implementation for Windows
if sys.platform == 'win32':
from rpython.rlib import rwin32
LCID = LCTYPE = rwin32.DWORD
GetACP = external('GetACP',
[], rffi.INT,
calling_conv='win')
GetLocaleInfo = external('GetLocaleInfoA',
[LCID, LCTYPE, rwin32.LPSTR, rffi.INT], rffi.INT,
calling_conv='win')
def getdefaultlocale():
encoding = "cp%d" % GetACP()
BUFSIZE = 50
buf_lang = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw')
buf_country = lltype.malloc(rffi.CCHARP.TO, BUFSIZE, flavor='raw')
try:
if (GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT,
cConfig.LOCALE_SISO639LANGNAME,
buf_lang, BUFSIZE) and
GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT,
cConfig.LOCALE_SISO3166CTRYNAME,
buf_country, BUFSIZE)):
lang = rffi.charp2str(buf_lang)
country = rffi.charp2str(buf_country)
language = "%s_%s" % (lang, country)
# If we end up here, this windows version didn't know about
# ISO639/ISO3166 names (it's probably Windows 95). Return the
# Windows language identifier instead (a hexadecimal number)
elif GetLocaleInfo(cConfig.LOCALE_USER_DEFAULT,
cConfig.LOCALE_IDEFAULTLANGUAGE,
buf_lang, BUFSIZE):
lang = rffi.charp2str(buf_lang)
language = "0x%s" % (lang,)
else:
language = None
finally:
lltype.free(buf_lang, flavor='raw')
lltype.free(buf_country, flavor='raw')
return language, encoding
|