8000 Bug #2388: Fix gcc warnings when compiling with --enable-unicode=ucs4. · python/cpython@d918e4e · GitHub
[go: up one dir, main page]

Skip to content

Commit d918e4e

Browse files
committed
Bug #2388: Fix gcc warnings when compiling with --enable-unicode=ucs4.
1 parent 295814e commit d918e4e

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

Objects/stringlib/formatter.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,19 @@ FORMAT_STRING(PyObject* value, PyObject* args)
785785
break;
786786
default:
787787
/* unknown */
788-
PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
789-
format.type);
788+
#if STRINGLIB_IS_UNICODE
789+
/* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
790+
hence the two cases. If it is char, gcc complains that the
791+
condition below is always true, hence the ifdef. */
792+
if (format.type > 32 && format.type <128)
793+
#endif
794+
PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
795+
(char)format.type);
796+
#if STRINGLIB_IS_UNICODE
797+
else
798+
PyErr_Format(PyExc_ValueError, "Unknown conversion type '\\x%x'",
799+
(unsigned int)format.type);
800+
#endif
790801
goto done;
791802
}
792803

Objects/stringlib/string_format.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,17 @@ do_conversion(PyObject *obj, STRINGLIB_CHAR conversion)
740740
case 's':
741741
return STRINGLIB_TOSTR(obj);
742742
default:
743-
PyErr_Format(PyExc_ValueError,
744-
"Unknown converion specifier %c",
745-
conversion);
743+
if (conversion > 32 && conversion < 127) {
744+
/* It's the ASCII subrange; casting to char is safe
745+
(assuming the execution character set is an ASCII
746+
superset). */
747+
PyErr_Format(PyExc_ValueError,
748+
"Unknown conversion specifier %c",
749+
(char)conversion);
750+
} else
751+
PyErr_Format(PyExc_ValueError,
752+
"Unknown conversion specifier \\x%x",
753+
(unsigned int)conversion);
746754
return NULL;
747755
}
748756
}

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8644,7 +8644,7 @@ PyObject *PyUnicode_Format(PyObject *format,
86448644
if (!isnumok) {
86458645
PyErr_Format(PyExc_TypeError,
86468646
"%%%c format: a number is required, "
8647-
"not %.200s", c, Py_TYPE(v)->tp_name);
8647+
"not %.200s", (char)c, Py_TYPE(v)->tp_name);
86488648
goto onError;
86498649
}
86508650
if (flags & F_ZERO)

0 commit comments

Comments
 (0)
0