You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2023 lines
62 KiB

12 years ago
  1. #include "Python.h"
  2. #include "structmember.h"
  3. #include "accu.h"
  4. #ifdef __GNUC__
  5. #define UNUSED __attribute__((__unused__))
  6. #else
  7. #define UNUSED
  8. #endif
  9. #define PyScanner_Check(op) PyObject_TypeCheck(op, &PyScannerType)
  10. #define PyScanner_CheckExact(op) (Py_TYPE(op) == &PyScannerType)
  11. #define PyEncoder_Check(op) PyObject_TypeCheck(op, &PyEncoderType)
  12. #define PyEncoder_CheckExact(op) (Py_TYPE(op) == &PyEncoderType)
  13. static PyTypeObject PyScannerType;
  14. static PyTypeObject PyEncoderType;
  15. typedef struct _PyScannerObject {
  16. PyObject_HEAD
  17. PyObject *strict;
  18. PyObject *object_hook;
  19. PyObject *object_pairs_hook;
  20. PyObject *parse_float;
  21. PyObject *parse_int;
  22. PyObject *parse_constant;
  23. PyObject *memo;
  24. } PyScannerObject;
  25. static PyMemberDef scanner_members[] = {
  26. {"strict", T_OBJECT, offsetof(PyScannerObject, strict), READONLY, "strict"},
  27. {"object_hook", T_OBJECT, offsetof(PyScannerObject, object_hook), READONLY, "object_hook"},
  28. {"object_pairs_hook", T_OBJECT, offsetof(PyScannerObject, object_pairs_hook), READONLY},
  29. {"parse_float", T_OBJECT, offsetof(PyScannerObject, parse_float), READONLY, "parse_float"},
  30. {"parse_int", T_OBJECT, offsetof(PyScannerObject, parse_int), READONLY, "parse_int"},
  31. {"parse_constant", T_OBJECT, offsetof(PyScannerObject, parse_constant), READONLY, "parse_constant"},
  32. {NULL}
  33. };
  34. typedef struct _PyEncoderObject {
  35. PyObject_HEAD
  36. PyObject *markers;
  37. PyObject *defaultfn;
  38. PyObject *encoder;
  39. PyObject *indent;
  40. PyObject *key_separator;
  41. PyObject *item_separator;
  42. PyObject *sort_keys;
  43. PyObject *skipkeys;
  44. PyCFunction fast_encode;
  45. int allow_nan;
  46. } PyEncoderObject;
  47. static PyMemberDef encoder_members[] = {
  48. {"markers", T_OBJECT, offsetof(PyEncoderObject, markers), READONLY, "markers"},
  49. {"default", T_OBJECT, offsetof(PyEncoderObject, defaultfn), READONLY, "default"},
  50. {"encoder", T_OBJECT, offsetof(PyEncoderObject, encoder), READONLY, "encoder"},
  51. {"indent", T_OBJECT, offsetof(PyEncoderObject, indent), READONLY, "indent"},
  52. {"key_separator", T_OBJECT, offsetof(PyEncoderObject, key_separator), READONLY, "key_separator"},
  53. {"item_separator", T_OBJECT, offsetof(PyEncoderObject, item_separator), READONLY, "item_separator"},
  54. {"sort_keys", T_OBJECT, offsetof(PyEncoderObject, sort_keys), READONLY, "sort_keys"},
  55. {"skipkeys", T_OBJECT, offsetof(PyEncoderObject, skipkeys), READONLY, "skipkeys"},
  56. {NULL}
  57. };
  58. static PyObject *
  59. join_list_unicode(PyObject *lst)
  60. {
  61. /* return u''.join(lst) */
  62. static PyObject *sep = NULL;
  63. if (sep == NULL) {
  64. sep = PyUnicode_FromStringAndSize("", 0);
  65. if (sep == NULL)
  66. return NULL;
  67. }
  68. return PyUnicode_Join(sep, lst);
  69. }
  70. /* Forward decls */
  71. static PyObject *
  72. ascii_escape_unicode(PyObject *pystr);
  73. static PyObject *
  74. py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr);
  75. void init_json(void);
  76. static PyObject *
  77. scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr);
  78. static PyObject *
  79. _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx);
  80. static PyObject *
  81. scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
  82. static int
  83. scanner_init(PyObject *self, PyObject *args, PyObject *kwds);
  84. static void
  85. scanner_dealloc(PyObject *self);
  86. static int
  87. scanner_clear(PyObject *self);
  88. static PyObject *
  89. encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
  90. static int
  91. encoder_init(PyObject *self, PyObject *args, PyObject *kwds);
  92. static void
  93. encoder_dealloc(PyObject *self);
  94. static int
  95. encoder_clear(PyObject *self);
  96. static int
  97. encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc, PyObject *seq, Py_ssize_t indent_level);
  98. static int
  99. encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, PyObject *obj, Py_ssize_t indent_level);
  100. static int
  101. encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, PyObject *dct, Py_ssize_t indent_level);
  102. static PyObject *
  103. _encoded_const(PyObject *obj);
  104. static void
  105. raise_errmsg(char *msg, PyObject *s, Py_ssize_t end);
  106. static PyObject *
  107. encoder_encode_string(PyEncoderObject *s, PyObject *obj);
  108. static PyObject *
  109. encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj);
  110. static PyObject *
  111. encoder_encode_float(PyEncoderObject *s, PyObject *obj);
  112. #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"')
  113. #define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
  114. static Py_ssize_t
  115. ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
  116. {
  117. /* Escape unicode code point c to ASCII escape sequences
  118. in char *output. output must have at least 12 bytes unused to
  119. accommodate an escaped surrogate pair "\uXXXX\uXXXX" */
  120. output[chars++] = '\\';
  121. switch (c) {
  122. case '\\': output[chars++] = c; break;
  123. case '"': output[chars++] = c; break;
  124. case '\b': output[chars++] = 'b'; break;
  125. case '\f': output[chars++] = 'f'; break;
  126. case '\n': output[chars++] = 'n'; break;
  127. case '\r': output[chars++] = 'r'; break;
  128. case '\t': output[chars++] = 't'; break;
  129. default:
  130. if (c >= 0x10000) {
  131. /* UTF-16 surrogate pair */
  132. Py_UCS4 v = Py_UNICODE_HIGH_SURROGATE(c);
  133. output[chars++] = 'u';
  134. output[chars++] = Py_hexdigits[(v >> 12) & 0xf];
  135. output[chars++] = Py_hexdigits[(v >> 8) & 0xf];
  136. output[chars++] = Py_hexdigits[(v >> 4) & 0xf];
  137. output[chars++] = Py_hexdigits[(v ) & 0xf];
  138. c = Py_UNICODE_LOW_SURROGATE(c);
  139. output[chars++] = '\\';
  140. }
  141. output[chars++] = 'u';
  142. output[chars++] = Py_hexdigits[(c >> 12) & 0xf];
  143. output[chars++] = Py_hexdigits[(c >> 8) & 0xf];
  144. output[chars++] = Py_hexdigits[(c >> 4) & 0xf];
  145. output[chars++] = Py_hexdigits[(c ) & 0xf];
  146. }
  147. return chars;
  148. }
  149. static PyObject *
  150. ascii_escape_unicode(PyObject *pystr)
  151. {
  152. /* Take a PyUnicode pystr and return a new ASCII-only escaped PyUnicode */
  153. Py_ssize_t i;
  154. Py_ssize_t input_chars;
  155. Py_ssize_t output_size;
  156. Py_ssize_t chars;
  157. PyObject *rval;
  158. void *input;
  159. unsigned char *output;
  160. int kind;
  161. if (PyUnicode_READY(pystr) == -1)
  162. return NULL;
  163. input_chars = PyUnicode_GET_LENGTH(pystr);
  164. input = PyUnicode_DATA(pystr);
  165. kind = PyUnicode_KIND(pystr);
  166. /* Compute the output size */
  167. for (i = 0, output_size = 2; i < input_chars; i++) {
  168. Py_UCS4 c = PyUnicode_READ(kind, input, i);
  169. Py_ssize_t d;
  170. if (S_CHAR(c)) {
  171. d = 1;
  172. }
  173. else {
  174. switch(c) {
  175. case '\\': case '"': case '\b': case '\f':
  176. case '\n': case '\r': case '\t':
  177. d = 2; break;
  178. default:
  179. d = c >= 0x10000 ? 12 : 6;
  180. }
  181. }
  182. if (output_size > PY_SSIZE_T_MAX - d) {
  183. PyErr_SetString(PyExc_OverflowError, "string is too long to escape");
  184. return NULL;
  185. }
  186. output_size += d;
  187. }
  188. rval = PyUnicode_New(output_size, 127);
  189. if (rval == NULL) {
  190. return NULL;
  191. }
  192. output = PyUnicode_1BYTE_DATA(rval);
  193. chars = 0;
  194. output[chars++] = '"';
  195. for (i = 0; i < input_chars; i++) {
  196. Py_UCS4 c = PyUnicode_READ(kind, input, i);
  197. if (S_CHAR(c)) {
  198. output[chars++] = c;
  199. }
  200. else {
  201. chars = ascii_escape_unichar(c, output, chars);
  202. }
  203. }
  204. output[chars++] = '"';
  205. #ifdef Py_DEBUG
  206. assert(_PyUnicode_CheckConsistency(rval, 1));
  207. #endif
  208. return rval;
  209. }
  210. static PyObject *
  211. escape_unicode(PyObject *pystr)
  212. {
  213. /* Take a PyUnicode pystr and return a new escaped PyUnicode */
  214. Py_ssize_t i;
  215. Py_ssize_t input_chars;
  216. Py_ssize_t output_size;
  217. Py_ssize_t chars;
  218. PyObject *rval;
  219. void *input;
  220. int kind;
  221. Py_UCS4 maxchar;
  222. if (PyUnicode_READY(pystr) == -1)
  223. return NULL;
  224. maxchar = PyUnicode_MAX_CHAR_VALUE(pystr);
  225. input_chars = PyUnicode_GET_LENGTH(pystr);
  226. input = PyUnicode_DATA(pystr);
  227. kind = PyUnicode_KIND(pystr);
  228. /* Compute the output size */
  229. for (i = 0, output_size = 2; i < input_chars; i++) {
  230. Py_UCS4 c = PyUnicode_READ(kind, input, i);
  231. Py_ssize_t d;
  232. switch (c) {
  233. case '\\': case '"': case '\b': case '\f':
  234. case '\n': case '\r': case '\t':
  235. d = 2;
  236. break;
  237. default:
  238. if (c <= 0x1f)
  239. d = 6;
  240. else
  241. d = 1;
  242. }
  243. if (output_size > PY_SSIZE_T_MAX - d) {
  244. PyErr_SetString(PyExc_OverflowError, "string is too long to escape");
  245. return NULL;
  246. }
  247. output_size += d;
  248. }
  249. rval = PyUnicode_New(output_size, maxchar);
  250. if (rval == NULL)
  251. return NULL;
  252. kind = PyUnicode_KIND(rval);
  253. #define ENCODE_OUTPUT do { \
  254. chars = 0; \
  255. output[chars++] = '"'; \
  256. for (i = 0; i < input_chars; i++) { \
  257. Py_UCS4 c = PyUnicode_READ(kind, input, i); \
  258. switch (c) { \
  259. case '\\': output[chars++] = '\\'; output[chars++] = c; break; \
  260. case '"': output[chars++] = '\\'; output[chars++] = c; break; \
  261. case '\b': output[chars++] = '\\'; output[chars++] = 'b'; break; \
  262. case '\f': output[chars++] = '\\'; output[chars++] = 'f'; break; \
  263. case '\n': output[chars++] = '\\'; output[chars++] = 'n'; break; \
  264. case '\r': output[chars++] = '\\'; output[chars++] = 'r'; break; \
  265. case '\t': output[chars++] = '\\'; output[chars++] = 't'; break; \
  266. default: \
  267. if (c <= 0x1f) { \
  268. output[chars++] = '\\'; \
  269. output[chars++] = 'u'; \
  270. output[chars++] = '0'; \
  271. output[chars++] = '0'; \
  272. output[chars++] = Py_hexdigits[(c >> 4) & 0xf]; \
  273. output[chars++] = Py_hexdigits[(c ) & 0xf]; \
  274. } else { \
  275. output[chars++] = c; \
  276. } \
  277. } \
  278. } \
  279. output[chars++] = '"'; \
  280. } while (0)
  281. if (kind == PyUnicode_1BYTE_KIND) {
  282. Py_UCS1 *output = PyUnicode_1BYTE_DATA(rval);
  283. ENCODE_OUTPUT;
  284. } else if (kind == PyUnicode_2BYTE_KIND) {
  285. Py_UCS2 *output = PyUnicode_2BYTE_DATA(rval);
  286. ENCODE_OUTPUT;
  287. } else {
  288. Py_UCS4 *output = PyUnicode_4BYTE_DATA(rval);
  289. assert(kind == PyUnicode_4BYTE_KIND);
  290. ENCODE_OUTPUT;
  291. }
  292. #undef ENCODE_OUTPUT
  293. #ifdef Py_DEBUG
  294. assert(_PyUnicode_CheckConsistency(rval, 1));
  295. #endif
  296. return rval;
  297. }
  298. static void
  299. raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
  300. {
  301. /* Use JSONDecodeError exception to raise a nice looking ValueError subclass */
  302. static PyObject *JSONDecodeError = NULL;
  303. PyObject *exc;
  304. if (JSONDecodeError == NULL) {
  305. PyObject *decoder = PyImport_ImportModule("json.decoder");
  306. if (decoder == NULL)
  307. return;
  308. JSONDecodeError = PyObject_GetAttrString(decoder, "JSONDecodeError");
  309. Py_DECREF(decoder);
  310. if (JSONDecodeError == NULL)
  311. return;
  312. }
  313. exc = PyObject_CallFunction(JSONDecodeError, "(zOn)", msg, s, end);
  314. if (exc) {
  315. PyErr_SetObject(JSONDecodeError, exc);
  316. Py_DECREF(exc);
  317. }
  318. }
  319. static void
  320. raise_stop_iteration(Py_ssize_t idx)
  321. {
  322. PyObject *value = PyLong_FromSsize_t(idx);
  323. if (value != NULL) {
  324. PyErr_SetObject(PyExc_StopIteration, value);
  325. Py_DECREF(value);
  326. }
  327. }
  328. static PyObject *
  329. _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
  330. /* return (rval, idx) tuple, stealing reference to rval */
  331. PyObject *tpl;
  332. PyObject *pyidx;
  333. /*
  334. steal a reference to rval, returns (rval, idx)
  335. */
  336. if (rval == NULL) {
  337. return NULL;
  338. }
  339. pyidx = PyLong_FromSsize_t(idx);
  340. if (pyidx == NULL) {
  341. Py_DECREF(rval);
  342. return NULL;
  343. }
  344. tpl = PyTuple_New(2);
  345. if (tpl == NULL) {
  346. Py_DECREF(pyidx);
  347. Py_DECREF(rval);
  348. return NULL;
  349. }
  350. PyTuple_SET_ITEM(tpl, 0, rval);
  351. PyTuple_SET_ITEM(tpl, 1, pyidx);
  352. return tpl;
  353. }
  354. #define APPEND_OLD_CHUNK \
  355. if (chunk != NULL) { \
  356. if (chunks == NULL) { \
  357. chunks = PyList_New(0); \
  358. if (chunks == NULL) { \
  359. goto bail; \
  360. } \
  361. } \
  362. if (PyList_Append(chunks, chunk)) { \
  363. Py_CLEAR(chunk); \
  364. goto bail; \
  365. } \
  366. Py_CLEAR(chunk); \
  367. }
  368. static PyObject *
  369. scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next_end_ptr)
  370. {
  371. /* Read the JSON string from PyUnicode pystr.
  372. end is the index of the first character after the quote.
  373. if strict is zero then literal control characters are allowed
  374. *next_end_ptr is a return-by-reference index of the character
  375. after the end quote
  376. Return value is a new PyUnicode
  377. */
  378. PyObject *rval = NULL;
  379. Py_ssize_t len;
  380. Py_ssize_t begin = end - 1;
  381. Py_ssize_t next /* = begin */;
  382. const void *buf;
  383. int kind;
  384. PyObject *chunks = NULL;
  385. PyObject *chunk = NULL;
  386. if (PyUnicode_READY(pystr) == -1)
  387. return 0;
  388. len = PyUnicode_GET_LENGTH(pystr);
  389. buf = PyUnicode_DATA(pystr);
  390. kind = PyUnicode_KIND(pystr);
  391. if (end < 0 || len < end) {
  392. PyErr_SetString(PyExc_ValueError, "end is out of bounds");
  393. goto bail;
  394. }
  395. while (1) {
  396. /* Find the end of the string or the next escape */
  397. Py_UCS4 c = 0;
  398. for (next = end; next < len; next++) {
  399. c = PyUnicode_READ(kind, buf, next);
  400. if (c == '"' || c == '\\') {
  401. break;
  402. }
  403. else if (strict && c <= 0x1f) {
  404. raise_errmsg("Invalid control character at", pystr, next);
  405. goto bail;
  406. }
  407. }
  408. if (!(c == '"' || c == '\\')) {
  409. raise_errmsg("Unterminated string starting at", pystr, begin);
  410. goto bail;
  411. }
  412. /* Pick up this chunk if it's not zero length */
  413. if (next != end) {
  414. APPEND_OLD_CHUNK
  415. chunk = PyUnicode_FromKindAndData(
  416. kind,
  417. (char*)buf + kind * end,
  418. next - end);
  419. if (chunk == NULL) {
  420. goto bail;
  421. }
  422. }
  423. next++;
  424. if (c == '"') {
  425. end = next;
  426. break;
  427. }
  428. if (next == len) {
  429. raise_errmsg("Unterminated string starting at", pystr, begin);
  430. goto bail;
  431. }
  432. c = PyUnicode_READ(kind, buf, next);
  433. if (c != 'u') {
  434. /* Non-unicode backslash escapes */
  435. end = next + 1;
  436. switch (c) {
  437. case '"': break;
  438. case '\\': break;
  439. case '/': break;
  440. case 'b': c = '\b'; break;
  441. case 'f': c = '\f'; break;
  442. case 'n': c = '\n'; break;
  443. case 'r': c = '\r'; break;
  444. case 't': c = '\t'; break;
  445. default: c = 0;
  446. }
  447. if (c == 0) {
  448. raise_errmsg("Invalid \\escape", pystr, end - 2);
  449. goto bail;
  450. }
  451. }
  452. else {
  453. c = 0;
  454. next++;
  455. end = next + 4;
  456. if (end >= len) {
  457. raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
  458. goto bail;
  459. }
  460. /* Decode 4 hex digits */
  461. for (; next < end; next++) {
  462. Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
  463. c <<= 4;
  464. switch (digit) {
  465. case '0': case '1': case '2': case '3': case '4':
  466. case '5': case '6': case '7': case '8': case '9':
  467. c |= (digit - '0'); break;
  468. case 'a': case 'b': case 'c': case 'd': case 'e':
  469. case 'f':
  470. c |= (digit - 'a' + 10); break;
  471. case 'A': case 'B': case 'C': case 'D': case 'E':
  472. case 'F':
  473. c |= (digit - 'A' + 10); break;
  474. default:
  475. raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
  476. goto bail;
  477. }
  478. }
  479. /* Surrogate pair */
  480. if (Py_UNICODE_IS_HIGH_SURROGATE(c) && end + 6 < len &&
  481. PyUnicode_READ(kind, buf, next++) == '\\' &&
  482. PyUnicode_READ(kind, buf, next++) == 'u') {
  483. Py_UCS4 c2 = 0;
  484. end += 6;
  485. /* Decode 4 hex digits */
  486. for (; next < end; next++) {
  487. Py_UCS4 digit = PyUnicode_READ(kind, buf, next);
  488. c2 <<= 4;
  489. switch (digit) {
  490. case '0': case '1': case '2': case '3': case '4':
  491. case '5': case '6': case '7': case '8': case '9':
  492. c2 |= (digit - '0'); break;
  493. case 'a': case 'b': case 'c': case 'd': case 'e':
  494. case 'f':
  495. c2 |= (digit - 'a' + 10); break;
  496. case 'A': case 'B': case 'C': case 'D': case 'E':
  497. case 'F':
  498. c2 |= (digit - 'A' + 10); break;
  499. default:
  500. raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5);
  501. goto bail;
  502. }
  503. }
  504. if (Py_UNICODE_IS_LOW_SURROGATE(c2))
  505. c = Py_UNICODE_JOIN_SURROGATES(c, c2);
  506. else
  507. end -= 6;
  508. }
  509. }
  510. APPEND_OLD_CHUNK
  511. chunk = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, &c, 1);
  512. if (chunk == NULL) {
  513. goto bail;
  514. }
  515. }
  516. if (chunks == NULL) {
  517. if (chunk != NULL)
  518. rval = chunk;
  519. else
  520. rval = PyUnicode_FromStringAndSize("", 0);
  521. }
  522. else {
  523. APPEND_OLD_CHUNK
  524. rval = join_list_unicode(chunks);
  525. if (rval == NULL) {
  526. goto bail;
  527. }
  528. Py_CLEAR(chunks);
  529. }
  530. *next_end_ptr = end;
  531. return rval;
  532. bail:
  533. *next_end_ptr = -1;
  534. Py_XDECREF(chunks);
  535. Py_XDECREF(chunk);
  536. return NULL;
  537. }
  538. PyDoc_STRVAR(pydoc_scanstring,
  539. "scanstring(string, end, strict=True) -> (string, end)\n"
  540. "\n"
  541. "Scan the string s for a JSON string. End is the index of the\n"
  542. "character in s after the quote that started the JSON string.\n"
  543. "Unescapes all valid JSON string escape sequences and raises ValueError\n"
  544. "on attempt to decode an invalid string. If strict is False then literal\n"
  545. "control characters are allowed in the string.\n"
  546. "\n"
  547. "Returns a tuple of the decoded string and the index of the character in s\n"
  548. "after the end quote."
  549. );
  550. static PyObject *
  551. py_scanstring(PyObject* self UNUSED, PyObject *args)
  552. {
  553. PyObject *pystr;
  554. PyObject *rval;
  555. Py_ssize_t end;
  556. Py_ssize_t next_end = -1;
  557. int strict = 1;
  558. if (!PyArg_ParseTuple(args, "On|i:scanstring", &pystr, &end, &strict)) {
  559. return NULL;
  560. }
  561. if (PyUnicode_Check(pystr)) {
  562. rval = scanstring_unicode(pystr, end, strict, &next_end);
  563. }
  564. else {
  565. PyErr_Format(PyExc_TypeError,
  566. "first argument must be a string, not %.80s",
  567. Py_TYPE(pystr)->tp_name);
  568. return NULL;
  569. }
  570. return _build_rval_index_tuple(rval, next_end);
  571. }
  572. PyDoc_STRVAR(pydoc_encode_basestring_ascii,
  573. "encode_basestring_ascii(string) -> string\n"
  574. "\n"
  575. "Return an ASCII-only JSON representation of a Python string"
  576. );
  577. static PyObject *
  578. py_encode_basestring_ascii(PyObject* self UNUSED, PyObject *pystr)
  579. {
  580. PyObject *rval;
  581. /* Return an ASCII-only JSON representation of a Python string */
  582. /* METH_O */
  583. if (PyUnicode_Check(pystr)) {
  584. rval = ascii_escape_unicode(pystr);
  585. }
  586. else {
  587. PyErr_Format(PyExc_TypeError,
  588. "first argument must be a string, not %.80s",
  589. Py_TYPE(pystr)->tp_name);
  590. return NULL;
  591. }
  592. return rval;
  593. }
  594. PyDoc_STRVAR(pydoc_encode_basestring,
  595. "encode_basestring(string) -> string\n"
  596. "\n"
  597. "Return a JSON representation of a Python string"
  598. );
  599. static PyObject *
  600. py_encode_basestring(PyObject* self UNUSED, PyObject *pystr)
  601. {
  602. PyObject *rval;
  603. /* Return a JSON representation of a Python string */
  604. /* METH_O */
  605. if (PyUnicode_Check(pystr)) {
  606. rval = escape_unicode(pystr);
  607. }
  608. else {
  609. PyErr_Format(PyExc_TypeError,
  610. "first argument must be a string, not %.80s",
  611. Py_TYPE(pystr)->tp_name);
  612. return NULL;
  613. }
  614. return rval;
  615. }
  616. static void
  617. scanner_dealloc(PyObject *self)
  618. {
  619. /* Deallocate scanner object */
  620. scanner_clear(self);
  621. Py_TYPE(self)->tp_free(self);
  622. }
  623. static int
  624. scanner_traverse(PyObject *self, visitproc visit, void *arg)
  625. {
  626. PyScannerObject *s;
  627. assert(PyScanner_Check(self));
  628. s = (PyScannerObject *)self;
  629. Py_VISIT(s->strict);
  630. Py_VISIT(s->object_hook);
  631. Py_VISIT(s->object_pairs_hook);
  632. Py_VISIT(s->parse_float);
  633. Py_VISIT(s->parse_int);
  634. Py_VISIT(s->parse_constant);
  635. return 0;
  636. }
  637. static int
  638. scanner_clear(PyObject *self)
  639. {
  640. PyScannerObject *s;
  641. assert(PyScanner_Check(self));
  642. s = (PyScannerObject *)self;
  643. Py_CLEAR(s->strict);
  644. Py_CLEAR(s->object_hook);
  645. Py_CLEAR(s->object_pairs_hook);
  646. Py_CLEAR(s->parse_float);
  647. Py_CLEAR(s->parse_int);
  648. Py_CLEAR(s->parse_constant);
  649. Py_CLEAR(s->memo);
  650. return 0;
  651. }
  652. static PyObject *
  653. _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
  654. /* Read a JSON object from PyUnicode pystr.
  655. idx is the index of the first character after the opening curly brace.
  656. *next_idx_ptr is a return-by-reference index to the first character after
  657. the closing curly brace.
  658. Returns a new PyObject (usually a dict, but object_hook can change that)
  659. */
  660. void *str;
  661. int kind;
  662. Py_ssize_t end_idx;
  663. PyObject *val = NULL;
  664. PyObject *rval = NULL;
  665. PyObject *key = NULL;
  666. int strict = PyObject_IsTrue(s->strict);
  667. int has_pairs_hook = (s->object_pairs_hook != Py_None);
  668. Py_ssize_t next_idx;
  669. if (strict < 0)
  670. return NULL;
  671. if (PyUnicode_READY(pystr) == -1)
  672. return NULL;
  673. str = PyUnicode_DATA(pystr);
  674. kind = PyUnicode_KIND(pystr);
  675. end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
  676. if (has_pairs_hook)
  677. rval = PyList_New(0);
  678. else
  679. rval = PyDict_New();
  680. if (rval == NULL)
  681. return NULL;
  682. /* skip whitespace after { */
  683. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind,str, idx))) idx++;
  684. /* only loop if the object is non-empty */
  685. if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '}') {
  686. while (1) {
  687. PyObject *memokey;
  688. /* read key */
  689. if (idx > end_idx || PyUnicode_READ(kind, str, idx) != '"') {
  690. raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
  691. goto bail;
  692. }
  693. key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
  694. if (key == NULL)
  695. goto bail;
  696. memokey = PyDict_GetItem(s->memo, key);
  697. if (memokey != NULL) {
  698. Py_INCREF(memokey);
  699. Py_DECREF(key);
  700. key = memokey;
  701. }
  702. else {
  703. if (PyDict_SetItem(s->memo, key, key) < 0)
  704. goto bail;
  705. }
  706. idx = next_idx;
  707. /* skip whitespace between key and : delimiter, read :, skip whitespace */
  708. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
  709. if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ':') {
  710. raise_errmsg("Expecting ':' delimiter", pystr, idx);
  711. goto bail;
  712. }
  713. idx++;
  714. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
  715. /* read any JSON term */
  716. val = scan_once_unicode(s, pystr, idx, &next_idx);
  717. if (val == NULL)
  718. goto bail;
  719. if (has_pairs_hook) {
  720. PyObject *item = PyTuple_Pack(2, key, val);
  721. if (item == NULL)
  722. goto bail;
  723. Py_CLEAR(key);
  724. Py_CLEAR(val);
  725. if (PyList_Append(rval, item) == -1) {
  726. Py_DECREF(item);
  727. goto bail;
  728. }
  729. Py_DECREF(item);
  730. }
  731. else {
  732. if (PyDict_SetItem(rval, key, val) < 0)
  733. goto bail;
  734. Py_CLEAR(key);
  735. Py_CLEAR(val);
  736. }
  737. idx = next_idx;
  738. /* skip whitespace before } or , */
  739. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
  740. /* bail if the object is closed or we didn't get the , delimiter */
  741. if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == '}')
  742. break;
  743. if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
  744. raise_errmsg("Expecting ',' delimiter", pystr, idx);
  745. goto bail;
  746. }
  747. idx++;
  748. /* skip whitespace after , delimiter */
  749. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
  750. }
  751. }
  752. *next_idx_ptr = idx + 1;
  753. if (has_pairs_hook) {
  754. val = PyObject_CallFunctionObjArgs(s->object_pairs_hook, rval, NULL);
  755. Py_DECREF(rval);
  756. return val;
  757. }
  758. /* if object_hook is not None: rval = object_hook(rval) */
  759. if (s->object_hook != Py_None) {
  760. val = PyObject_CallFunctionObjArgs(s->object_hook, rval, NULL);
  761. Py_DECREF(rval);
  762. return val;
  763. }
  764. return rval;
  765. bail:
  766. Py_XDECREF(key);
  767. Py_XDECREF(val);
  768. Py_XDECREF(rval);
  769. return NULL;
  770. }
  771. static PyObject *
  772. _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
  773. /* Read a JSON array from PyUnicode pystr.
  774. idx is the index of the first character after the opening brace.
  775. *next_idx_ptr is a return-by-reference index to the first character after
  776. the closing brace.
  777. Returns a new PyList
  778. */
  779. void *str;
  780. int kind;
  781. Py_ssize_t end_idx;
  782. PyObject *val = NULL;
  783. PyObject *rval = PyList_New(0);
  784. Py_ssize_t next_idx;
  785. if (rval == NULL)
  786. return NULL;
  787. if (PyUnicode_READY(pystr) == -1)
  788. return NULL;
  789. str = PyUnicode_DATA(pystr);
  790. kind = PyUnicode_KIND(pystr);
  791. end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
  792. /* skip whitespace after [ */
  793. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
  794. /* only loop if the array is non-empty */
  795. if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
  796. while (1) {
  797. /* read any JSON term */
  798. val = scan_once_unicode(s, pystr, idx, &next_idx);
  799. if (val == NULL)
  800. goto bail;
  801. if (PyList_Append(rval, val) == -1)
  802. goto bail;
  803. Py_CLEAR(val);
  804. idx = next_idx;
  805. /* skip whitespace between term and , */
  806. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
  807. /* bail if the array is closed or we didn't get the , delimiter */
  808. if (idx <= end_idx && PyUnicode_READ(kind, str, idx) == ']')
  809. break;
  810. if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ',') {
  811. raise_errmsg("Expecting ',' delimiter", pystr, idx);
  812. goto bail;
  813. }
  814. idx++;
  815. /* skip whitespace after , */
  816. while (idx <= end_idx && IS_WHITESPACE(PyUnicode_READ(kind, str, idx))) idx++;
  817. }
  818. }
  819. /* verify that idx < end_idx, PyUnicode_READ(kind, str, idx) should be ']' */
  820. if (idx > end_idx || PyUnicode_READ(kind, str, idx) != ']') {
  821. raise_errmsg("Expecting value", pystr, end_idx);
  822. goto bail;
  823. }
  824. *next_idx_ptr = idx + 1;
  825. return rval;
  826. bail:
  827. Py_XDECREF(val);
  828. Py_DECREF(rval);
  829. return NULL;
  830. }
  831. static PyObject *
  832. _parse_constant(PyScannerObject *s, const char *constant, Py_ssize_t idx, Py_ssize_t *next_idx_ptr) {
  833. /* Read a JSON constant.
  834. constant is the constant string that was found
  835. ("NaN", "Infinity", "-Infinity").
  836. idx is the index of the first character of the constant
  837. *next_idx_ptr is a return-by-reference index to the first character after
  838. the constant.
  839. Returns the result of parse_constant
  840. */
  841. PyObject *cstr;
  842. PyObject *rval;
  843. /* constant is "NaN", "Infinity", or "-Infinity" */
  844. cstr = PyUnicode_InternFromString(constant);
  845. if (cstr == NULL)
  846. return NULL;
  847. /* rval = parse_constant(constant) */
  848. rval = PyObject_CallFunctionObjArgs(s->parse_constant, cstr, NULL);
  849. idx += PyUnicode_GET_LENGTH(cstr);
  850. Py_DECREF(cstr);
  851. *next_idx_ptr = idx;
  852. return rval;
  853. }
  854. static PyObject *
  855. _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_ssize_t *next_idx_ptr) {
  856. /* Read a JSON number from PyUnicode pystr.
  857. idx is the index of the first character of the number
  858. *next_idx_ptr is a return-by-reference index to the first character after
  859. the number.
  860. Returns a new PyObject representation of that number:
  861. PyLong, or PyFloat.
  862. May return other types if parse_int or parse_float are set
  863. */
  864. void *str;
  865. int kind;
  866. Py_ssize_t end_idx;
  867. Py_ssize_t idx = start;
  868. int is_float = 0;
  869. PyObject *rval;
  870. PyObject *numstr = NULL;
  871. PyObject *custom_func;
  872. if (PyUnicode_READY(pystr) == -1)
  873. return NULL;
  874. str = PyUnicode_DATA(pystr);
  875. kind = PyUnicode_KIND(pystr);
  876. end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
  877. /* read a sign if it's there, make sure it's not the end of the string */
  878. if (PyUnicode_READ(kind, str, idx) == '-') {
  879. idx++;
  880. if (idx > end_idx) {
  881. raise_stop_iteration(start);
  882. return NULL;
  883. }
  884. }
  885. /* read as many integer digits as we find as long as it doesn't start with 0 */
  886. if (PyUnicode_READ(kind, str, idx) >= '1' && PyUnicode_READ(kind, str, idx) <= '9') {
  887. idx++;
  888. while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
  889. }
  890. /* if it starts with 0 we only expect one integer digit */
  891. else if (PyUnicode_READ(kind, str, idx) == '0') {
  892. idx++;
  893. }
  894. /* no integer digits, error */
  895. else {
  896. raise_stop_iteration(start);
  897. return NULL;
  898. }
  899. /* if the next char is '.' followed by a digit then read all float digits */
  900. if (idx < end_idx && PyUnicode_READ(kind, str, idx) == '.' && PyUnicode_READ(kind, str, idx + 1) >= '0' && PyUnicode_READ(kind, str, idx + 1) <= '9') {
  901. is_float = 1;
  902. idx += 2;
  903. while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
  904. }
  905. /* if the next char is 'e' or 'E' then maybe read the exponent (or backtrack) */
  906. if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == 'e' || PyUnicode_READ(kind, str, idx) == 'E')) {
  907. Py_ssize_t e_start = idx;
  908. idx++;
  909. /* read an exponent sign if present */
  910. if (idx < end_idx && (PyUnicode_READ(kind, str, idx) == '-' || PyUnicode_READ(kind, str, idx) == '+')) idx++;
  911. /* read all digits */
  912. while (idx <= end_idx && PyUnicode_READ(kind, str, idx) >= '0' && PyUnicode_READ(kind, str, idx) <= '9') idx++;
  913. /* if we got a digit, then parse as float. if not, backtrack */
  914. if (PyUnicode_READ(kind, str, idx - 1) >= '0' && PyUnicode_READ(kind, str, idx - 1) <= '9') {
  915. is_float = 1;
  916. }
  917. else {
  918. idx = e_start;
  919. }
  920. }
  921. if (is_float && s->parse_float != (PyObject *)&PyFloat_Type)
  922. custom_func = s->parse_float;
  923. else if (!is_float && s->parse_int != (PyObject *) &PyLong_Type)
  924. custom_func = s->parse_int;
  925. else
  926. custom_func = NULL;
  927. if (custom_func) {
  928. /* copy the section we determined to be a number */
  929. numstr = PyUnicode_FromKindAndData(kind,
  930. (char*)str + kind * start,
  931. idx - start);
  932. if (numstr == NULL)
  933. return NULL;
  934. rval = PyObject_CallFunctionObjArgs(custom_func, numstr, NULL);
  935. }
  936. else {
  937. Py_ssize_t i, n;
  938. char *buf;
  939. /* Straight conversion to ASCII, to avoid costly conversion of
  940. decimal unicode digits (which cannot appear here) */
  941. n = idx - start;
  942. numstr = PyBytes_FromStringAndSize(NULL, n);
  943. if (numstr == NULL)
  944. return NULL;
  945. buf = PyBytes_AS_STRING(numstr);
  946. for (i = 0; i < n; i++) {
  947. buf[i] = (char) PyUnicode_READ(kind, str, i + start);
  948. }
  949. if (is_float)
  950. rval = PyFloat_FromString(numstr);
  951. else
  952. rval = PyLong_FromString(buf, NULL, 10);
  953. }
  954. Py_DECREF(numstr);
  955. *next_idx_ptr = idx;
  956. return rval;
  957. }
  958. static PyObject *
  959. scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_t *next_idx_ptr)
  960. {
  961. /* Read one JSON term (of any kind) from PyUnicode pystr.
  962. idx is the index of the first character of the term
  963. *next_idx_ptr is a return-by-reference index to the first character after
  964. the number.
  965. Returns a new PyObject representation of the term.
  966. */
  967. PyObject *res;
  968. void *str;
  969. int kind;
  970. Py_ssize_t length;
  971. int strict;
  972. if (PyUnicode_READY(pystr) == -1)
  973. return NULL;
  974. str = PyUnicode_DATA(pystr);
  975. kind = PyUnicode_KIND(pystr);
  976. length = PyUnicode_GET_LENGTH(pystr);
  977. if (idx < 0) {
  978. PyErr_SetString(PyExc_ValueError, "idx cannot be negative");
  979. return NULL;
  980. }
  981. if (idx >= length) {
  982. raise_stop_iteration(idx);
  983. return NULL;
  984. }
  985. switch (PyUnicode_READ(kind, str, idx)) {
  986. case '"':
  987. /* string */
  988. strict = PyObject_IsTrue(s->strict);
  989. if (strict < 0)
  990. return NULL;
  991. return scanstring_unicode(pystr, idx + 1, strict, next_idx_ptr);
  992. case '{':
  993. /* object */
  994. if (Py_EnterRecursiveCall(" while decoding a JSON object "
  995. "from a unicode string"))
  996. return NULL;
  997. res = _parse_object_unicode(s, pystr, idx + 1, next_idx_ptr);
  998. Py_LeaveRecursiveCall();
  999. return res;
  1000. case '[':
  1001. /* array */
  1002. if (Py_EnterRecursiveCall(" while decoding a JSON array "
  1003. "from a unicode string"))
  1004. return NULL;
  1005. res = _parse_array_unicode(s, pystr, idx + 1, next_idx_ptr);
  1006. Py_LeaveRecursiveCall();
  1007. return res;
  1008. case 'n':
  1009. /* null */
  1010. if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'u' && PyUnicode_READ(kind, str, idx + 2) == 'l' && PyUnicode_READ(kind, str, idx + 3) == 'l') {
  1011. Py_INCREF(Py_None);
  1012. *next_idx_ptr = idx + 4;
  1013. return Py_None;
  1014. }
  1015. break;
  1016. case 't':
  1017. /* true */
  1018. if ((idx + 3 < length) && PyUnicode_READ(kind, str, idx + 1) == 'r' && PyUnicode_READ(kind, str, idx + 2) == 'u' && PyUnicode_READ(kind, str, idx + 3) == 'e') {
  1019. Py_INCREF(Py_True);
  1020. *next_idx_ptr = idx + 4;
  1021. return Py_True;
  1022. }
  1023. break;
  1024. case 'f':
  1025. /* false */
  1026. if ((idx + 4 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
  1027. PyUnicode_READ(kind, str, idx + 2) == 'l' &&
  1028. PyUnicode_READ(kind, str, idx + 3) == 's' &&
  1029. PyUnicode_READ(kind, str, idx + 4) == 'e') {
  1030. Py_INCREF(Py_False);
  1031. *next_idx_ptr = idx + 5;
  1032. return Py_False;
  1033. }
  1034. break;
  1035. case 'N':
  1036. /* NaN */
  1037. if ((idx + 2 < length) && PyUnicode_READ(kind, str, idx + 1) == 'a' &&
  1038. PyUnicode_READ(kind, str, idx + 2) == 'N') {
  1039. return _parse_constant(s, "NaN", idx, next_idx_ptr);
  1040. }
  1041. break;
  1042. case 'I':
  1043. /* Infinity */
  1044. if ((idx + 7 < length) && PyUnicode_READ(kind, str, idx + 1) == 'n' &&
  1045. PyUnicode_READ(kind, str, idx + 2) == 'f' &&
  1046. PyUnicode_READ(kind, str, idx + 3) == 'i' &&
  1047. PyUnicode_READ(kind, str, idx + 4) == 'n' &&
  1048. PyUnicode_READ(kind, str, idx + 5) == 'i' &&
  1049. PyUnicode_READ(kind, str, idx + 6) == 't' &&
  1050. PyUnicode_READ(kind, str, idx + 7) == 'y') {
  1051. return _parse_constant(s, "Infinity", idx, next_idx_ptr);
  1052. }
  1053. break;
  1054. case '-':
  1055. /* -Infinity */
  1056. if ((idx + 8 < length) && PyUnicode_READ(kind, str, idx + 1) == 'I' &&
  1057. PyUnicode_READ(kind, str, idx + 2) == 'n' &&
  1058. PyUnicode_READ(kind, str, idx + 3) == 'f' &&
  1059. PyUnicode_READ(kind, str, idx + 4) == 'i' &&
  1060. PyUnicode_READ(kind, str, idx + 5) == 'n' &&
  1061. PyUnicode_READ(kind, str, idx + 6) == 'i' &&
  1062. PyUnicode_READ(kind, str, idx + 7) == 't' &&
  1063. PyUnicode_READ(kind, str, idx + 8) == 'y') {
  1064. return _parse_constant(s, "-Infinity", idx, next_idx_ptr);
  1065. }
  1066. break;
  1067. }
  1068. /* Didn't find a string, object, array, or named constant. Look for a number. */
  1069. return _match_number_unicode(s, pystr, idx, next_idx_ptr);
  1070. }
  1071. static PyObject *
  1072. scanner_call(PyObject *self, PyObject *args, PyObject *kwds)
  1073. {
  1074. /* Python callable interface to scan_once_{str,unicode} */
  1075. PyObject *pystr;
  1076. PyObject *rval;
  1077. Py_ssize_t idx;
  1078. Py_ssize_t next_idx = -1;
  1079. static char *kwlist[] = {"string", "idx", NULL};
  1080. PyScannerObject *s;
  1081. assert(PyScanner_Check(self));
  1082. s = (PyScannerObject *)self;
  1083. if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:scan_once", kwlist, &pystr, &idx))
  1084. return NULL;
  1085. if (PyUnicode_Check(pystr)) {
  1086. rval = scan_once_unicode(s, pystr, idx, &next_idx);
  1087. }
  1088. else {
  1089. PyErr_Format(PyExc_TypeError,
  1090. "first argument must be a string, not %.80s",
  1091. Py_TYPE(pystr)->tp_name);
  1092. return NULL;
  1093. }
  1094. PyDict_Clear(s->memo);
  1095. if (rval == NULL)
  1096. return NULL;
  1097. return _build_rval_index_tuple(rval, next_idx);
  1098. }
  1099. static PyObject *
  1100. scanner_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  1101. {
  1102. PyScannerObject *s;
  1103. s = (PyScannerObject *)type->tp_alloc(type, 0);
  1104. if (s != NULL) {
  1105. s->strict = NULL;
  1106. s->object_hook = NULL;
  1107. s->object_pairs_hook = NULL;
  1108. s->parse_float = NULL;
  1109. s->parse_int = NULL;
  1110. s->parse_constant = NULL;
  1111. }
  1112. return (PyObject *)s;
  1113. }
  1114. static int
  1115. scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
  1116. {
  1117. /* Initialize Scanner object */
  1118. PyObject *ctx;
  1119. static char *kwlist[] = {"context", NULL};
  1120. PyScannerObject *s;
  1121. assert(PyScanner_Check(self));
  1122. s = (PyScannerObject *)self;
  1123. if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:make_scanner", kwlist, &ctx))
  1124. return -1;
  1125. if (s->memo == NULL) {
  1126. s->memo = PyDict_New();
  1127. if (s->memo == NULL)
  1128. goto bail;
  1129. }
  1130. /* All of these will fail "gracefully" so we don't need to verify them */
  1131. s->strict = PyObject_GetAttrString(ctx, "strict");
  1132. if (s->strict == NULL)
  1133. goto bail;
  1134. s->object_hook = PyObject_GetAttrString(ctx, "object_hook");
  1135. if (s->object_hook == NULL)
  1136. goto bail;
  1137. s->object_pairs_hook = PyObject_GetAttrString(ctx, "object_pairs_hook");
  1138. if (s->object_pairs_hook == NULL)
  1139. goto bail;
  1140. s->parse_float = PyObject_GetAttrString(ctx, "parse_float");
  1141. if (s->parse_float == NULL)
  1142. goto bail;
  1143. s->parse_int = PyObject_GetAttrString(ctx, "parse_int");
  1144. if (s->parse_int == NULL)
  1145. goto bail;
  1146. s->parse_constant = PyObject_GetAttrString(ctx, "parse_constant");
  1147. if (s->parse_constant == NULL)
  1148. goto bail;
  1149. return 0;
  1150. bail:
  1151. Py_CLEAR(s->strict);
  1152. Py_CLEAR(s->object_hook);
  1153. Py_CLEAR(s->object_pairs_hook);
  1154. Py_CLEAR(s->parse_float);
  1155. Py_CLEAR(s->parse_int);
  1156. Py_CLEAR(s->parse_constant);
  1157. return -1;
  1158. }
  1159. PyDoc_STRVAR(scanner_doc, "JSON scanner object");
  1160. static
  1161. PyTypeObject PyScannerType = {
  1162. PyVarObject_HEAD_INIT(NULL, 0)
  1163. "_json.Scanner", /* tp_name */
  1164. sizeof(PyScannerObject), /* tp_basicsize */
  1165. 0, /* tp_itemsize */
  1166. scanner_dealloc, /* tp_dealloc */
  1167. 0, /* tp_print */
  1168. 0, /* tp_getattr */
  1169. 0, /* tp_setattr */
  1170. 0, /* tp_compare */
  1171. 0, /* tp_repr */
  1172. 0, /* tp_as_number */
  1173. 0, /* tp_as_sequence */
  1174. 0, /* tp_as_mapping */
  1175. 0, /* tp_hash */
  1176. scanner_call, /* tp_call */
  1177. 0, /* tp_str */
  1178. 0,/* PyObject_GenericGetAttr, */ /* tp_getattro */
  1179. 0,/* PyObject_GenericSetAttr, */ /* tp_setattro */
  1180. 0, /* tp_as_buffer */
  1181. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  1182. scanner_doc, /* tp_doc */
  1183. scanner_traverse, /* tp_traverse */
  1184. scanner_clear, /* tp_clear */
  1185. 0, /* tp_richcompare */
  1186. 0, /* tp_weaklistoffset */
  1187. 0, /* tp_iter */
  1188. 0, /* tp_iternext */
  1189. 0, /* tp_methods */
  1190. scanner_members, /* tp_members */
  1191. 0, /* tp_getset */
  1192. 0, /* tp_base */
  1193. 0, /* tp_dict */
  1194. 0, /* tp_descr_get */
  1195. 0, /* tp_descr_set */
  1196. 0, /* tp_dictoffset */
  1197. scanner_init, /* tp_init */
  1198. 0,/* PyType_GenericAlloc, */ /* tp_alloc */
  1199. scanner_new, /* tp_new */
  1200. 0,/* PyObject_GC_Del, */ /* tp_free */
  1201. };
  1202. static PyObject *
  1203. encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  1204. {
  1205. PyEncoderObject *s;
  1206. s = (PyEncoderObject *)type->tp_alloc(type, 0);
  1207. if (s != NULL) {
  1208. s->markers = NULL;
  1209. s->defaultfn = NULL;
  1210. s->encoder = NULL;
  1211. s->indent = NULL;
  1212. s->key_separator = NULL;
  1213. s->item_separator = NULL;
  1214. s->sort_keys = NULL;
  1215. s->skipkeys = NULL;
  1216. }
  1217. return (PyObject *)s;
  1218. }
  1219. static int
  1220. encoder_init(PyObject *self, PyObject *args, PyObject *kwds)
  1221. {
  1222. /* initialize Encoder object */
  1223. static char *kwlist[] = {"markers", "default", "encoder", "indent", "key_separator", "item_separator", "sort_keys", "skipkeys", "allow_nan", NULL};
  1224. PyEncoderObject *s;
  1225. PyObject *markers, *defaultfn, *encoder, *indent, *key_separator;
  1226. PyObject *item_separator, *sort_keys, *skipkeys;
  1227. int allow_nan;
  1228. assert(PyEncoder_Check(self));
  1229. s = (PyEncoderObject *)self;
  1230. if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOOUUOOp:make_encoder", kwlist,
  1231. &markers, &defaultfn, &encoder, &indent,
  1232. &key_separator, &item_separator,
  1233. &sort_keys, &skipkeys, &allow_nan))
  1234. return -1;
  1235. if (markers != Py_None && !PyDict_Check(markers)) {
  1236. PyErr_Format(PyExc_TypeError,
  1237. "make_encoder() argument 1 must be dict or None, "
  1238. "not %.200s", Py_TYPE(markers)->tp_name);
  1239. return -1;
  1240. }
  1241. s->markers = markers;
  1242. s->defaultfn = defaultfn;
  1243. s->encoder = encoder;
  1244. s->indent = indent;
  1245. s->key_separator = key_separator;
  1246. s->item_separator = item_separator;
  1247. s->sort_keys = sort_keys;
  1248. s->skipkeys = skipkeys;
  1249. s->fast_encode = NULL;
  1250. if (PyCFunction_Check(s->encoder)) {
  1251. PyCFunction f = PyCFunction_GetFunction(s->encoder);
  1252. if (f == (PyCFunction)py_encode_basestring_ascii ||
  1253. f == (PyCFunction)py_encode_basestring) {
  1254. s->fast_encode = f;
  1255. }
  1256. }
  1257. s->allow_nan = allow_nan;
  1258. Py_INCREF(s->markers);
  1259. Py_INCREF(s->defaultfn);
  1260. Py_INCREF(s->encoder);
  1261. Py_INCREF(s->indent);
  1262. Py_INCREF(s->key_separator);
  1263. Py_INCREF(s->item_separator);
  1264. Py_INCREF(s->sort_keys);
  1265. Py_INCREF(s->skipkeys);
  1266. return 0;
  1267. }
  1268. static PyObject *
  1269. encoder_call(PyObject *self, PyObject *args, PyObject *kwds)
  1270. {
  1271. /* Python callable interface to encode_listencode_obj */
  1272. static char *kwlist[] = {"obj", "_current_indent_level", NULL};
  1273. PyObject *obj;
  1274. Py_ssize_t indent_level;
  1275. PyEncoderObject *s;
  1276. _PyAccu acc;
  1277. assert(PyEncoder_Check(self));
  1278. s = (PyEncoderObject *)self;
  1279. if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:_iterencode", kwlist,
  1280. &obj, &indent_level))
  1281. return NULL;
  1282. if (_PyAccu_Init(&acc))
  1283. return NULL;
  1284. if (encoder_listencode_obj(s, &acc, obj, indent_level)) {
  1285. _PyAccu_Destroy(&acc);
  1286. return NULL;
  1287. }
  1288. return _PyAccu_FinishAsList(&acc);
  1289. }
  1290. static PyObject *
  1291. _encoded_const(PyObject *obj)
  1292. {
  1293. /* Return the JSON string representation of None, True, False */
  1294. if (obj == Py_None) {
  1295. static PyObject *s_null = NULL;
  1296. if (s_null == NULL) {
  1297. s_null = PyUnicode_InternFromString("null");
  1298. }
  1299. Py_INCREF(s_null);
  1300. return s_null;
  1301. }
  1302. else if (obj == Py_True) {
  1303. static PyObject *s_true = NULL;
  1304. if (s_true == NULL) {
  1305. s_true = PyUnicode_InternFromString("true");
  1306. }
  1307. Py_INCREF(s_true);
  1308. return s_true;
  1309. }
  1310. else if (obj == Py_False) {
  1311. static PyObject *s_false = NULL;
  1312. if (s_false == NULL) {
  1313. s_false = PyUnicode_InternFromString("false");
  1314. }
  1315. Py_INCREF(s_false);
  1316. return s_false;
  1317. }
  1318. else {
  1319. PyErr_SetString(PyExc_ValueError, "not a const");
  1320. return NULL;
  1321. }
  1322. }
  1323. static PyObject *
  1324. encoder_encode_long(PyEncoderObject* s UNUSED, PyObject *obj)
  1325. {
  1326. /* Return the JSON representation of a PyLong and PyLong subclasses.
  1327. Calls int() on PyLong subclasses in case the str() was changed.
  1328. Added specifically to deal with IntEnum. See Issue18264. */
  1329. PyObject *encoded, *longobj;
  1330. if (PyLong_CheckExact(obj)) {
  1331. encoded = PyObject_Str(obj);
  1332. }
  1333. else {
  1334. longobj = PyNumber_Long(obj);
  1335. if (longobj == NULL) {
  1336. PyErr_SetString(
  1337. PyExc_ValueError,
  1338. "Unable to coerce int subclass to int"
  1339. );
  1340. return NULL;
  1341. }
  1342. encoded = PyObject_Str(longobj);
  1343. Py_DECREF(longobj);
  1344. }
  1345. return encoded;
  1346. }
  1347. static PyObject *
  1348. encoder_encode_float(PyEncoderObject *s, PyObject *obj)
  1349. {
  1350. /* Return the JSON representation of a PyFloat.
  1351. Modified to call float() on float subclasses in case the subclass
  1352. changes the repr. See Issue18264. */
  1353. PyObject *encoded, *floatobj;
  1354. double i = PyFloat_AS_DOUBLE(obj);
  1355. if (!Py_IS_FINITE(i)) {
  1356. if (!s->allow_nan) {
  1357. PyErr_SetString(
  1358. PyExc_ValueError,
  1359. "Out of range float values are not JSON compliant"
  1360. );
  1361. return NULL;
  1362. }
  1363. if (i > 0) {
  1364. return PyUnicode_FromString("Infinity");
  1365. }
  1366. else if (i < 0) {
  1367. return PyUnicode_FromString("-Infinity");
  1368. }
  1369. else {
  1370. return PyUnicode_FromString("NaN");
  1371. }
  1372. }
  1373. /* coerce float subclasses to float (primarily for Enum) */
  1374. if (PyFloat_CheckExact(obj)) {
  1375. /* Use a better float format here? */
  1376. encoded = PyObject_Repr(obj);
  1377. }
  1378. else {
  1379. floatobj = PyNumber_Float(obj);
  1380. if (floatobj == NULL) {
  1381. PyErr_SetString(
  1382. PyExc_ValueError,
  1383. "Unable to coerce float subclass to float"
  1384. );
  1385. return NULL;
  1386. }
  1387. encoded = PyObject_Repr(floatobj);
  1388. Py_DECREF(floatobj);
  1389. }
  1390. return encoded;
  1391. }
  1392. static PyObject *
  1393. encoder_encode_string(PyEncoderObject *s, PyObject *obj)
  1394. {
  1395. /* Return the JSON representation of a string */
  1396. if (s->fast_encode)
  1397. return s->fast_encode(NULL, obj);
  1398. else
  1399. return PyObject_CallFunctionObjArgs(s->encoder, obj, NULL);
  1400. }
  1401. static int
  1402. _steal_accumulate(_PyAccu *acc, PyObject *stolen)
  1403. {
  1404. /* Append stolen and then decrement its reference count */
  1405. int rval = _PyAccu_Accumulate(acc, stolen);
  1406. Py_DECREF(stolen);
  1407. return rval;
  1408. }
  1409. static int
  1410. encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
  1411. PyObject *obj, Py_ssize_t indent_level)
  1412. {
  1413. /* Encode Python object obj to a JSON term */
  1414. PyObject *newobj;
  1415. int rv;
  1416. if (obj == Py_None || obj == Py_True || obj == Py_False) {
  1417. PyObject *cstr = _encoded_const(obj);
  1418. if (cstr == NULL)
  1419. return -1;
  1420. return _steal_accumulate(acc, cstr);
  1421. }
  1422. else if (PyUnicode_Check(obj))
  1423. {
  1424. PyObject *encoded = encoder_encode_string(s, obj);
  1425. if (encoded == NULL)
  1426. return -1;
  1427. return _steal_accumulate(acc, encoded);
  1428. }
  1429. else if (PyLong_Check(obj)) {
  1430. PyObject *encoded = encoder_encode_long(s, obj);
  1431. if (encoded == NULL)
  1432. return -1;
  1433. return _steal_accumulate(acc, encoded);
  1434. }
  1435. else if (PyFloat_Check(obj)) {
  1436. PyObject *encoded = encoder_encode_float(s, obj);
  1437. if (encoded == NULL)
  1438. return -1;
  1439. return _steal_accumulate(acc, encoded);
  1440. }
  1441. else if (PyList_Check(obj) || PyTuple_Check(obj)) {
  1442. if (Py_EnterRecursiveCall(" while encoding a JSON object"))
  1443. return -1;
  1444. rv = encoder_listencode_list(s, acc, obj, indent_level);
  1445. Py_LeaveRecursiveCall();
  1446. return rv;
  1447. }
  1448. else if (PyDict_Check(obj)) {
  1449. if (Py_EnterRecursiveCall(" while encoding a JSON object"))
  1450. return -1;
  1451. rv = encoder_listencode_dict(s, acc, obj, indent_level);
  1452. Py_LeaveRecursiveCall();
  1453. return rv;
  1454. }
  1455. else {
  1456. PyObject *ident = NULL;
  1457. if (s->markers != Py_None) {
  1458. int has_key;
  1459. ident = PyLong_FromVoidPtr(obj);
  1460. if (ident == NULL)
  1461. return -1;
  1462. has_key = PyDict_Contains(s->markers, ident);
  1463. if (has_key) {
  1464. if (has_key != -1)
  1465. PyErr_SetString(PyExc_ValueError, "Circular reference detected");
  1466. Py_DECREF(ident);
  1467. return -1;
  1468. }
  1469. if (PyDict_SetItem(s->markers, ident, obj)) {
  1470. Py_DECREF(ident);
  1471. return -1;
  1472. }
  1473. }
  1474. newobj = PyObject_CallFunctionObjArgs(s->defaultfn, obj, NULL);
  1475. if (newobj == NULL) {
  1476. Py_XDECREF(ident);
  1477. return -1;
  1478. }
  1479. if (Py_EnterRecursiveCall(" while encoding a JSON object"))
  1480. return -1;
  1481. rv = encoder_listencode_obj(s, acc, newobj, indent_level);
  1482. Py_LeaveRecursiveCall();
  1483. Py_DECREF(newobj);
  1484. if (rv) {
  1485. Py_XDECREF(ident);
  1486. return -1;
  1487. }
  1488. if (ident != NULL) {
  1489. if (PyDict_DelItem(s->markers, ident)) {
  1490. Py_XDECREF(ident);
  1491. return -1;
  1492. }
  1493. Py_XDECREF(ident);
  1494. }
  1495. return rv;
  1496. }
  1497. }
  1498. static int
  1499. encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
  1500. PyObject *dct, Py_ssize_t indent_level)
  1501. {
  1502. /* Encode Python dict dct a JSON term */
  1503. static PyObject *open_dict = NULL;
  1504. static PyObject *close_dict = NULL;
  1505. static PyObject *empty_dict = NULL;
  1506. PyObject *kstr = NULL;
  1507. PyObject *ident = NULL;
  1508. PyObject *it = NULL;
  1509. PyObject *items;
  1510. PyObject *item = NULL;
  1511. int skipkeys;
  1512. int sortkeys;
  1513. Py_ssize_t idx;
  1514. if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
  1515. open_dict = PyUnicode_InternFromString("{");
  1516. close_dict = PyUnicode_InternFromString("}");
  1517. empty_dict = PyUnicode_InternFromString("{}");
  1518. if (open_dict == NULL || close_dict == NULL || empty_dict == NULL)
  1519. return -1;
  1520. }
  1521. if (Py_SIZE(dct) == 0)
  1522. return _PyAccu_Accumulate(acc, empty_dict);
  1523. if (s->markers != Py_None) {
  1524. int has_key;
  1525. ident = PyLong_FromVoidPtr(dct);
  1526. if (ident == NULL)
  1527. goto bail;
  1528. has_key = PyDict_Contains(s->markers, ident);
  1529. if (has_key) {
  1530. if (has_key != -1)
  1531. PyErr_SetString(PyExc_ValueError, "Circular reference detected");
  1532. goto bail;
  1533. }
  1534. if (PyDict_SetItem(s->markers, ident, dct)) {
  1535. goto bail;
  1536. }
  1537. }
  1538. if (_PyAccu_Accumulate(acc, open_dict))
  1539. goto bail;
  1540. if (s->indent != Py_None) {
  1541. /* TODO: DOES NOT RUN */
  1542. indent_level += 1;
  1543. /*
  1544. newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
  1545. separator = _item_separator + newline_indent
  1546. buf += newline_indent
  1547. */
  1548. }
  1549. items = PyMapping_Items(dct);
  1550. if (items == NULL)
  1551. goto bail;
  1552. sortkeys = PyObject_IsTrue(s->sort_keys);
  1553. if (sortkeys < 0 || (sortkeys && PyList_Sort(items) < 0))
  1554. goto bail;
  1555. it = PyObject_GetIter(items);
  1556. Py_DECREF(items);
  1557. if (it == NULL)
  1558. goto bail;
  1559. skipkeys = PyObject_IsTrue(s->skipkeys);
  1560. if (skipkeys < 0)
  1561. goto bail;
  1562. idx = 0;
  1563. while ((item = PyIter_Next(it)) != NULL) {
  1564. PyObject *encoded, *key, *value;
  1565. if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
  1566. PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
  1567. goto bail;
  1568. }
  1569. key = PyTuple_GET_ITEM(item, 0);
  1570. if (PyUnicode_Check(key)) {
  1571. Py_INCREF(key);
  1572. kstr = key;
  1573. }
  1574. else if (PyFloat_Check(key)) {
  1575. kstr = encoder_encode_float(s, key);
  1576. if (kstr == NULL)
  1577. goto bail;
  1578. }
  1579. else if (key == Py_True || key == Py_False || key == Py_None) {
  1580. /* This must come before the PyLong_Check because
  1581. True and False are also 1 and 0.*/
  1582. kstr = _encoded_const(key);
  1583. if (kstr == NULL)
  1584. goto bail;
  1585. }
  1586. else if (PyLong_Check(key)) {
  1587. kstr = encoder_encode_long(s, key);
  1588. if (kstr == NULL) {
  1589. goto bail;
  1590. }
  1591. }
  1592. else if (skipkeys) {
  1593. Py_DECREF(item);
  1594. continue;
  1595. }
  1596. else {
  1597. /* TODO: include repr of key */
  1598. PyErr_SetString(PyExc_TypeError, "keys must be a string");
  1599. goto bail;
  1600. }
  1601. if (idx) {
  1602. if (_PyAccu_Accumulate(acc, s->item_separator))
  1603. goto bail;
  1604. }
  1605. encoded = encoder_encode_string(s, kstr);
  1606. Py_CLEAR(kstr);
  1607. if (encoded == NULL)
  1608. goto bail;
  1609. if (_PyAccu_Accumulate(acc, encoded)) {
  1610. Py_DECREF(encoded);
  1611. goto bail;
  1612. }
  1613. Py_DECREF(encoded);
  1614. if (_PyAccu_Accumulate(acc, s->key_separator))
  1615. goto bail;
  1616. value = PyTuple_GET_ITEM(item, 1);
  1617. if (encoder_listencode_obj(s, acc, value, indent_level))
  1618. goto bail;
  1619. idx += 1;
  1620. Py_DECREF(item);
  1621. }
  1622. if (PyErr_Occurred())
  1623. goto bail;
  1624. Py_CLEAR(it);
  1625. if (ident != NULL) {
  1626. if (PyDict_DelItem(s->markers, ident))
  1627. goto bail;
  1628. Py_CLEAR(ident);
  1629. }
  1630. /* TODO DOES NOT RUN; dead code
  1631. if (s->indent != Py_None) {
  1632. indent_level -= 1;
  1633. yield '\n' + (' ' * (_indent * _current_indent_level))
  1634. }*/
  1635. if (_PyAccu_Accumulate(acc, close_dict))
  1636. goto bail;
  1637. return 0;
  1638. bail:
  1639. Py_XDECREF(it);
  1640. Py_XDECREF(item);
  1641. Py_XDECREF(kstr);
  1642. Py_XDECREF(ident);
  1643. return -1;
  1644. }
  1645. static int
  1646. encoder_listencode_list(PyEncoderObject *s, _PyAccu *acc,
  1647. PyObject *seq, Py_ssize_t indent_level)
  1648. {
  1649. /* Encode Python list seq to a JSON term */
  1650. static PyObject *open_array = NULL;
  1651. static PyObject *close_array = NULL;
  1652. static PyObject *empty_array = NULL;
  1653. PyObject *ident = NULL;
  1654. PyObject *s_fast = NULL;
  1655. Py_ssize_t i;
  1656. if (open_array == NULL || close_array == NULL || empty_array == NULL) {
  1657. open_array = PyUnicode_InternFromString("[");
  1658. close_array = PyUnicode_InternFromString("]");
  1659. empty_array = PyUnicode_InternFromString("[]");
  1660. if (open_array == NULL || close_array == NULL || empty_array == NULL)
  1661. return -1;
  1662. }
  1663. ident = NULL;
  1664. s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
  1665. if (s_fast == NULL)
  1666. return -1;
  1667. if (PySequence_Fast_GET_SIZE(s_fast) == 0) {
  1668. Py_DECREF(s_fast);
  1669. return _PyAccu_Accumulate(acc, empty_array);
  1670. }
  1671. if (s->markers != Py_None) {
  1672. int has_key;
  1673. ident = PyLong_FromVoidPtr(seq);
  1674. if (ident == NULL)
  1675. goto bail;
  1676. has_key = PyDict_Contains(s->markers, ident);
  1677. if (has_key) {
  1678. if (has_key != -1)
  1679. PyErr_SetString(PyExc_ValueError, "Circular reference detected");
  1680. goto bail;
  1681. }
  1682. if (PyDict_SetItem(s->markers, ident, seq)) {
  1683. goto bail;
  1684. }
  1685. }
  1686. if (_PyAccu_Accumulate(acc, open_array))
  1687. goto bail;
  1688. if (s->indent != Py_None) {
  1689. /* TODO: DOES NOT RUN */
  1690. indent_level += 1;
  1691. /*
  1692. newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
  1693. separator = _item_separator + newline_indent
  1694. buf += newline_indent
  1695. */
  1696. }
  1697. for (i = 0; i < PySequence_Fast_GET_SIZE(s_fast); i++) {
  1698. PyObject *obj = PySequence_Fast_GET_ITEM(s_fast, i);
  1699. if (i) {
  1700. if (_PyAccu_Accumulate(acc, s->item_separator))
  1701. goto bail;
  1702. }
  1703. if (encoder_listencode_obj(s, acc, obj, indent_level))
  1704. goto bail;
  1705. }
  1706. if (ident != NULL) {
  1707. if (PyDict_DelItem(s->markers, ident))
  1708. goto bail;
  1709. Py_CLEAR(ident);
  1710. }
  1711. /* TODO: DOES NOT RUN
  1712. if (s->indent != Py_None) {
  1713. indent_level -= 1;
  1714. yield '\n' + (' ' * (_indent * _current_indent_level))
  1715. }*/
  1716. if (_PyAccu_Accumulate(acc, close_array))
  1717. goto bail;
  1718. Py_DECREF(s_fast);
  1719. return 0;
  1720. bail:
  1721. Py_XDECREF(ident);
  1722. Py_DECREF(s_fast);
  1723. return -1;
  1724. }
  1725. static void
  1726. encoder_dealloc(PyObject *self)
  1727. {
  1728. /* Deallocate Encoder */
  1729. encoder_clear(self);
  1730. Py_TYPE(self)->tp_free(self);
  1731. }
  1732. static int
  1733. encoder_traverse(PyObject *self, visitproc visit, void *arg)
  1734. {
  1735. PyEncoderObject *s;
  1736. assert(PyEncoder_Check(self));
  1737. s = (PyEncoderObject *)self;
  1738. Py_VISIT(s->markers);
  1739. Py_VISIT(s->defaultfn);
  1740. Py_VISIT(s->encoder);
  1741. Py_VISIT(s->indent);
  1742. Py_VISIT(s->key_separator);
  1743. Py_VISIT(s->item_separator);
  1744. Py_VISIT(s->sort_keys);
  1745. Py_VISIT(s->skipkeys);
  1746. return 0;
  1747. }
  1748. static int
  1749. encoder_clear(PyObject *self)
  1750. {
  1751. /* Deallocate Encoder */
  1752. PyEncoderObject *s;
  1753. assert(PyEncoder_Check(self));
  1754. s = (PyEncoderObject *)self;
  1755. Py_CLEAR(s->markers);
  1756. Py_CLEAR(s->defaultfn);
  1757. Py_CLEAR(s->encoder);
  1758. Py_CLEAR(s->indent);
  1759. Py_CLEAR(s->key_separator);
  1760. Py_CLEAR(s->item_separator);
  1761. Py_CLEAR(s->sort_keys);
  1762. Py_CLEAR(s->skipkeys);
  1763. return 0;
  1764. }
  1765. PyDoc_STRVAR(encoder_doc, "_iterencode(obj, _current_indent_level) -> iterable");
  1766. static
  1767. PyTypeObject PyEncoderType = {
  1768. PyVarObject_HEAD_INIT(NULL, 0)
  1769. "_json.Encoder", /* tp_name */
  1770. sizeof(PyEncoderObject), /* tp_basicsize */
  1771. 0, /* tp_itemsize */
  1772. encoder_dealloc, /* tp_dealloc */
  1773. 0, /* tp_print */
  1774. 0, /* tp_getattr */
  1775. 0, /* tp_setattr */
  1776. 0, /* tp_compare */
  1777. 0, /* tp_repr */
  1778. 0, /* tp_as_number */
  1779. 0, /* tp_as_sequence */
  1780. 0, /* tp_as_mapping */
  1781. 0, /* tp_hash */
  1782. encoder_call, /* tp_call */
  1783. 0, /* tp_str */
  1784. 0, /* tp_getattro */
  1785. 0, /* tp_setattro */
  1786. 0, /* tp_as_buffer */
  1787. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  1788. encoder_doc, /* tp_doc */
  1789. encoder_traverse, /* tp_traverse */
  1790. encoder_clear, /* tp_clear */
  1791. 0, /* tp_richcompare */
  1792. 0, /* tp_weaklistoffset */
  1793. 0, /* tp_iter */
  1794. 0, /* tp_iternext */
  1795. 0, /* tp_methods */
  1796. encoder_members, /* tp_members */
  1797. 0, /* tp_getset */
  1798. 0, /* tp_base */
  1799. 0, /* tp_dict */
  1800. 0, /* tp_descr_get */
  1801. 0, /* tp_descr_set */
  1802. 0, /* tp_dictoffset */
  1803. encoder_init, /* tp_init */
  1804. 0, /* tp_alloc */
  1805. encoder_new, /* tp_new */
  1806. 0, /* tp_free */
  1807. };
  1808. static PyMethodDef speedups_methods[] = {
  1809. {"encode_basestring_ascii",
  1810. (PyCFunction)py_encode_basestring_ascii,
  1811. METH_O,
  1812. pydoc_encode_basestring_ascii},
  1813. {"encode_basestring",
  1814. (PyCFunction)py_encode_basestring,
  1815. METH_O,
  1816. pydoc_encode_basestring},
  1817. {"scanstring",
  1818. (PyCFunction)py_scanstring,
  1819. METH_VARARGS,
  1820. pydoc_scanstring},
  1821. {NULL, NULL, 0, NULL}
  1822. };
  1823. PyDoc_STRVAR(module_doc,
  1824. "json speedups\n");
  1825. static struct PyModuleDef jsonmodule = {
  1826. PyModuleDef_HEAD_INIT,
  1827. "_json",
  1828. module_doc,
  1829. -1,
  1830. speedups_methods,
  1831. NULL,
  1832. NULL,
  1833. NULL,
  1834. NULL
  1835. };
  1836. PyMODINIT_FUNC
  1837. PyInit__json(void)
  1838. {
  1839. PyObject *m = PyModule_Create(&jsonmodule);
  1840. if (!m)
  1841. return NULL;
  1842. PyScannerType.tp_new = PyType_GenericNew;
  1843. if (PyType_Ready(&PyScannerType) < 0)
  1844. goto fail;
  1845. PyEncoderType.tp_new = PyType_GenericNew;
  1846. if (PyType_Ready(&PyEncoderType) < 0)
  1847. goto fail;
  1848. Py_INCREF((PyObject*)&PyScannerType);
  1849. if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
  1850. Py_DECREF((PyObject*)&PyScannerType);
  1851. goto fail;
  1852. }
  1853. Py_INCREF((PyObject*)&PyEncoderType);
  1854. if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
  1855. Py_DECREF((PyObject*)&PyEncoderType);
  1856. goto fail;
  1857. }
  1858. return m;
  1859. fail:
  1860. Py_DECREF(m);
  1861. return NULL;
  1862. }