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.

872 lines
24 KiB

Marc-Andre's third try at this bulk patch seems to work (except that his copy of test_contains.py seems to be broken -- the lines he deleted were already absent). Checkin messages: New Unicode support for int(), float(), complex() and long(). - new APIs PyInt_FromUnicode() and PyLong_FromUnicode() - added support for Unicode to PyFloat_FromString() - new encoding API PyUnicode_EncodeDecimal() which converts Unicode to a decimal char* string (used in the above new APIs) - shortcuts for calls like int(<int object>) and float(<float obj>) - tests for all of the above Unicode compares and contains checks: - comparing Unicode and non-string types now works; TypeErrors are masked, all other errors such as ValueError during Unicode coercion are passed through (note that PyUnicode_Compare does not implement the masking -- PyObject_Compare does this) - contains now works for non-string types too; TypeErrors are masked and 0 returned; all other errors are passed through Better testing support for the standard codecs. Misc minor enhancements, such as an alias dbcs for the mbcs codec. Changes: - PyLong_FromString() now applies the same error checks as does PyInt_FromString(): trailing garbage is reported as error and not longer silently ignored. The only characters which may be trailing the digits are 'L' and 'l' -- these are still silently ignored. - string.ato?() now directly interface to int(), long() and float(). The error strings are now a little different, but the type still remains the same. These functions are now ready to get declared obsolete ;-) - PyNumber_Int() now also does a check for embedded NULL chars in the input string; PyNumber_Long() already did this (and still does) Followed by: Looks like I've gone a step too far there... (and test_contains.py seem to have a bug too). I've changed back to reporting all errors in PyUnicode_Contains() and added a few more test cases to test_contains.py (plus corrected the join() NameError).
26 years ago
Marc-Andre's third try at this bulk patch seems to work (except that his copy of test_contains.py seems to be broken -- the lines he deleted were already absent). Checkin messages: New Unicode support for int(), float(), complex() and long(). - new APIs PyInt_FromUnicode() and PyLong_FromUnicode() - added support for Unicode to PyFloat_FromString() - new encoding API PyUnicode_EncodeDecimal() which converts Unicode to a decimal char* string (used in the above new APIs) - shortcuts for calls like int(<int object>) and float(<float obj>) - tests for all of the above Unicode compares and contains checks: - comparing Unicode and non-string types now works; TypeErrors are masked, all other errors such as ValueError during Unicode coercion are passed through (note that PyUnicode_Compare does not implement the masking -- PyObject_Compare does this) - contains now works for non-string types too; TypeErrors are masked and 0 returned; all other errors are passed through Better testing support for the standard codecs. Misc minor enhancements, such as an alias dbcs for the mbcs codec. Changes: - PyLong_FromString() now applies the same error checks as does PyInt_FromString(): trailing garbage is reported as error and not longer silently ignored. The only characters which may be trailing the digits are 'L' and 'l' -- these are still silently ignored. - string.ato?() now directly interface to int(), long() and float(). The error strings are now a little different, but the type still remains the same. These functions are now ready to get declared obsolete ;-) - PyNumber_Int() now also does a check for embedded NULL chars in the input string; PyNumber_Long() already did this (and still does) Followed by: Looks like I've gone a step too far there... (and test_contains.py seem to have a bug too). I've changed back to reporting all errors in PyUnicode_Contains() and added a few more test cases to test_contains.py (plus corrected the join() NameError).
26 years ago
Marc-Andre's third try at this bulk patch seems to work (except that his copy of test_contains.py seems to be broken -- the lines he deleted were already absent). Checkin messages: New Unicode support for int(), float(), complex() and long(). - new APIs PyInt_FromUnicode() and PyLong_FromUnicode() - added support for Unicode to PyFloat_FromString() - new encoding API PyUnicode_EncodeDecimal() which converts Unicode to a decimal char* string (used in the above new APIs) - shortcuts for calls like int(<int object>) and float(<float obj>) - tests for all of the above Unicode compares and contains checks: - comparing Unicode and non-string types now works; TypeErrors are masked, all other errors such as ValueError during Unicode coercion are passed through (note that PyUnicode_Compare does not implement the masking -- PyObject_Compare does this) - contains now works for non-string types too; TypeErrors are masked and 0 returned; all other errors are passed through Better testing support for the standard codecs. Misc minor enhancements, such as an alias dbcs for the mbcs codec. Changes: - PyLong_FromString() now applies the same error checks as does PyInt_FromString(): trailing garbage is reported as error and not longer silently ignored. The only characters which may be trailing the digits are 'L' and 'l' -- these are still silently ignored. - string.ato?() now directly interface to int(), long() and float(). The error strings are now a little different, but the type still remains the same. These functions are now ready to get declared obsolete ;-) - PyNumber_Int() now also does a check for embedded NULL chars in the input string; PyNumber_Long() already did this (and still does) Followed by: Looks like I've gone a step too far there... (and test_contains.py seem to have a bug too). I've changed back to reporting all errors in PyUnicode_Contains() and added a few more test cases to test_contains.py (plus corrected the join() NameError).
26 years ago
Marc-Andre's third try at this bulk patch seems to work (except that his copy of test_contains.py seems to be broken -- the lines he deleted were already absent). Checkin messages: New Unicode support for int(), float(), complex() and long(). - new APIs PyInt_FromUnicode() and PyLong_FromUnicode() - added support for Unicode to PyFloat_FromString() - new encoding API PyUnicode_EncodeDecimal() which converts Unicode to a decimal char* string (used in the above new APIs) - shortcuts for calls like int(<int object>) and float(<float obj>) - tests for all of the above Unicode compares and contains checks: - comparing Unicode and non-string types now works; TypeErrors are masked, all other errors such as ValueError during Unicode coercion are passed through (note that PyUnicode_Compare does not implement the masking -- PyObject_Compare does this) - contains now works for non-string types too; TypeErrors are masked and 0 returned; all other errors are passed through Better testing support for the standard codecs. Misc minor enhancements, such as an alias dbcs for the mbcs codec. Changes: - PyLong_FromString() now applies the same error checks as does PyInt_FromString(): trailing garbage is reported as error and not longer silently ignored. The only characters which may be trailing the digits are 'L' and 'l' -- these are still silently ignored. - string.ato?() now directly interface to int(), long() and float(). The error strings are now a little different, but the type still remains the same. These functions are now ready to get declared obsolete ;-) - PyNumber_Int() now also does a check for embedded NULL chars in the input string; PyNumber_Long() already did this (and still does) Followed by: Looks like I've gone a step too far there... (and test_contains.py seem to have a bug too). I've changed back to reporting all errors in PyUnicode_Contains() and added a few more test cases to test_contains.py (plus corrected the join() NameError).
26 years ago
Marc-Andre's third try at this bulk patch seems to work (except that his copy of test_contains.py seems to be broken -- the lines he deleted were already absent). Checkin messages: New Unicode support for int(), float(), complex() and long(). - new APIs PyInt_FromUnicode() and PyLong_FromUnicode() - added support for Unicode to PyFloat_FromString() - new encoding API PyUnicode_EncodeDecimal() which converts Unicode to a decimal char* string (used in the above new APIs) - shortcuts for calls like int(<int object>) and float(<float obj>) - tests for all of the above Unicode compares and contains checks: - comparing Unicode and non-string types now works; TypeErrors are masked, all other errors such as ValueError during Unicode coercion are passed through (note that PyUnicode_Compare does not implement the masking -- PyObject_Compare does this) - contains now works for non-string types too; TypeErrors are masked and 0 returned; all other errors are passed through Better testing support for the standard codecs. Misc minor enhancements, such as an alias dbcs for the mbcs codec. Changes: - PyLong_FromString() now applies the same error checks as does PyInt_FromString(): trailing garbage is reported as error and not longer silently ignored. The only characters which may be trailing the digits are 'L' and 'l' -- these are still silently ignored. - string.ato?() now directly interface to int(), long() and float(). The error strings are now a little different, but the type still remains the same. These functions are now ready to get declared obsolete ;-) - PyNumber_Int() now also does a check for embedded NULL chars in the input string; PyNumber_Long() already did this (and still does) Followed by: Looks like I've gone a step too far there... (and test_contains.py seem to have a bug too). I've changed back to reporting all errors in PyUnicode_Contains() and added a few more test cases to test_contains.py (plus corrected the join() NameError).
26 years ago
Marc-Andre's third try at this bulk patch seems to work (except that his copy of test_contains.py seems to be broken -- the lines he deleted were already absent). Checkin messages: New Unicode support for int(), float(), complex() and long(). - new APIs PyInt_FromUnicode() and PyLong_FromUnicode() - added support for Unicode to PyFloat_FromString() - new encoding API PyUnicode_EncodeDecimal() which converts Unicode to a decimal char* string (used in the above new APIs) - shortcuts for calls like int(<int object>) and float(<float obj>) - tests for all of the above Unicode compares and contains checks: - comparing Unicode and non-string types now works; TypeErrors are masked, all other errors such as ValueError during Unicode coercion are passed through (note that PyUnicode_Compare does not implement the masking -- PyObject_Compare does this) - contains now works for non-string types too; TypeErrors are masked and 0 returned; all other errors are passed through Better testing support for the standard codecs. Misc minor enhancements, such as an alias dbcs for the mbcs codec. Changes: - PyLong_FromString() now applies the same error checks as does PyInt_FromString(): trailing garbage is reported as error and not longer silently ignored. The only characters which may be trailing the digits are 'L' and 'l' -- these are still silently ignored. - string.ato?() now directly interface to int(), long() and float(). The error strings are now a little different, but the type still remains the same. These functions are now ready to get declared obsolete ;-) - PyNumber_Int() now also does a check for embedded NULL chars in the input string; PyNumber_Long() already did this (and still does) Followed by: Looks like I've gone a step too far there... (and test_contains.py seem to have a bug too). I've changed back to reporting all errors in PyUnicode_Contains() and added a few more test cases to test_contains.py (plus corrected the join() NameError).
26 years ago
  1. /* ------------------------------------------------------------------------
  2. Python Codec Registry and support functions
  3. Written by Marc-Andre Lemburg (mal@lemburg.com).
  4. Copyright (c) Corporation for National Research Initiatives.
  5. ------------------------------------------------------------------------ */
  6. #include "Python.h"
  7. #include <ctype.h>
  8. /* --- Codec Registry ----------------------------------------------------- */
  9. /* Import the standard encodings package which will register the first
  10. codec search function.
  11. This is done in a lazy way so that the Unicode implementation does
  12. not downgrade startup time of scripts not needing it.
  13. ImportErrors are silently ignored by this function. Only one try is
  14. made.
  15. */
  16. static int _PyCodecRegistry_Init(void); /* Forward */
  17. int PyCodec_Register(PyObject *search_function)
  18. {
  19. PyInterpreterState *interp = PyThreadState_GET()->interp;
  20. if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
  21. goto onError;
  22. if (search_function == NULL) {
  23. PyErr_BadArgument();
  24. goto onError;
  25. }
  26. if (!PyCallable_Check(search_function)) {
  27. PyErr_SetString(PyExc_TypeError, "argument must be callable");
  28. goto onError;
  29. }
  30. return PyList_Append(interp->codec_search_path, search_function);
  31. onError:
  32. return -1;
  33. }
  34. /* Convert a string to a normalized Python string: all characters are
  35. converted to lower case, spaces are replaced with underscores. */
  36. static
  37. PyObject *normalizestring(const char *string)
  38. {
  39. register size_t i;
  40. size_t len = strlen(string);
  41. char *p;
  42. PyObject *v;
  43. if (len > PY_SSIZE_T_MAX) {
  44. PyErr_SetString(PyExc_OverflowError, "string is too large");
  45. return NULL;
  46. }
  47. v = PyString_FromStringAndSize(NULL, len);
  48. if (v == NULL)
  49. return NULL;
  50. p = PyString_AS_STRING(v);
  51. for (i = 0; i < len; i++) {
  52. register char ch = string[i];
  53. if (ch == ' ')
  54. ch = '-';
  55. else
  56. ch = Py_TOLOWER(Py_CHARMASK(ch));
  57. p[i] = ch;
  58. }
  59. return v;
  60. }
  61. /* Lookup the given encoding and return a tuple providing the codec
  62. facilities.
  63. The encoding string is looked up converted to all lower-case
  64. characters. This makes encodings looked up through this mechanism
  65. effectively case-insensitive.
  66. If no codec is found, a LookupError is set and NULL returned.
  67. As side effect, this tries to load the encodings package, if not
  68. yet done. This is part of the lazy load strategy for the encodings
  69. package.
  70. */
  71. PyObject *_PyCodec_Lookup(const char *encoding)
  72. {
  73. PyInterpreterState *interp;
  74. PyObject *result, *args = NULL, *v;
  75. Py_ssize_t i, len;
  76. if (encoding == NULL) {
  77. PyErr_BadArgument();
  78. goto onError;
  79. }
  80. interp = PyThreadState_GET()->interp;
  81. if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
  82. goto onError;
  83. /* Convert the encoding to a normalized Python string: all
  84. characters are converted to lower case, spaces and hyphens are
  85. replaced with underscores. */
  86. v = normalizestring(encoding);
  87. if (v == NULL)
  88. goto onError;
  89. PyString_InternInPlace(&v);
  90. /* First, try to lookup the name in the registry dictionary */
  91. result = PyDict_GetItem(interp->codec_search_cache, v);
  92. if (result != NULL) {
  93. Py_INCREF(result);
  94. Py_DECREF(v);
  95. return result;
  96. }
  97. /* Next, scan the search functions in order of registration */
  98. args = PyTuple_New(1);
  99. if (args == NULL)
  100. goto onError;
  101. PyTuple_SET_ITEM(args,0,v);
  102. len = PyList_Size(interp->codec_search_path);
  103. if (len < 0)
  104. goto onError;
  105. if (len == 0) {
  106. PyErr_SetString(PyExc_LookupError,
  107. "no codec search functions registered: "
  108. "can't find encoding");
  109. goto onError;
  110. }
  111. for (i = 0; i < len; i++) {
  112. PyObject *func;
  113. func = PyList_GetItem(interp->codec_search_path, i);
  114. if (func == NULL)
  115. goto onError;
  116. result = PyEval_CallObject(func, args);
  117. if (result == NULL)
  118. goto onError;
  119. if (result == Py_None) {
  120. Py_DECREF(result);
  121. continue;
  122. }
  123. if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
  124. PyErr_SetString(PyExc_TypeError,
  125. "codec search functions must return 4-tuples");
  126. Py_DECREF(result);
  127. goto onError;
  128. }
  129. break;
  130. }
  131. if (i == len) {
  132. /* XXX Perhaps we should cache misses too ? */
  133. PyErr_Format(PyExc_LookupError,
  134. "unknown encoding: %s", encoding);
  135. goto onError;
  136. }
  137. /* Cache and return the result */
  138. PyDict_SetItem(interp->codec_search_cache, v, result);
  139. Py_DECREF(args);
  140. return result;
  141. onError:
  142. Py_XDECREF(args);
  143. return NULL;
  144. }
  145. static
  146. PyObject *args_tuple(PyObject *object,
  147. const char *errors)
  148. {
  149. PyObject *args;
  150. args = PyTuple_New(1 + (errors != NULL));
  151. if (args == NULL)
  152. return NULL;
  153. Py_INCREF(object);
  154. PyTuple_SET_ITEM(args,0,object);
  155. if (errors) {
  156. PyObject *v;
  157. v = PyString_FromString(errors);
  158. if (v == NULL) {
  159. Py_DECREF(args);
  160. return NULL;
  161. }
  162. PyTuple_SET_ITEM(args, 1, v);
  163. }
  164. return args;
  165. }
  166. /* Helper function to get a codec item */
  167. static
  168. PyObject *codec_getitem(const char *encoding, int index)
  169. {
  170. PyObject *codecs;
  171. PyObject *v;
  172. codecs = _PyCodec_Lookup(encoding);
  173. if (codecs == NULL)
  174. return NULL;
  175. v = PyTuple_GET_ITEM(codecs, index);
  176. Py_DECREF(codecs);
  177. Py_INCREF(v);
  178. return v;
  179. }
  180. /* Helper function to create an incremental codec. */
  181. static
  182. PyObject *codec_getincrementalcodec(const char *encoding,
  183. const char *errors,
  184. const char *attrname)
  185. {
  186. PyObject *codecs, *ret, *inccodec;
  187. codecs = _PyCodec_Lookup(encoding);
  188. if (codecs == NULL)
  189. return NULL;
  190. inccodec = PyObject_GetAttrString(codecs, attrname);
  191. Py_DECREF(codecs);
  192. if (inccodec == NULL)
  193. return NULL;
  194. if (errors)
  195. ret = PyObject_CallFunction(inccodec, "s", errors);
  196. else
  197. ret = PyObject_CallFunction(inccodec, NULL);
  198. Py_DECREF(inccodec);
  199. return ret;
  200. }
  201. /* Helper function to create a stream codec. */
  202. static
  203. PyObject *codec_getstreamcodec(const char *encoding,
  204. PyObject *stream,
  205. const char *errors,
  206. const int index)
  207. {
  208. PyObject *codecs, *streamcodec, *codeccls;
  209. codecs = _PyCodec_Lookup(encoding);
  210. if (codecs == NULL)
  211. return NULL;
  212. codeccls = PyTuple_GET_ITEM(codecs, index);
  213. if (errors != NULL)
  214. streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
  215. else
  216. streamcodec = PyObject_CallFunction(codeccls, "O", stream);
  217. Py_DECREF(codecs);
  218. return streamcodec;
  219. }
  220. /* Convenience APIs to query the Codec registry.
  221. All APIs return a codec object with incremented refcount.
  222. */
  223. PyObject *PyCodec_Encoder(const char *encoding)
  224. {
  225. return codec_getitem(encoding, 0);
  226. }
  227. PyObject *PyCodec_Decoder(const char *encoding)
  228. {
  229. return codec_getitem(encoding, 1);
  230. }
  231. PyObject *PyCodec_IncrementalEncoder(const char *encoding,
  232. const char *errors)
  233. {
  234. return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
  235. }
  236. PyObject *PyCodec_IncrementalDecoder(const char *encoding,
  237. const char *errors)
  238. {
  239. return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
  240. }
  241. PyObject *PyCodec_StreamReader(const char *encoding,
  242. PyObject *stream,
  243. const char *errors)
  244. {
  245. return codec_getstreamcodec(encoding, stream, errors, 2);
  246. }
  247. PyObject *PyCodec_StreamWriter(const char *encoding,
  248. PyObject *stream,
  249. const char *errors)
  250. {
  251. return codec_getstreamcodec(encoding, stream, errors, 3);
  252. }
  253. /* Encode an object (e.g. an Unicode object) using the given encoding
  254. and return the resulting encoded object (usually a Python string).
  255. errors is passed to the encoder factory as argument if non-NULL. */
  256. PyObject *PyCodec_Encode(PyObject *object,
  257. const char *encoding,
  258. const char *errors)
  259. {
  260. PyObject *encoder = NULL;
  261. PyObject *args = NULL, *result = NULL;
  262. PyObject *v;
  263. encoder = PyCodec_Encoder(encoding);
  264. if (encoder == NULL)
  265. goto onError;
  266. args = args_tuple(object, errors);
  267. if (args == NULL)
  268. goto onError;
  269. result = PyEval_CallObject(encoder,args);
  270. if (result == NULL)
  271. goto onError;
  272. if (!PyTuple_Check(result) ||
  273. PyTuple_GET_SIZE(result) != 2) {
  274. PyErr_SetString(PyExc_TypeError,
  275. "encoder must return a tuple (object,integer)");
  276. goto onError;
  277. }
  278. v = PyTuple_GET_ITEM(result,0);
  279. Py_INCREF(v);
  280. /* We don't check or use the second (integer) entry. */
  281. Py_DECREF(args);
  282. Py_DECREF(encoder);
  283. Py_DECREF(result);
  284. return v;
  285. onError:
  286. Py_XDECREF(result);
  287. Py_XDECREF(args);
  288. Py_XDECREF(encoder);
  289. return NULL;
  290. }
  291. /* Decode an object (usually a Python string) using the given encoding
  292. and return an equivalent object (e.g. an Unicode object).
  293. errors is passed to the decoder factory as argument if non-NULL. */
  294. PyObject *PyCodec_Decode(PyObject *object,
  295. const char *encoding,
  296. const char *errors)
  297. {
  298. PyObject *decoder = NULL;
  299. PyObject *args = NULL, *result = NULL;
  300. PyObject *v;
  301. decoder = PyCodec_Decoder(encoding);
  302. if (decoder == NULL)
  303. goto onError;
  304. args = args_tuple(object, errors);
  305. if (args == NULL)
  306. goto onError;
  307. result = PyEval_CallObject(decoder,args);
  308. if (result == NULL)
  309. goto onError;
  310. if (!PyTuple_Check(result) ||
  311. PyTuple_GET_SIZE(result) != 2) {
  312. PyErr_SetString(PyExc_TypeError,
  313. "decoder must return a tuple (object,integer)");
  314. goto onError;
  315. }
  316. v = PyTuple_GET_ITEM(result,0);
  317. Py_INCREF(v);
  318. /* We don't check or use the second (integer) entry. */
  319. Py_DECREF(args);
  320. Py_DECREF(decoder);
  321. Py_DECREF(result);
  322. return v;
  323. onError:
  324. Py_XDECREF(args);
  325. Py_XDECREF(decoder);
  326. Py_XDECREF(result);
  327. return NULL;
  328. }
  329. /* Register the error handling callback function error under the name
  330. name. This function will be called by the codec when it encounters
  331. an unencodable characters/undecodable bytes and doesn't know the
  332. callback name, when name is specified as the error parameter
  333. in the call to the encode/decode function.
  334. Return 0 on success, -1 on error */
  335. int PyCodec_RegisterError(const char *name, PyObject *error)
  336. {
  337. PyInterpreterState *interp = PyThreadState_GET()->interp;
  338. if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
  339. return -1;
  340. if (!PyCallable_Check(error)) {
  341. PyErr_SetString(PyExc_TypeError, "handler must be callable");
  342. return -1;
  343. }
  344. return PyDict_SetItemString(interp->codec_error_registry,
  345. (char *)name, error);
  346. }
  347. /* Lookup the error handling callback function registered under the
  348. name error. As a special case NULL can be passed, in which case
  349. the error handling callback for strict encoding will be returned. */
  350. PyObject *PyCodec_LookupError(const char *name)
  351. {
  352. PyObject *handler = NULL;
  353. PyInterpreterState *interp = PyThreadState_GET()->interp;
  354. if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
  355. return NULL;
  356. if (name==NULL)
  357. name = "strict";
  358. handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
  359. if (!handler)
  360. PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
  361. else
  362. Py_INCREF(handler);
  363. return handler;
  364. }
  365. static void wrong_exception_type(PyObject *exc)
  366. {
  367. PyObject *type = PyObject_GetAttrString(exc, "__class__");
  368. if (type != NULL) {
  369. PyObject *name = PyObject_GetAttrString(type, "__name__");
  370. Py_DECREF(type);
  371. if (name != NULL) {
  372. PyObject *string = PyObject_Str(name);
  373. Py_DECREF(name);
  374. if (string != NULL) {
  375. PyErr_Format(PyExc_TypeError,
  376. "don't know how to handle %.400s in error callback",
  377. PyString_AS_STRING(string));
  378. Py_DECREF(string);
  379. }
  380. }
  381. }
  382. }
  383. PyObject *PyCodec_StrictErrors(PyObject *exc)
  384. {
  385. if (PyExceptionInstance_Check(exc))
  386. PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
  387. else
  388. PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
  389. return NULL;
  390. }
  391. #ifdef Py_USING_UNICODE
  392. PyObject *PyCodec_IgnoreErrors(PyObject *exc)
  393. {
  394. Py_ssize_t end;
  395. if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
  396. if (PyUnicodeEncodeError_GetEnd(exc, &end))
  397. return NULL;
  398. }
  399. else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
  400. if (PyUnicodeDecodeError_GetEnd(exc, &end))
  401. return NULL;
  402. }
  403. else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
  404. if (PyUnicodeTranslateError_GetEnd(exc, &end))
  405. return NULL;
  406. }
  407. else {
  408. wrong_exception_type(exc);
  409. return NULL;
  410. }
  411. /* ouch: passing NULL, 0, pos gives None instead of u'' */
  412. return Py_BuildValue("(u#n)", &end, 0, end);
  413. }
  414. PyObject *PyCodec_ReplaceErrors(PyObject *exc)
  415. {
  416. PyObject *restuple;
  417. Py_ssize_t start;
  418. Py_ssize_t end;
  419. Py_ssize_t i;
  420. if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
  421. PyObject *res;
  422. Py_UNICODE *p;
  423. if (PyUnicodeEncodeError_GetStart(exc, &start))
  424. return NULL;
  425. if (PyUnicodeEncodeError_GetEnd(exc, &end))
  426. return NULL;
  427. res = PyUnicode_FromUnicode(NULL, end-start);
  428. if (res == NULL)
  429. return NULL;
  430. for (p = PyUnicode_AS_UNICODE(res), i = start;
  431. i<end; ++p, ++i)
  432. *p = '?';
  433. restuple = Py_BuildValue("(On)", res, end);
  434. Py_DECREF(res);
  435. return restuple;
  436. }
  437. else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
  438. Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
  439. if (PyUnicodeDecodeError_GetEnd(exc, &end))
  440. return NULL;
  441. return Py_BuildValue("(u#n)", &res, 1, end);
  442. }
  443. else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
  444. PyObject *res;
  445. Py_UNICODE *p;
  446. if (PyUnicodeTranslateError_GetStart(exc, &start))
  447. return NULL;
  448. if (PyUnicodeTranslateError_GetEnd(exc, &end))
  449. return NULL;
  450. res = PyUnicode_FromUnicode(NULL, end-start);
  451. if (res == NULL)
  452. return NULL;
  453. for (p = PyUnicode_AS_UNICODE(res), i = start;
  454. i<end; ++p, ++i)
  455. *p = Py_UNICODE_REPLACEMENT_CHARACTER;
  456. restuple = Py_BuildValue("(On)", res, end);
  457. Py_DECREF(res);
  458. return restuple;
  459. }
  460. else {
  461. wrong_exception_type(exc);
  462. return NULL;
  463. }
  464. }
  465. PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
  466. {
  467. if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
  468. PyObject *restuple;
  469. PyObject *object;
  470. Py_ssize_t start;
  471. Py_ssize_t end;
  472. PyObject *res;
  473. Py_UNICODE *p;
  474. Py_UNICODE *startp;
  475. Py_UNICODE *outp;
  476. int ressize;
  477. if (PyUnicodeEncodeError_GetStart(exc, &start))
  478. return NULL;
  479. if (PyUnicodeEncodeError_GetEnd(exc, &end))
  480. return NULL;
  481. if (!(object = PyUnicodeEncodeError_GetObject(exc)))
  482. return NULL;
  483. startp = PyUnicode_AS_UNICODE(object);
  484. for (p = startp+start, ressize = 0; p < startp+end; ++p) {
  485. if (*p<10)
  486. ressize += 2+1+1;
  487. else if (*p<100)
  488. ressize += 2+2+1;
  489. else if (*p<1000)
  490. ressize += 2+3+1;
  491. else if (*p<10000)
  492. ressize += 2+4+1;
  493. #ifndef Py_UNICODE_WIDE
  494. else
  495. ressize += 2+5+1;
  496. #else
  497. else if (*p<100000)
  498. ressize += 2+5+1;
  499. else if (*p<1000000)
  500. ressize += 2+6+1;
  501. else
  502. ressize += 2+7+1;
  503. #endif
  504. }
  505. /* allocate replacement */
  506. res = PyUnicode_FromUnicode(NULL, ressize);
  507. if (res == NULL) {
  508. Py_DECREF(object);
  509. return NULL;
  510. }
  511. /* generate replacement */
  512. for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
  513. p < startp+end; ++p) {
  514. Py_UNICODE c = *p;
  515. int digits;
  516. int base;
  517. *outp++ = '&';
  518. *outp++ = '#';
  519. if (*p<10) {
  520. digits = 1;
  521. base = 1;
  522. }
  523. else if (*p<100) {
  524. digits = 2;
  525. base = 10;
  526. }
  527. else if (*p<1000) {
  528. digits = 3;
  529. base = 100;
  530. }
  531. else if (*p<10000) {
  532. digits = 4;
  533. base = 1000;
  534. }
  535. #ifndef Py_UNICODE_WIDE
  536. else {
  537. digits = 5;
  538. base = 10000;
  539. }
  540. #else
  541. else if (*p<100000) {
  542. digits = 5;
  543. base = 10000;
  544. }
  545. else if (*p<1000000) {
  546. digits = 6;
  547. base = 100000;
  548. }
  549. else {
  550. digits = 7;
  551. base = 1000000;
  552. }
  553. #endif
  554. while (digits-->0) {
  555. *outp++ = '0' + c/base;
  556. c %= base;
  557. base /= 10;
  558. }
  559. *outp++ = ';';
  560. }
  561. restuple = Py_BuildValue("(On)", res, end);
  562. Py_DECREF(res);
  563. Py_DECREF(object);
  564. return restuple;
  565. }
  566. else {
  567. wrong_exception_type(exc);
  568. return NULL;
  569. }
  570. }
  571. static Py_UNICODE hexdigits[] = {
  572. '0', '1', '2', '3', '4', '5', '6', '7',
  573. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  574. };
  575. PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
  576. {
  577. if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
  578. PyObject *restuple;
  579. PyObject *object;
  580. Py_ssize_t start;
  581. Py_ssize_t end;
  582. PyObject *res;
  583. Py_UNICODE *p;
  584. Py_UNICODE *startp;
  585. Py_UNICODE *outp;
  586. int ressize;
  587. if (PyUnicodeEncodeError_GetStart(exc, &start))
  588. return NULL;
  589. if (PyUnicodeEncodeError_GetEnd(exc, &end))
  590. return NULL;
  591. if (!(object = PyUnicodeEncodeError_GetObject(exc)))
  592. return NULL;
  593. startp = PyUnicode_AS_UNICODE(object);
  594. for (p = startp+start, ressize = 0; p < startp+end; ++p) {
  595. #ifdef Py_UNICODE_WIDE
  596. if (*p >= 0x00010000)
  597. ressize += 1+1+8;
  598. else
  599. #endif
  600. if (*p >= 0x100) {
  601. ressize += 1+1+4;
  602. }
  603. else
  604. ressize += 1+1+2;
  605. }
  606. res = PyUnicode_FromUnicode(NULL, ressize);
  607. if (res==NULL)
  608. return NULL;
  609. for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
  610. p < startp+end; ++p) {
  611. Py_UNICODE c = *p;
  612. *outp++ = '\\';
  613. #ifdef Py_UNICODE_WIDE
  614. if (c >= 0x00010000) {
  615. *outp++ = 'U';
  616. *outp++ = hexdigits[(c>>28)&0xf];
  617. *outp++ = hexdigits[(c>>24)&0xf];
  618. *outp++ = hexdigits[(c>>20)&0xf];
  619. *outp++ = hexdigits[(c>>16)&0xf];
  620. *outp++ = hexdigits[(c>>12)&0xf];
  621. *outp++ = hexdigits[(c>>8)&0xf];
  622. }
  623. else
  624. #endif
  625. if (c >= 0x100) {
  626. *outp++ = 'u';
  627. *outp++ = hexdigits[(c>>12)&0xf];
  628. *outp++ = hexdigits[(c>>8)&0xf];
  629. }
  630. else
  631. *outp++ = 'x';
  632. *outp++ = hexdigits[(c>>4)&0xf];
  633. *outp++ = hexdigits[c&0xf];
  634. }
  635. restuple = Py_BuildValue("(On)", res, end);
  636. Py_DECREF(res);
  637. Py_DECREF(object);
  638. return restuple;
  639. }
  640. else {
  641. wrong_exception_type(exc);
  642. return NULL;
  643. }
  644. }
  645. #endif
  646. static PyObject *strict_errors(PyObject *self, PyObject *exc)
  647. {
  648. return PyCodec_StrictErrors(exc);
  649. }
  650. #ifdef Py_USING_UNICODE
  651. static PyObject *ignore_errors(PyObject *self, PyObject *exc)
  652. {
  653. return PyCodec_IgnoreErrors(exc);
  654. }
  655. static PyObject *replace_errors(PyObject *self, PyObject *exc)
  656. {
  657. return PyCodec_ReplaceErrors(exc);
  658. }
  659. static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
  660. {
  661. return PyCodec_XMLCharRefReplaceErrors(exc);
  662. }
  663. static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
  664. {
  665. return PyCodec_BackslashReplaceErrors(exc);
  666. }
  667. #endif
  668. static int _PyCodecRegistry_Init(void)
  669. {
  670. static struct {
  671. char *name;
  672. PyMethodDef def;
  673. } methods[] =
  674. {
  675. {
  676. "strict",
  677. {
  678. "strict_errors",
  679. strict_errors,
  680. METH_O,
  681. PyDoc_STR("Implements the 'strict' error handling, which "
  682. "raises a UnicodeError on coding errors.")
  683. }
  684. },
  685. #ifdef Py_USING_UNICODE
  686. {
  687. "ignore",
  688. {
  689. "ignore_errors",
  690. ignore_errors,
  691. METH_O,
  692. PyDoc_STR("Implements the 'ignore' error handling, which "
  693. "ignores malformed data and continues.")
  694. }
  695. },
  696. {
  697. "replace",
  698. {
  699. "replace_errors",
  700. replace_errors,
  701. METH_O,
  702. PyDoc_STR("Implements the 'replace' error handling, which "
  703. "replaces malformed data with a replacement marker.")
  704. }
  705. },
  706. {
  707. "xmlcharrefreplace",
  708. {
  709. "xmlcharrefreplace_errors",
  710. xmlcharrefreplace_errors,
  711. METH_O,
  712. PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
  713. "which replaces an unencodable character with the "
  714. "appropriate XML character reference.")
  715. }
  716. },
  717. {
  718. "backslashreplace",
  719. {
  720. "backslashreplace_errors",
  721. backslashreplace_errors,
  722. METH_O,
  723. PyDoc_STR("Implements the 'backslashreplace' error handling, "
  724. "which replaces an unencodable character with a "
  725. "backslashed escape sequence.")
  726. }
  727. }
  728. #endif
  729. };
  730. PyInterpreterState *interp = PyThreadState_GET()->interp;
  731. PyObject *mod;
  732. unsigned i;
  733. if (interp->codec_search_path != NULL)
  734. return 0;
  735. interp->codec_search_path = PyList_New(0);
  736. interp->codec_search_cache = PyDict_New();
  737. interp->codec_error_registry = PyDict_New();
  738. if (interp->codec_error_registry) {
  739. for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
  740. PyObject *func = PyCFunction_New(&methods[i].def, NULL);
  741. int res;
  742. if (!func)
  743. Py_FatalError("can't initialize codec error registry");
  744. res = PyCodec_RegisterError(methods[i].name, func);
  745. Py_DECREF(func);
  746. if (res)
  747. Py_FatalError("can't initialize codec error registry");
  748. }
  749. }
  750. if (interp->codec_search_path == NULL ||
  751. interp->codec_search_cache == NULL ||
  752. interp->codec_error_registry == NULL)
  753. Py_FatalError("can't initialize codec registry");
  754. mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0);
  755. if (mod == NULL) {
  756. if (PyErr_ExceptionMatches(PyExc_ImportError)) {
  757. /* Ignore ImportErrors... this is done so that
  758. distributions can disable the encodings package. Note
  759. that other errors are not masked, e.g. SystemErrors
  760. raised to inform the user of an error in the Python
  761. configuration are still reported back to the user. */
  762. PyErr_Clear();
  763. return 0;
  764. }
  765. return -1;
  766. }
  767. Py_DECREF(mod);
  768. return 0;
  769. }