11#include "parts.h"
22
3+ /*
4+ * The Codecs C API assume that 'encoding' is not NULL, lest
5+ * it uses PyErr_BadArgument() to set a TypeError exception.
6+ *
7+ * In this file, we allow to call the functions using None
8+ * as NULL to explicitly check this behaviour.
9+ */
10+
311// === Codecs registration and un-registration ================================
412
513static PyObject *
@@ -23,8 +31,8 @@ codec_unregister(PyObject *Py_UNUSED(module), PyObject *search_function)
2331static PyObject *
2432codec_known_encoding (PyObject * Py_UNUSED (module ), PyObject * args )
2533{
26- const char * encoding ; // should not be NULL
27- if (!PyArg_ParseTuple (args , "s " , & encoding )) {
34+ const char * encoding ; // should not be NULL (see top-file comment)
35+ if (!PyArg_ParseTuple (args , "z " , & encoding )) {
2836 return NULL ;
2937 }
3038 return PyCodec_KnownEncoding (encoding ) ? Py_True : Py_False ;
@@ -36,9 +44,9 @@ static PyObject *
3644codec_encode (PyObject * Py_UNUSED (module ), PyObject * args )
3745{
3846 PyObject * input ;
39- const char * encoding ; // should not be NULL
47+ const char * encoding ; // should not be NULL (see top-file comment)
4048 const char * errors ; // can be NULL
41- if (!PyArg_ParseTuple (args , "O|sz " , & input , & encoding , & errors )) {
49+ if (!PyArg_ParseTuple (args , "O|zz " , & input , & encoding , & errors )) {
4250 return NULL ;
4351 }
4452 return PyCodec_Encode (input , encoding , errors );
@@ -48,9 +56,9 @@ static PyObject *
4856codec_decode (PyObject * Py_UNUSED (module ), PyObject * args )
4957{
5058 PyObject * input ;
51- const char * encoding ; // should not be NULL
59+ const char * encoding ; // should not be NULL (see top-file comment)
5260 const char * errors ; // can be NULL
53- if (!PyArg_ParseTuple (args , "O|sz " , & input , & encoding , & errors )) {
61+ if (!PyArg_ParseTuple (args , "O|zz " , & input , & encoding , & errors )) {
5462 return NULL ;
5563 }
5664 return PyCodec_Decode (input , encoding , errors );
@@ -59,8 +67,8 @@ codec_decode(PyObject *Py_UNUSED(module), PyObject *args)
5967static PyObject *
6068codec_encoder (PyObject * Py_UNUSED (module ), PyObject * args )
6169{
62- const char * encoding ; // should not be NULL
63- if (!PyArg_ParseTuple (args , "s " , & encoding )) {
70+ const char * encoding ; // should not be NULL (see top-file comment)
71+ if (!PyArg_ParseTuple (args , "z " , & encoding )) {
6472 return NULL ;
6573 }
6674 return PyCodec_Encoder (encoding );
@@ -69,8 +77,8 @@ codec_encoder(PyObject *Py_UNUSED(module), PyObject *args)
6977static PyObject *
7078codec_decoder (PyObject * Py_UNUSED (module ), PyObject * args )
7179{
72- const char * encoding ; // should not be NULL
73- if (!PyArg_ParseTuple (args , "s " , & encoding )) {
80+ const char * encoding ; // should not be NULL (see top-file comment)
81+ if (!PyArg_ParseTuple (args , "z " , & encoding )) {
7482 return NULL ;
7583 }
7684 return PyCodec_Decoder (encoding );
@@ -79,9 +87,9 @@ codec_decoder(PyObject *Py_UNUSED(module), PyObject *args)
7987static PyObject *
8088codec_incremental_encoder (PyObject * Py_UNUSED (module ), PyObject * args )
8189{
82- const char * encoding ; // should not be NULL
83- const char * errors ; // should not be NULL
84- if (!PyArg_ParseTuple (args , "ss " , & encoding , & errors )) {
90+ const char * encoding ; // should not be NULL (see top-file comment)
91+ const char * errors ; // can be NULL
92+ if (!PyArg_ParseTuple (args , "zz " , & encoding , & errors )) {
8593 return NULL ;
8694 }
8795 return PyCodec_IncrementalEncoder (encoding , errors );
@@ -90,9 +98,9 @@ codec_incremental_encoder(PyObject *Py_UNUSED(module), PyObject *args)
9098static PyObject *
9199codec_incremental_decoder (PyObject * Py_UNUSED (module ), PyObject * args )
92100{
93- const char * encoding ; // should not be NULL
94- const char * errors ; // should not be NULL
95- if (!PyArg_ParseTuple (args , "ss " , & encoding , & errors )) {
101+ const char * encoding ; // should not be NULL (see top-file comment)
102+ const char * errors ; // can be NULL
103+ if (!PyArg_ParseTuple (args , "zz " , & encoding , & errors )) {
96104 return NULL ;
97105 }
98106 return PyCodec_IncrementalDecoder (encoding , errors );
@@ -101,10 +109,10 @@ codec_incremental_decoder(PyObject *Py_UNUSED(module), PyObject *args)
101109static PyObject *
102110codec_stream_reader (PyObject * Py_UNUSED (module ), PyObject * args )
103111{
104- const char * encoding ; // should not be NULL
112+ const char * encoding ; // should not be NULL (see top-file comment)
105113 PyObject * stream ;
106- const char * errors ; // should not be NULL
107- if (!PyArg_ParseTuple (args , "sOs " , & encoding , & stream , & errors )) {
114+ const char * errors ; // can be NULL
115+ if (!PyArg_ParseTuple (args , "zOz " , & encoding , & stream , & errors )) {
108116 return NULL ;
109117 }
110118 return PyCodec_StreamReader (encoding , stream , errors );
@@ -113,10 +121,10 @@ codec_stream_reader(PyObject *Py_UNUSED(module), PyObject *args)
113121static PyObject *
114122codec_stream_writer (PyObject * Py_UNUSED (module ), PyObject * args )
115123{
116- const char * encoding ; // should not be NULL
124+ const char * encoding ; // should not be NULL (see top-file comment)
117125 PyObject * stream ;
118- const char * errors ; // should not be NULL
119- if (!PyArg_ParseTuple (args , "sOs " , & encoding , & stream , & errors )) {
126+ const char * errors ; // can be NULL
127+ if (!PyArg_ParseTuple (args , "zOz " , & encoding , & stream , & errors )) {
120128 return NULL ;
121129 }
122130 return PyCodec_StreamWriter (encoding , stream , errors );
@@ -127,7 +135,7 @@ codec_stream_writer(PyObject *Py_UNUSED(module), PyObject *args)
127135static PyObject *
128136codec_register_error (PyObject * Py_UNUSED (module ), PyObject * args )
129137{
130- const char * encoding ; // should not be NULL
138+ const char * encoding ; // must not be NULL
131139 PyObject * error ;
132140 if (!PyArg_ParseTuple (args , "sO" , & encoding , & error )) {
133141 return NULL ;
0 commit comments