summaryrefslogtreecommitdiffstats
path: root/Python/formatter_unicode.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/formatter_unicode.c')
-rw-r--r--Python/formatter_unicode.c39
1 files changed, 19 insertions, 20 deletions
diff --git a/Python/formatter_unicode.c b/Python/formatter_unicode.c
index e3a8149..617d58b 100644
--- a/Python/formatter_unicode.c
+++ b/Python/formatter_unicode.c
@@ -118,7 +118,7 @@ typedef struct {
} InternalFormatSpec;
#if 0
-/* Occassionally useful for debugging. Should normally be commented out. */
+/* Occasionally useful for debugging. Should normally be commented out. */
static void
DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
{
@@ -347,9 +347,11 @@ fill_padding(_PyUnicodeWriter *writer,
/************************************************************************/
/* Locale type codes. */
-#define LT_CURRENT_LOCALE 0
-#define LT_DEFAULT_LOCALE 1
-#define LT_NO_LOCALE 2
+enum LocaleType {
+ LT_CURRENT_LOCALE,
+ LT_DEFAULT_LOCALE,
+ LT_NO_LOCALE
+};
/* Locale info needed for formatting integers and the part of floats
before and including the decimal. Note that locales only support
@@ -663,7 +665,7 @@ static char no_grouping[1] = {CHAR_MAX};
LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
none if LT_NO_LOCALE. */
static int
-get_locale_info(int type, LocaleInfo *locale_info)
+get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
{
switch (type) {
case LT_CURRENT_LOCALE: {
@@ -676,21 +678,16 @@ get_locale_info(int type, LocaleInfo *locale_info)
locale_info->thousands_sep = PyUnicode_DecodeLocale(
locale_data->thousands_sep,
NULL);
- if (locale_info->thousands_sep == NULL) {
- Py_DECREF(locale_info->decimal_point);
+ if (locale_info->thousands_sep == NULL)
return -1;
- }
locale_info->grouping = locale_data->grouping;
break;
}
case LT_DEFAULT_LOCALE:
locale_info->decimal_point = PyUnicode_FromOrdinal('.');
locale_info->thousands_sep = PyUnicode_FromOrdinal(',');
- if (!locale_info->decimal_point || !locale_info->thousands_sep) {
- Py_XDECREF(locale_info->decimal_point);
- Py_XDECREF(locale_info->thousands_sep);
+ if (!locale_info->decimal_point || !locale_info->thousands_sep)
return -1;
- }
locale_info->grouping = "\3"; /* Group every 3 characters. The
(implicit) trailing 0 means repeat
infinitely. */
@@ -698,15 +695,10 @@ get_locale_info(int type, LocaleInfo *locale_info)
case LT_NO_LOCALE:
locale_info->decimal_point = PyUnicode_FromOrdinal('.');
locale_info->thousands_sep = PyUnicode_New(0, 0);
- if (!locale_info->decimal_point || !locale_info->thousands_sep) {
- Py_XDECREF(locale_info->decimal_point);
- Py_XDECREF(locale_info->thousands_sep);
+ if (!locale_info->decimal_point || !locale_info->thousands_sep)
return -1;
- }
locale_info->grouping = no_grouping;
break;
- default:
- assert(0);
}
return 0;
}
@@ -846,6 +838,13 @@ format_long_internal(PyObject *value, const InternalFormatSpec *format,
" format specifier 'c'");
goto done;
}
+ /* error to request alternate format */
+ if (format->alternate) {
+ PyErr_SetString(PyExc_ValueError,
+ "Alternate form (#) not allowed with integer"
+ " format specifier 'c'");
+ goto done;
+ }
/* taken from unicodeobject.c formatchar() */
/* Integer input truncated to a character */
@@ -1035,7 +1034,7 @@ format_float_internal(PyObject *value,
else if (type == 'r')
type = 'g';
- /* Cast "type", because if we're in unicode we need to pass a
+ /* Cast "type", because if we're in unicode we need to pass an
8-bit char. This is safe, because we've restricted what "type"
can be. */
buf = PyOS_double_to_string(val, (char)type, precision, flags,
@@ -1214,7 +1213,7 @@ format_complex_internal(PyObject *value,
else if (type == 'r')
type = 'g';
- /* Cast "type", because if we're in unicode we need to pass a
+ /* Cast "type", because if we're in unicode we need to pass an
8-bit char. This is safe, because we've restricted what "type"
can be. */
re_buf = PyOS_double_to_string(re, (char)type, precision, flags,