@@ -48,10 +48,11 @@ typedef struct _PyEncoderObject {
48
48
PyObject * indent ;
49
49
PyObject * key_separator ;
50
50
PyObject * item_separator ;
51
- char sort_keys ;
52
- char skipkeys ;
53
- int allow_nan ;
54
- int (* fast_encode )(PyUnicodeWriter * , PyObject * );
51
+ bool sort_keys ;
52
+ bool skipkeys ;
53
+ bool allow_nan ;
54
+ bool fast_encode ;
55
+ bool ensure_ascii ; /* used only when fast_encode == true */
55
56
} PyEncoderObject ;
56
57
57
58
#define PyEncoderObject_CAST (op ) ((PyEncoderObject *)(op))
@@ -303,55 +304,9 @@ escape_unicode(PyObject *pystr)
303
304
return rval ;
304
305
}
305
306
306
- // Take a PyUnicode pystr and write an ASCII-only escaped string to writer.
307
- // Same to ascii_escape_unicode(), but write to PyUnicodeWriter instead of
308
- // return Unicode object.
309
- static int
310
- write_escaped_ascii (PyUnicodeWriter * writer , PyObject * pystr )
311
- {
312
- Py_ssize_t i ;
313
- Py_ssize_t input_chars ;
314
- Py_ssize_t chars ;
315
- Py_ssize_t copy_len = 0 ;
316
- const void * input ;
317
- int kind ;
318
- int ret ;
319
- unsigned char buf [12 ];
320
-
321
- input_chars = PyUnicode_GET_LENGTH (pystr );
322
- input = PyUnicode_DATA (pystr );
323
- kind = PyUnicode_KIND (pystr );
324
-
325
- ret = PyUnicodeWriter_WriteChar (writer , '"' );
326
- if (ret ) return ret ;
327
-
328
- for (i = 0 ; i < input_chars ; i ++ ) {
329
- Py_UCS4 c = PyUnicode_READ (kind , input , i );
330
- if (S_CHAR (c )) {
331
- copy_len ++ ;
332
- }
333
- else {
334
- ret = PyUnicodeWriter_WriteSubstring (writer , pystr , i - copy_len , i );
335
- if (ret ) return ret ;
336
- copy_len = 0 ;
337
-
338
- chars = ascii_escape_unichar (c , buf , 0 );
339
- ret = PyUnicodeWriter_WriteUTF8 (writer , (const char * )buf , chars );
340
- if (ret ) return ret ;
341
- }
342
- }
343
-
344
- ret = PyUnicodeWriter_WriteSubstring (writer , pystr , i - copy_len , i );
345
- if (ret ) return ret ;
346
-
347
- return PyUnicodeWriter_WriteChar (writer , '"' );
348
- }
349
-
350
307
// Take a PyUnicode pystr and write an escaped string to writer.
351
- // Same to escape_unicode(), but write to PyUnicodeWriter instead of
352
- // return Unicode object.
353
308
static int
354
- write_escaped_unicode (PyUnicodeWriter * writer , PyObject * pystr )
309
+ write_escaped_unicode (PyUnicodeWriter * writer , PyObject * pystr , bool ascii_only )
355
310
{
356
311
Py_ssize_t i ;
357
312
Py_ssize_t input_chars ;
@@ -371,7 +326,7 @@ write_escaped_unicode(PyUnicodeWriter *writer, PyObject *pystr)
371
326
372
327
for (i = 0 ; i < input_chars ; i ++ ) {
373
328
Py_UCS4 c = PyUnicode_READ (kind , input , i );
374
- if (c <= 0x1f || c == '\\' || c == '"' ) {
329
+ if (c <= 0x1f || c == '\\' || c == '"' || ( ascii_only && c >= 0x7f ) ) {
375
330
ret = PyUnicodeWriter_WriteSubstring (writer , pystr , i - copy_len , i );
376
331
if (ret ) return ret ;
377
332
copy_len = 0 ;
@@ -1338,15 +1293,17 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1338
1293
s -> sort_keys = sort_keys ;
1339
1294
s -> skipkeys = skipkeys ;
1340
1295
s -> allow_nan = allow_nan ;
1341
- s -> fast_encode = NULL ;
1296
+ s -> fast_encode = false;
1297
+ s -> ensure_ascii = false;
1342
1298
1343
1299
if (PyCFunction_Check (s -> encoder )) {
1344
1300
PyCFunction f = PyCFunction_GetFunction (s -> encoder );
1345
1301
if (f == py_encode_basestring_ascii ){
1346
- s -> fast_encode = write_escaped_ascii ;
1302
+ s -> fast_encode = true;
1303
+ s -> ensure_ascii = true;
1347
1304
}
1348
1305
else if (f == py_encode_basestring ) {
1349
- s -> fast_encode = write_escaped_unicode ;
1306
+ s -> fast_encode = true ;
1350
1307
}
1351
1308
}
1352
1309
@@ -1540,7 +1497,7 @@ static int
1540
1497
encoder_write_string (PyEncoderObject * s , PyUnicodeWriter * writer , PyObject * obj )
1541
1498
{
1542
1499
if (s -> fast_encode ) {
1543
- return s -> fast_encode (writer , obj );
1500
+ return write_escaped_unicode (writer , obj , s -> ensure_ascii );
1544
1501
}
1545
1502
1546
1503
/* Return the JSON representation of a string */
0 commit comments