mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00

svn+ssh://pythondev@svn.python.org/python/branches/p3yk ................ r55837 | guido.van.rossum | 2007-06-08 16:04:42 -0700 (Fri, 08 Jun 2007) | 2 lines PEP 3119 -- the abc module. ................ r55838 | guido.van.rossum | 2007-06-08 17:38:55 -0700 (Fri, 08 Jun 2007) | 2 lines Implement part of PEP 3119 -- One Trick Ponies. ................ r55847 | guido.van.rossum | 2007-06-09 08:28:06 -0700 (Sat, 09 Jun 2007) | 2 lines Different way to do one trick ponies, allowing registration (per PEP strawman). ................ r55849 | guido.van.rossum | 2007-06-09 18:06:38 -0700 (Sat, 09 Jun 2007) | 3 lines Make sure that the magic looking for __hash__ (etc.) doesn't apply to real subclasses of Hashable. ................ r55852 | guido.van.rossum | 2007-06-10 08:29:51 -0700 (Sun, 10 Jun 2007) | 2 lines Add some more examples, e.g. generators and dict views. ................ r55853 | guido.van.rossum | 2007-06-10 08:31:59 -0700 (Sun, 10 Jun 2007) | 2 lines keys() and items() *are* containers -- just values() isn't. ................ r55864 | georg.brandl | 2007-06-10 15:29:40 -0700 (Sun, 10 Jun 2007) | 2 lines PEP 3127: new octal literals, binary literals. ................ r55865 | georg.brandl | 2007-06-10 15:31:37 -0700 (Sun, 10 Jun 2007) | 2 lines Some octal literal fixes in Tools. ................ r55866 | georg.brandl | 2007-06-10 15:37:43 -0700 (Sun, 10 Jun 2007) | 2 lines Tokenizer changes for PEP 3127. ................ r55867 | georg.brandl | 2007-06-10 15:37:55 -0700 (Sun, 10 Jun 2007) | 2 lines Some docs for PEP 3127. ................ r55868 | georg.brandl | 2007-06-10 15:44:39 -0700 (Sun, 10 Jun 2007) | 2 lines Missed a place in intobject.c. Is that used anymore anyway? ................ r55871 | neal.norwitz | 2007-06-10 18:31:49 -0700 (Sun, 10 Jun 2007) | 182 lines Merged revisions 55729-55868 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r55731 | neal.norwitz | 2007-06-01 00:29:12 -0700 (Fri, 01 Jun 2007) | 7 lines SF 1668596/1720897: distutils now copies data files even if package_dir is empty. This needs to be backported. I'm too tired tonight. It would be great if someone backports this if the buildbots are ok with it. Otherwise, I will try to get to it tomorrow. ........ r55732 | georg.brandl | 2007-06-01 04:33:33 -0700 (Fri, 01 Jun 2007) | 2 lines Bug #1722484: remove docstrings again when running with -OO. ........ r55735 | georg.brandl | 2007-06-01 12:20:27 -0700 (Fri, 01 Jun 2007) | 2 lines Fix wrong issue number. ........ r55739 | brett.cannon | 2007-06-01 20:02:29 -0700 (Fri, 01 Jun 2007) | 3 lines Have configure raise an error when building on AtheOS. Code specific to AtheOS will be removed in Python 2.7. ........ r55746 | neal.norwitz | 2007-06-02 11:33:53 -0700 (Sat, 02 Jun 2007) | 1 line Update expected birthday of 2.6 ........ r55751 | neal.norwitz | 2007-06-03 13:32:50 -0700 (Sun, 03 Jun 2007) | 10 lines Backout the original 'fix' to 1721309 which had no effect. Different versions of Berkeley DB handle this differently. The comments and bug report should have the details. Memory is allocated in 4.4 (and presumably earlier), but not in 4.5. Thus 4.5 has the free error, but not earlier versions. Mostly update comments, plus make the free conditional. This fix was already applied to the 2.5 branch. ........ r55752 | brett.cannon | 2007-06-03 16:13:41 -0700 (Sun, 03 Jun 2007) | 6 lines Make _strptime.TimeRE().pattern() use ``\s+`` for matching whitespace instead of ``\s*``. This prevents patterns from "stealing" bits from other patterns in order to make a match work. Closes bug #1730389. Will be backported. ........ r55766 | hyeshik.chang | 2007-06-05 11:16:52 -0700 (Tue, 05 Jun 2007) | 4 lines Fix build on FreeBSD. Bluetooth HCI API in FreeBSD is quite different from Linux's. Just fix the build for now but the code doesn't support the complete capability of HCI on FreeBSD yet. ........ r55770 | hyeshik.chang | 2007-06-05 11:58:51 -0700 (Tue, 05 Jun 2007) | 4 lines Bug #1728403: Fix a bug that CJKCodecs StreamReader hangs when it reads a file that ends with incomplete sequence and sizehint argument for .read() is specified. ........ r55775 | hyeshik.chang | 2007-06-05 12:28:15 -0700 (Tue, 05 Jun 2007) | 2 lines Fix for Windows: close a temporary file before trying to delete it. ........ r55783 | guido.van.rossum | 2007-06-05 14:24:47 -0700 (Tue, 05 Jun 2007) | 2 lines Patch by Tim Delany (missing DECREF). SF #1731330. ........ r55785 | collin.winter | 2007-06-05 17:17:35 -0700 (Tue, 05 Jun 2007) | 3 lines Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O). ........ r55786 | facundo.batista | 2007-06-06 08:13:37 -0700 (Wed, 06 Jun 2007) | 4 lines FTP.ntransfercmd method now uses create_connection when passive, using the timeout received in connection time. ........ r55792 | facundo.batista | 2007-06-06 10:15:23 -0700 (Wed, 06 Jun 2007) | 7 lines Added an optional timeout parameter to function urllib2.urlopen, with tests in test_urllib2net.py (must have network resource enabled to execute them). Also modified test_urllib2.py because testing mock classes must take it into acount. Docs are also updated. ........ r55793 | thomas.heller | 2007-06-06 13:19:19 -0700 (Wed, 06 Jun 2007) | 1 line Build _ctypes and _ctypes_test in the ReleaseAMD64 configuration. ........ r55802 | georg.brandl | 2007-06-07 06:23:24 -0700 (Thu, 07 Jun 2007) | 3 lines Disallow function calls like foo(None=1). Backport from py3k rev. 55708 by Guido. ........ r55804 | georg.brandl | 2007-06-07 06:30:24 -0700 (Thu, 07 Jun 2007) | 2 lines Make reindent.py executable. ........ r55805 | georg.brandl | 2007-06-07 06:34:10 -0700 (Thu, 07 Jun 2007) | 2 lines Patch #1667860: Fix UnboundLocalError in urllib2. ........ r55821 | kristjan.jonsson | 2007-06-07 16:53:49 -0700 (Thu, 07 Jun 2007) | 1 line Fixing changes to getbuildinfo.c that broke linux builds ........ r55828 | thomas.heller | 2007-06-08 09:10:27 -0700 (Fri, 08 Jun 2007) | 1 line Make this test work with older Python releases where struct has no 't' format character. ........ r55829 | martin.v.loewis | 2007-06-08 10:29:20 -0700 (Fri, 08 Jun 2007) | 3 lines Bug #1733488: Fix compilation of bufferobject.c on AIX. Will backport to 2.5. ........ r55831 | thomas.heller | 2007-06-08 11:20:09 -0700 (Fri, 08 Jun 2007) | 2 lines [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur with small modifications. ........ r55832 | thomas.heller | 2007-06-08 12:01:06 -0700 (Fri, 08 Jun 2007) | 1 line Fix gcc warnings intruduced by passing Py_ssize_t to PyErr_Format calls. ........ r55833 | thomas.heller | 2007-06-08 12:08:31 -0700 (Fri, 08 Jun 2007) | 2 lines Fix wrong documentation, and correct the punktuation. Closes [1700455]. ........ r55834 | thomas.heller | 2007-06-08 12:14:23 -0700 (Fri, 08 Jun 2007) | 1 line Fix warnings by using proper function prototype. ........ r55839 | neal.norwitz | 2007-06-08 20:36:34 -0700 (Fri, 08 Jun 2007) | 7 lines Prevent expandtabs() on string and unicode objects from causing a segfault when a large width is passed on 32-bit platforms. Found by Google. It would be good for people to review this especially carefully and verify I don't have an off by one error and there is no other way to cause overflow. ........ r55841 | neal.norwitz | 2007-06-08 21:48:22 -0700 (Fri, 08 Jun 2007) | 1 line Use macro version of GET_SIZE to avoid Coverity warning (#150) about a possible error. ........ r55842 | martin.v.loewis | 2007-06-09 00:42:52 -0700 (Sat, 09 Jun 2007) | 3 lines Patch #1733960: Allow T_LONGLONG to accept ints. Will backport to 2.5. ........ r55843 | martin.v.loewis | 2007-06-09 00:58:05 -0700 (Sat, 09 Jun 2007) | 2 lines Fix Windows build. ........ r55845 | martin.v.loewis | 2007-06-09 03:10:26 -0700 (Sat, 09 Jun 2007) | 2 lines Provide LLONG_MAX for S390. ........ r55854 | thomas.heller | 2007-06-10 08:59:17 -0700 (Sun, 10 Jun 2007) | 4 lines First version of build scripts for Windows/AMD64 (no external components are built yet, and 'kill_python' is disabled). ........ r55855 | thomas.heller | 2007-06-10 10:55:51 -0700 (Sun, 10 Jun 2007) | 3 lines For now, disable the _bsddb, _sqlite3, _ssl, _testcapi, _tkinter modules in the ReleaseAMD64 configuration because they do not compile. ........ r55856 | thomas.heller | 2007-06-10 11:27:54 -0700 (Sun, 10 Jun 2007) | 1 line Need to set the environment variables, otherwise devenv.com is not found. ........ r55860 | thomas.heller | 2007-06-10 14:01:17 -0700 (Sun, 10 Jun 2007) | 1 line Revert commit 55855. ........ ................ r55880 | neal.norwitz | 2007-06-10 22:07:36 -0700 (Sun, 10 Jun 2007) | 5 lines Fix the refleak counter on test_collections. The ABC metaclass creates a registry which must be cleared on each run. Otherwise, there *seem* to be refleaks when there really aren't any. (The class is held within the registry even though it's no longer needed.) ................ r55884 | neal.norwitz | 2007-06-10 22:46:33 -0700 (Sun, 10 Jun 2007) | 1 line These tests have been removed, so they are no longer needed here ................ r55886 | georg.brandl | 2007-06-11 00:26:37 -0700 (Mon, 11 Jun 2007) | 3 lines Optimize access to True and False in the compiler (if True) and the peepholer (LOAD_NAME True). ................ r55905 | georg.brandl | 2007-06-11 10:02:26 -0700 (Mon, 11 Jun 2007) | 5 lines Remove __oct__ and __hex__ and use __index__ for converting non-ints before formatting in a base. Add a bin() builtin. ................ r55906 | georg.brandl | 2007-06-11 10:04:44 -0700 (Mon, 11 Jun 2007) | 2 lines int(x, 0) does not "guess". ................ r55907 | georg.brandl | 2007-06-11 10:05:47 -0700 (Mon, 11 Jun 2007) | 2 lines Add a comment to explain that nb_oct and nb_hex are nonfunctional. ................ r55908 | guido.van.rossum | 2007-06-11 10:49:18 -0700 (Mon, 11 Jun 2007) | 2 lines Get rid of unused imports and comment. ................ r55910 | guido.van.rossum | 2007-06-11 13:05:17 -0700 (Mon, 11 Jun 2007) | 2 lines _Abstract.__new__ now requires either no arguments or __init__ overridden. ................ r55911 | guido.van.rossum | 2007-06-11 13:07:49 -0700 (Mon, 11 Jun 2007) | 7 lines Move the collections ABCs to a separate file, _abcoll.py, in order to avoid needing to import _collections.so during the bootstrap (this will become apparent in the next submit of os.py). Add (plain and mutable) ABCs for Set, Mapping, Sequence. ................ r55912 | guido.van.rossum | 2007-06-11 13:09:31 -0700 (Mon, 11 Jun 2007) | 2 lines Rewrite the _Environ class to use the new collections ABCs. ................ r55913 | guido.van.rossum | 2007-06-11 13:59:45 -0700 (Mon, 11 Jun 2007) | 72 lines Merged revisions 55869-55912 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r55869 | neal.norwitz | 2007-06-10 17:42:11 -0700 (Sun, 10 Jun 2007) | 1 line Add Atul Varma for patch # 1667860 ........ r55870 | neal.norwitz | 2007-06-10 18:22:03 -0700 (Sun, 10 Jun 2007) | 1 line Ignore valgrind problems on Ubuntu from ld ........ r55872 | neal.norwitz | 2007-06-10 18:48:46 -0700 (Sun, 10 Jun 2007) | 2 lines Ignore config.status.lineno which seems new (new autoconf?) ........ r55873 | neal.norwitz | 2007-06-10 19:14:39 -0700 (Sun, 10 Jun 2007) | 1 line Prevent these tests from running on Win64 since they don\'t apply there either ........ r55874 | neal.norwitz | 2007-06-10 19:16:10 -0700 (Sun, 10 Jun 2007) | 5 lines Fix a bug when there was a newline in the string expandtabs was called on. This also catches another condition that can overflow. Will backport. ........ r55879 | neal.norwitz | 2007-06-10 21:52:37 -0700 (Sun, 10 Jun 2007) | 1 line Prevent hang if the port cannot be opened. ........ r55881 | neal.norwitz | 2007-06-10 22:28:45 -0700 (Sun, 10 Jun 2007) | 4 lines Add all of the distuils modules that don't seem to have explicit tests. :-( Move an import in mworkscompiler so that this module can be imported on any platform. Hopefully this works on all platforms. ........ r55882 | neal.norwitz | 2007-06-10 22:35:10 -0700 (Sun, 10 Jun 2007) | 4 lines SF #1734732, lower case the module names per PEP 8. Will backport. ........ r55885 | neal.norwitz | 2007-06-10 23:16:48 -0700 (Sun, 10 Jun 2007) | 4 lines Not sure why this only fails sometimes on Unix machines. Better to disable it and only import msvccompiler on Windows since that's the only place it can work anyways. ........ r55887 | neal.norwitz | 2007-06-11 00:29:43 -0700 (Mon, 11 Jun 2007) | 4 lines Bug #1734723: Fix repr.Repr() so it doesn't ignore the maxtuple attribute. Will backport ........ r55889 | neal.norwitz | 2007-06-11 00:36:24 -0700 (Mon, 11 Jun 2007) | 1 line Reflow long line ........ r55896 | thomas.heller | 2007-06-11 08:58:33 -0700 (Mon, 11 Jun 2007) | 3 lines Use "O&" in calls to PyArg_Parse when we need a 'void*' instead of "k" or "K" codes. ........ r55901 | facundo.batista | 2007-06-11 09:27:08 -0700 (Mon, 11 Jun 2007) | 5 lines Added versionchanged flag to all the methods which received a new optional timeout parameter, and a versionadded flag to the socket.create_connection function. ........ ................ r55914 | guido.van.rossum | 2007-06-11 14:19:50 -0700 (Mon, 11 Jun 2007) | 3 lines New super() implementation, for PEP 3135 (though the PEP is not yet updated to this design, and small tweaks may still be made later). ................ r55923 | guido.van.rossum | 2007-06-11 21:15:24 -0700 (Mon, 11 Jun 2007) | 4 lines I'm guessing this module broke when Neal ripped out the types module -- it used 'list' both as a local variable and as the built-in list type. Renamed the local variable since that was easier. ................ r55924 | guido.van.rossum | 2007-06-11 21:20:05 -0700 (Mon, 11 Jun 2007) | 5 lines Change all occurrences of super(<thisclass>, <firstarg>) to super(). Seems to have worked, all the tests still pass. Exception: test_descr and test_descrtut, which have tons of these and are there to test the various usages. ................ r55939 | collin.winter | 2007-06-12 13:57:33 -0700 (Tue, 12 Jun 2007) | 1 line Patch #1735485: remove StandardError from the exception hierarchy. ................ r55954 | neal.norwitz | 2007-06-12 21:56:32 -0700 (Tue, 12 Jun 2007) | 51 lines Merged revisions 55913-55950 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r55926 | marc-andre.lemburg | 2007-06-12 02:09:58 -0700 (Tue, 12 Jun 2007) | 3 lines Apply patch #1734945 to support TurboLinux as distribution. ........ r55927 | marc-andre.lemburg | 2007-06-12 02:26:49 -0700 (Tue, 12 Jun 2007) | 3 lines Add patch #1726668: Windows Vista support. ........ r55929 | thomas.heller | 2007-06-12 08:36:22 -0700 (Tue, 12 Jun 2007) | 1 line Checkout, but do not yet try to build, exernal sources. ........ r55930 | thomas.heller | 2007-06-12 09:08:27 -0700 (Tue, 12 Jun 2007) | 6 lines Add bufferoverflowU.lib to the libraries needed by _ssl (is this the right thing to do?). Set the /XP64 /RETAIL build enviroment in the makefile when building ReleaseAMD64. ........ r55931 | thomas.heller | 2007-06-12 09:23:19 -0700 (Tue, 12 Jun 2007) | 5 lines Revert this change, since it breaks the win32 build: Add bufferoverflowU.lib to the libraries needed by _ssl (is this the right thing to do?). ........ r55934 | thomas.heller | 2007-06-12 10:28:31 -0700 (Tue, 12 Jun 2007) | 3 lines Specify the bufferoverflowU.lib to the makefile on the command line (for ReleaseAMD64 builds). ........ r55937 | thomas.heller | 2007-06-12 12:02:59 -0700 (Tue, 12 Jun 2007) | 3 lines Add bufferoverflowU.lib to PCBuild\_bsddb.vcproj. Build sqlite3.dll and bsddb. ........ r55938 | thomas.heller | 2007-06-12 12:56:12 -0700 (Tue, 12 Jun 2007) | 2 lines Don't rebuild Berkeley DB if not needed (this was committed by accident). ........ r55948 | martin.v.loewis | 2007-06-12 20:42:19 -0700 (Tue, 12 Jun 2007) | 3 lines Provide PY_LLONG_MAX on all systems having long long. Will backport to 2.5. ........ ................ r55959 | guido.van.rossum | 2007-06-13 09:22:41 -0700 (Wed, 13 Jun 2007) | 2 lines Fix a compilation warning. ................
1853 lines
51 KiB
C
1853 lines
51 KiB
C
/*
|
|
* New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
|
|
*
|
|
* Thanks go to Tim Peters and Michael Hudson for debugging.
|
|
*/
|
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
#include <Python.h>
|
|
#include "structmember.h"
|
|
#include "osdefs.h"
|
|
|
|
#define MAKE_IT_NONE(x) (x) = Py_None; Py_INCREF(Py_None);
|
|
|
|
/* NOTE: If the exception class hierarchy changes, don't forget to update
|
|
* Lib/test/exception_hierarchy.txt
|
|
*/
|
|
|
|
/*
|
|
* BaseException
|
|
*/
|
|
static PyObject *
|
|
BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyBaseExceptionObject *self;
|
|
|
|
self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
|
|
if (!self)
|
|
return NULL;
|
|
/* the dict is created on the fly in PyObject_GenericSetAttr */
|
|
self->dict = NULL;
|
|
|
|
self->args = PyTuple_New(0);
|
|
if (!self->args) {
|
|
Py_DECREF(self);
|
|
return NULL;
|
|
}
|
|
|
|
return (PyObject *)self;
|
|
}
|
|
|
|
static int
|
|
BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
if (!_PyArg_NoKeywords(self->ob_type->tp_name, kwds))
|
|
return -1;
|
|
|
|
Py_DECREF(self->args);
|
|
self->args = args;
|
|
Py_INCREF(self->args);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
BaseException_clear(PyBaseExceptionObject *self)
|
|
{
|
|
Py_CLEAR(self->dict);
|
|
Py_CLEAR(self->args);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
BaseException_dealloc(PyBaseExceptionObject *self)
|
|
{
|
|
_PyObject_GC_UNTRACK(self);
|
|
BaseException_clear(self);
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
static int
|
|
BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
|
|
{
|
|
Py_VISIT(self->dict);
|
|
Py_VISIT(self->args);
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
BaseException_str(PyBaseExceptionObject *self)
|
|
{
|
|
switch (PyTuple_GET_SIZE(self->args)) {
|
|
case 0:
|
|
return PyUnicode_FromString("");
|
|
case 1:
|
|
return PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
|
|
default:
|
|
return PyObject_Unicode(self->args);
|
|
}
|
|
}
|
|
|
|
static PyObject *
|
|
BaseException_repr(PyBaseExceptionObject *self)
|
|
{
|
|
char *name;
|
|
char *dot;
|
|
|
|
name = (char *)self->ob_type->tp_name;
|
|
dot = strrchr(name, '.');
|
|
if (dot != NULL) name = dot+1;
|
|
|
|
return PyUnicode_FromFormat("%s%R", name, self->args);
|
|
}
|
|
|
|
/* Pickling support */
|
|
static PyObject *
|
|
BaseException_reduce(PyBaseExceptionObject *self)
|
|
{
|
|
if (self->args && self->dict)
|
|
return PyTuple_Pack(3, self->ob_type, self->args, self->dict);
|
|
else
|
|
return PyTuple_Pack(2, self->ob_type, self->args);
|
|
}
|
|
|
|
/*
|
|
* Needed for backward compatibility, since exceptions used to store
|
|
* all their attributes in the __dict__. Code is taken from cPickle's
|
|
* load_build function.
|
|
*/
|
|
static PyObject *
|
|
BaseException_setstate(PyObject *self, PyObject *state)
|
|
{
|
|
PyObject *d_key, *d_value;
|
|
Py_ssize_t i = 0;
|
|
|
|
if (state != Py_None) {
|
|
if (!PyDict_Check(state)) {
|
|
PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
|
|
return NULL;
|
|
}
|
|
while (PyDict_Next(state, &i, &d_key, &d_value)) {
|
|
if (PyObject_SetAttr(self, d_key, d_value) < 0)
|
|
return NULL;
|
|
}
|
|
}
|
|
Py_RETURN_NONE;
|
|
}
|
|
|
|
|
|
static PyMethodDef BaseException_methods[] = {
|
|
{"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
|
|
{"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
|
|
{NULL, NULL, 0, NULL},
|
|
};
|
|
|
|
|
|
static PyObject *
|
|
BaseException_get_dict(PyBaseExceptionObject *self)
|
|
{
|
|
if (self->dict == NULL) {
|
|
self->dict = PyDict_New();
|
|
if (!self->dict)
|
|
return NULL;
|
|
}
|
|
Py_INCREF(self->dict);
|
|
return self->dict;
|
|
}
|
|
|
|
static int
|
|
BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
|
|
{
|
|
if (val == NULL) {
|
|
PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
|
|
return -1;
|
|
}
|
|
if (!PyDict_Check(val)) {
|
|
PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
|
|
return -1;
|
|
}
|
|
Py_CLEAR(self->dict);
|
|
Py_INCREF(val);
|
|
self->dict = val;
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
BaseException_get_args(PyBaseExceptionObject *self)
|
|
{
|
|
if (self->args == NULL) {
|
|
Py_INCREF(Py_None);
|
|
return Py_None;
|
|
}
|
|
Py_INCREF(self->args);
|
|
return self->args;
|
|
}
|
|
|
|
static int
|
|
BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
|
|
{
|
|
PyObject *seq;
|
|
if (val == NULL) {
|
|
PyErr_SetString(PyExc_TypeError, "args may not be deleted");
|
|
return -1;
|
|
}
|
|
seq = PySequence_Tuple(val);
|
|
if (!seq) return -1;
|
|
Py_CLEAR(self->args);
|
|
self->args = seq;
|
|
return 0;
|
|
}
|
|
|
|
|
|
static PyGetSetDef BaseException_getset[] = {
|
|
{"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
|
|
{"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
|
|
{NULL},
|
|
};
|
|
|
|
|
|
static PyTypeObject _PyExc_BaseException = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0, /*ob_size*/
|
|
"BaseException", /*tp_name*/
|
|
sizeof(PyBaseExceptionObject), /*tp_basicsize*/
|
|
0, /*tp_itemsize*/
|
|
(destructor)BaseException_dealloc, /*tp_dealloc*/
|
|
0, /*tp_print*/
|
|
0, /*tp_getattr*/
|
|
0, /*tp_setattr*/
|
|
0, /* tp_compare; */
|
|
(reprfunc)BaseException_repr, /*tp_repr*/
|
|
0, /*tp_as_number*/
|
|
0, /*tp_as_sequence*/
|
|
0, /*tp_as_mapping*/
|
|
0, /*tp_hash */
|
|
0, /*tp_call*/
|
|
(reprfunc)BaseException_str, /*tp_str*/
|
|
PyObject_GenericGetAttr, /*tp_getattro*/
|
|
PyObject_GenericSetAttr, /*tp_setattro*/
|
|
0, /*tp_as_buffer*/
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
|
|
Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
|
|
PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
|
|
(traverseproc)BaseException_traverse, /* tp_traverse */
|
|
(inquiry)BaseException_clear, /* tp_clear */
|
|
0, /* tp_richcompare */
|
|
0, /* tp_weaklistoffset */
|
|
0, /* tp_iter */
|
|
0, /* tp_iternext */
|
|
BaseException_methods, /* tp_methods */
|
|
0, /* tp_members */
|
|
BaseException_getset, /* tp_getset */
|
|
0, /* tp_base */
|
|
0, /* tp_dict */
|
|
0, /* tp_descr_get */
|
|
0, /* tp_descr_set */
|
|
offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
|
|
(initproc)BaseException_init, /* tp_init */
|
|
0, /* tp_alloc */
|
|
BaseException_new, /* tp_new */
|
|
};
|
|
/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
|
|
from the previous implmentation and also allowing Python objects to be used
|
|
in the API */
|
|
PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
|
|
|
|
/* note these macros omit the last semicolon so the macro invocation may
|
|
* include it and not look strange.
|
|
*/
|
|
#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
|
|
static PyTypeObject _PyExc_ ## EXCNAME = { \
|
|
PyObject_HEAD_INIT(NULL) \
|
|
0, \
|
|
# EXCNAME, \
|
|
sizeof(PyBaseExceptionObject), \
|
|
0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
|
|
0, 0, 0, 0, 0, 0, 0, \
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
|
|
PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
|
|
(inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
|
|
0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
|
|
(initproc)BaseException_init, 0, BaseException_new,\
|
|
}; \
|
|
PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
|
|
|
|
#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
|
|
static PyTypeObject _PyExc_ ## EXCNAME = { \
|
|
PyObject_HEAD_INIT(NULL) \
|
|
0, \
|
|
# EXCNAME, \
|
|
sizeof(Py ## EXCSTORE ## Object), \
|
|
0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
|
|
0, 0, 0, 0, 0, \
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
|
|
PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
|
|
(inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
|
|
0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
|
|
(initproc)EXCSTORE ## _init, 0, BaseException_new,\
|
|
}; \
|
|
PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
|
|
|
|
#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \
|
|
static PyTypeObject _PyExc_ ## EXCNAME = { \
|
|
PyObject_HEAD_INIT(NULL) \
|
|
0, \
|
|
# EXCNAME, \
|
|
sizeof(Py ## EXCSTORE ## Object), 0, \
|
|
(destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
|
|
(reprfunc)EXCSTR, 0, 0, 0, \
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
|
|
PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
|
|
(inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
|
|
EXCMEMBERS, 0, &_ ## EXCBASE, \
|
|
0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
|
|
(initproc)EXCSTORE ## _init, 0, BaseException_new,\
|
|
}; \
|
|
PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
|
|
|
|
|
|
/*
|
|
* Exception extends BaseException
|
|
*/
|
|
SimpleExtendsException(PyExc_BaseException, Exception,
|
|
"Common base class for all non-exit exceptions.");
|
|
|
|
|
|
/*
|
|
* TypeError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, TypeError,
|
|
"Inappropriate argument type.");
|
|
|
|
|
|
/*
|
|
* StopIteration extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, StopIteration,
|
|
"Signal the end from iterator.__next__().");
|
|
|
|
|
|
/*
|
|
* GeneratorExit extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, GeneratorExit,
|
|
"Request that a generator exit.");
|
|
|
|
|
|
/*
|
|
* SystemExit extends BaseException
|
|
*/
|
|
|
|
static int
|
|
SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
Py_ssize_t size = PyTuple_GET_SIZE(args);
|
|
|
|
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
|
|
return -1;
|
|
|
|
if (size == 0)
|
|
return 0;
|
|
Py_CLEAR(self->code);
|
|
if (size == 1)
|
|
self->code = PyTuple_GET_ITEM(args, 0);
|
|
else if (size > 1)
|
|
self->code = args;
|
|
Py_INCREF(self->code);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
SystemExit_clear(PySystemExitObject *self)
|
|
{
|
|
Py_CLEAR(self->code);
|
|
return BaseException_clear((PyBaseExceptionObject *)self);
|
|
}
|
|
|
|
static void
|
|
SystemExit_dealloc(PySystemExitObject *self)
|
|
{
|
|
_PyObject_GC_UNTRACK(self);
|
|
SystemExit_clear(self);
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
static int
|
|
SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
|
|
{
|
|
Py_VISIT(self->code);
|
|
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
|
|
}
|
|
|
|
static PyMemberDef SystemExit_members[] = {
|
|
{"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
|
|
PyDoc_STR("exception code")},
|
|
{NULL} /* Sentinel */
|
|
};
|
|
|
|
ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
|
|
SystemExit_dealloc, 0, SystemExit_members, 0,
|
|
"Request to exit from the interpreter.");
|
|
|
|
/*
|
|
* KeyboardInterrupt extends BaseException
|
|
*/
|
|
SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
|
|
"Program interrupted by user.");
|
|
|
|
|
|
/*
|
|
* ImportError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, ImportError,
|
|
"Import can't find module, or can't find name in module.");
|
|
|
|
|
|
/*
|
|
* EnvironmentError extends Exception
|
|
*/
|
|
|
|
/* Where a function has a single filename, such as open() or some
|
|
* of the os module functions, PyErr_SetFromErrnoWithFilename() is
|
|
* called, giving a third argument which is the filename. But, so
|
|
* that old code using in-place unpacking doesn't break, e.g.:
|
|
*
|
|
* except IOError, (errno, strerror):
|
|
*
|
|
* we hack args so that it only contains two items. This also
|
|
* means we need our own __str__() which prints out the filename
|
|
* when it was supplied.
|
|
*/
|
|
static int
|
|
EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
|
|
PyObject *kwds)
|
|
{
|
|
PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
|
|
PyObject *subslice = NULL;
|
|
|
|
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
|
|
return -1;
|
|
|
|
if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {
|
|
return 0;
|
|
}
|
|
|
|
if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,
|
|
&myerrno, &strerror, &filename)) {
|
|
return -1;
|
|
}
|
|
Py_CLEAR(self->myerrno); /* replacing */
|
|
self->myerrno = myerrno;
|
|
Py_INCREF(self->myerrno);
|
|
|
|
Py_CLEAR(self->strerror); /* replacing */
|
|
self->strerror = strerror;
|
|
Py_INCREF(self->strerror);
|
|
|
|
/* self->filename will remain Py_None otherwise */
|
|
if (filename != NULL) {
|
|
Py_CLEAR(self->filename); /* replacing */
|
|
self->filename = filename;
|
|
Py_INCREF(self->filename);
|
|
|
|
subslice = PyTuple_GetSlice(args, 0, 2);
|
|
if (!subslice)
|
|
return -1;
|
|
|
|
Py_DECREF(self->args); /* replacing args */
|
|
self->args = subslice;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
EnvironmentError_clear(PyEnvironmentErrorObject *self)
|
|
{
|
|
Py_CLEAR(self->myerrno);
|
|
Py_CLEAR(self->strerror);
|
|
Py_CLEAR(self->filename);
|
|
return BaseException_clear((PyBaseExceptionObject *)self);
|
|
}
|
|
|
|
static void
|
|
EnvironmentError_dealloc(PyEnvironmentErrorObject *self)
|
|
{
|
|
_PyObject_GC_UNTRACK(self);
|
|
EnvironmentError_clear(self);
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
static int
|
|
EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,
|
|
void *arg)
|
|
{
|
|
Py_VISIT(self->myerrno);
|
|
Py_VISIT(self->strerror);
|
|
Py_VISIT(self->filename);
|
|
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
|
|
}
|
|
|
|
static PyObject *
|
|
EnvironmentError_str(PyEnvironmentErrorObject *self)
|
|
{
|
|
if (self->filename)
|
|
return PyUnicode_FromFormat("[Errno %S] %S: %R",
|
|
self->myerrno ? self->myerrno: Py_None,
|
|
self->strerror ? self->strerror: Py_None,
|
|
self->filename);
|
|
else if (self->myerrno && self->strerror)
|
|
return PyUnicode_FromFormat("[Errno %S] %S",
|
|
self->myerrno ? self->myerrno: Py_None,
|
|
self->strerror ? self->strerror: Py_None);
|
|
else
|
|
return BaseException_str((PyBaseExceptionObject *)self);
|
|
}
|
|
|
|
static PyMemberDef EnvironmentError_members[] = {
|
|
{"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
|
|
PyDoc_STR("exception errno")},
|
|
{"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
|
|
PyDoc_STR("exception strerror")},
|
|
{"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
|
|
PyDoc_STR("exception filename")},
|
|
{NULL} /* Sentinel */
|
|
};
|
|
|
|
|
|
static PyObject *
|
|
EnvironmentError_reduce(PyEnvironmentErrorObject *self)
|
|
{
|
|
PyObject *args = self->args;
|
|
PyObject *res = NULL, *tmp;
|
|
|
|
/* self->args is only the first two real arguments if there was a
|
|
* file name given to EnvironmentError. */
|
|
if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
|
|
args = PyTuple_New(3);
|
|
if (!args) return NULL;
|
|
|
|
tmp = PyTuple_GET_ITEM(self->args, 0);
|
|
Py_INCREF(tmp);
|
|
PyTuple_SET_ITEM(args, 0, tmp);
|
|
|
|
tmp = PyTuple_GET_ITEM(self->args, 1);
|
|
Py_INCREF(tmp);
|
|
PyTuple_SET_ITEM(args, 1, tmp);
|
|
|
|
Py_INCREF(self->filename);
|
|
PyTuple_SET_ITEM(args, 2, self->filename);
|
|
} else
|
|
Py_INCREF(args);
|
|
|
|
if (self->dict)
|
|
res = PyTuple_Pack(3, self->ob_type, args, self->dict);
|
|
else
|
|
res = PyTuple_Pack(2, self->ob_type, args);
|
|
Py_DECREF(args);
|
|
return res;
|
|
}
|
|
|
|
|
|
static PyMethodDef EnvironmentError_methods[] = {
|
|
{"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},
|
|
{NULL}
|
|
};
|
|
|
|
ComplexExtendsException(PyExc_Exception, EnvironmentError,
|
|
EnvironmentError, EnvironmentError_dealloc,
|
|
EnvironmentError_methods, EnvironmentError_members,
|
|
EnvironmentError_str,
|
|
"Base class for I/O related errors.");
|
|
|
|
|
|
/*
|
|
* IOError extends EnvironmentError
|
|
*/
|
|
MiddlingExtendsException(PyExc_EnvironmentError, IOError,
|
|
EnvironmentError, "I/O operation failed.");
|
|
|
|
|
|
/*
|
|
* OSError extends EnvironmentError
|
|
*/
|
|
MiddlingExtendsException(PyExc_EnvironmentError, OSError,
|
|
EnvironmentError, "OS system call failed.");
|
|
|
|
|
|
/*
|
|
* WindowsError extends OSError
|
|
*/
|
|
#ifdef MS_WINDOWS
|
|
#include "errmap.h"
|
|
|
|
static int
|
|
WindowsError_clear(PyWindowsErrorObject *self)
|
|
{
|
|
Py_CLEAR(self->myerrno);
|
|
Py_CLEAR(self->strerror);
|
|
Py_CLEAR(self->filename);
|
|
Py_CLEAR(self->winerror);
|
|
return BaseException_clear((PyBaseExceptionObject *)self);
|
|
}
|
|
|
|
static void
|
|
WindowsError_dealloc(PyWindowsErrorObject *self)
|
|
{
|
|
_PyObject_GC_UNTRACK(self);
|
|
WindowsError_clear(self);
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
static int
|
|
WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
|
|
{
|
|
Py_VISIT(self->myerrno);
|
|
Py_VISIT(self->strerror);
|
|
Py_VISIT(self->filename);
|
|
Py_VISIT(self->winerror);
|
|
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
|
|
}
|
|
|
|
static int
|
|
WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyObject *o_errcode = NULL;
|
|
long errcode;
|
|
long posix_errno;
|
|
|
|
if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
|
|
== -1)
|
|
return -1;
|
|
|
|
if (self->myerrno == NULL)
|
|
return 0;
|
|
|
|
/* Set errno to the POSIX errno, and winerror to the Win32
|
|
error code. */
|
|
errcode = PyInt_AsLong(self->myerrno);
|
|
if (errcode == -1 && PyErr_Occurred())
|
|
return -1;
|
|
posix_errno = winerror_to_errno(errcode);
|
|
|
|
Py_CLEAR(self->winerror);
|
|
self->winerror = self->myerrno;
|
|
|
|
o_errcode = PyInt_FromLong(posix_errno);
|
|
if (!o_errcode)
|
|
return -1;
|
|
|
|
self->myerrno = o_errcode;
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
WindowsError_str(PyWindowsErrorObject *self)
|
|
{
|
|
if (self->filename)
|
|
return PyUnicode_FromFormat("[Error %S] %S: %R",
|
|
self->winerror ? self->winerror: Py_None,
|
|
self->strerror ? self->strerror: Py_None,
|
|
self->filename);
|
|
else if (self->winerror && self->strerror)
|
|
return PyUnicode_FromFormat("[Error %S] %S",
|
|
self->winerror ? self->winerror: Py_None,
|
|
self->strerror ? self->strerror: Py_None);
|
|
else
|
|
return EnvironmentError_str((PyEnvironmentErrorObject *)self);
|
|
}
|
|
|
|
static PyMemberDef WindowsError_members[] = {
|
|
{"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
|
|
PyDoc_STR("POSIX exception code")},
|
|
{"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
|
|
PyDoc_STR("exception strerror")},
|
|
{"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
|
|
PyDoc_STR("exception filename")},
|
|
{"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
|
|
PyDoc_STR("Win32 exception code")},
|
|
{NULL} /* Sentinel */
|
|
};
|
|
|
|
ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
|
|
WindowsError_dealloc, 0, WindowsError_members,
|
|
WindowsError_str, "MS-Windows OS system call failed.");
|
|
|
|
#endif /* MS_WINDOWS */
|
|
|
|
|
|
/*
|
|
* VMSError extends OSError (I think)
|
|
*/
|
|
#ifdef __VMS
|
|
MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
|
|
"OpenVMS OS system call failed.");
|
|
#endif
|
|
|
|
|
|
/*
|
|
* EOFError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, EOFError,
|
|
"Read beyond end of file.");
|
|
|
|
|
|
/*
|
|
* RuntimeError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, RuntimeError,
|
|
"Unspecified run-time error.");
|
|
|
|
|
|
/*
|
|
* NotImplementedError extends RuntimeError
|
|
*/
|
|
SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
|
|
"Method or function hasn't been implemented yet.");
|
|
|
|
/*
|
|
* NameError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, NameError,
|
|
"Name not found globally.");
|
|
|
|
/*
|
|
* UnboundLocalError extends NameError
|
|
*/
|
|
SimpleExtendsException(PyExc_NameError, UnboundLocalError,
|
|
"Local name referenced but not bound to a value.");
|
|
|
|
/*
|
|
* AttributeError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, AttributeError,
|
|
"Attribute not found.");
|
|
|
|
|
|
/*
|
|
* SyntaxError extends Exception
|
|
*/
|
|
|
|
static int
|
|
SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
PyObject *info = NULL;
|
|
Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
|
|
|
|
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
|
|
return -1;
|
|
|
|
if (lenargs >= 1) {
|
|
Py_CLEAR(self->msg);
|
|
self->msg = PyTuple_GET_ITEM(args, 0);
|
|
Py_INCREF(self->msg);
|
|
}
|
|
if (lenargs == 2) {
|
|
info = PyTuple_GET_ITEM(args, 1);
|
|
info = PySequence_Tuple(info);
|
|
if (!info) return -1;
|
|
|
|
if (PyTuple_GET_SIZE(info) != 4) {
|
|
/* not a very good error message, but it's what Python 2.4 gives */
|
|
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
|
|
Py_DECREF(info);
|
|
return -1;
|
|
}
|
|
|
|
Py_CLEAR(self->filename);
|
|
self->filename = PyTuple_GET_ITEM(info, 0);
|
|
Py_INCREF(self->filename);
|
|
|
|
Py_CLEAR(self->lineno);
|
|
self->lineno = PyTuple_GET_ITEM(info, 1);
|
|
Py_INCREF(self->lineno);
|
|
|
|
Py_CLEAR(self->offset);
|
|
self->offset = PyTuple_GET_ITEM(info, 2);
|
|
Py_INCREF(self->offset);
|
|
|
|
Py_CLEAR(self->text);
|
|
self->text = PyTuple_GET_ITEM(info, 3);
|
|
Py_INCREF(self->text);
|
|
|
|
Py_DECREF(info);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
SyntaxError_clear(PySyntaxErrorObject *self)
|
|
{
|
|
Py_CLEAR(self->msg);
|
|
Py_CLEAR(self->filename);
|
|
Py_CLEAR(self->lineno);
|
|
Py_CLEAR(self->offset);
|
|
Py_CLEAR(self->text);
|
|
Py_CLEAR(self->print_file_and_line);
|
|
return BaseException_clear((PyBaseExceptionObject *)self);
|
|
}
|
|
|
|
static void
|
|
SyntaxError_dealloc(PySyntaxErrorObject *self)
|
|
{
|
|
_PyObject_GC_UNTRACK(self);
|
|
SyntaxError_clear(self);
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
static int
|
|
SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
|
|
{
|
|
Py_VISIT(self->msg);
|
|
Py_VISIT(self->filename);
|
|
Py_VISIT(self->lineno);
|
|
Py_VISIT(self->offset);
|
|
Py_VISIT(self->text);
|
|
Py_VISIT(self->print_file_and_line);
|
|
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
|
|
}
|
|
|
|
/* This is called "my_basename" instead of just "basename" to avoid name
|
|
conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
|
|
defined, and Python does define that. */
|
|
static char *
|
|
my_basename(char *name)
|
|
{
|
|
char *cp = name;
|
|
char *result = name;
|
|
|
|
if (name == NULL)
|
|
return "???";
|
|
while (*cp != '\0') {
|
|
if (*cp == SEP)
|
|
result = cp + 1;
|
|
++cp;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
SyntaxError_str(PySyntaxErrorObject *self)
|
|
{
|
|
int have_filename = 0;
|
|
int have_lineno = 0;
|
|
|
|
/* XXX -- do all the additional formatting with filename and
|
|
lineno here */
|
|
|
|
have_filename = (self->filename != NULL) &&
|
|
PyString_Check(self->filename);
|
|
have_lineno = (self->lineno != NULL) && PyInt_CheckExact(self->lineno);
|
|
|
|
if (!have_filename && !have_lineno)
|
|
return PyObject_Unicode(self->msg ? self->msg : Py_None);
|
|
|
|
if (have_filename && have_lineno)
|
|
return PyUnicode_FromFormat("%S (%s, line %ld)",
|
|
self->msg ? self->msg : Py_None,
|
|
my_basename(PyString_AS_STRING(self->filename)),
|
|
PyInt_AsLong(self->lineno));
|
|
else if (have_filename)
|
|
return PyUnicode_FromFormat("%S (%s)",
|
|
self->msg ? self->msg : Py_None,
|
|
my_basename(PyString_AS_STRING(self->filename)));
|
|
else /* only have_lineno */
|
|
return PyUnicode_FromFormat("%S (line %ld)",
|
|
self->msg ? self->msg : Py_None,
|
|
PyInt_AsLong(self->lineno));
|
|
}
|
|
|
|
static PyMemberDef SyntaxError_members[] = {
|
|
{"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
|
|
PyDoc_STR("exception msg")},
|
|
{"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
|
|
PyDoc_STR("exception filename")},
|
|
{"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
|
|
PyDoc_STR("exception lineno")},
|
|
{"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
|
|
PyDoc_STR("exception offset")},
|
|
{"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
|
|
PyDoc_STR("exception text")},
|
|
{"print_file_and_line", T_OBJECT,
|
|
offsetof(PySyntaxErrorObject, print_file_and_line), 0,
|
|
PyDoc_STR("exception print_file_and_line")},
|
|
{NULL} /* Sentinel */
|
|
};
|
|
|
|
ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
|
|
SyntaxError_dealloc, 0, SyntaxError_members,
|
|
SyntaxError_str, "Invalid syntax.");
|
|
|
|
|
|
/*
|
|
* IndentationError extends SyntaxError
|
|
*/
|
|
MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
|
|
"Improper indentation.");
|
|
|
|
|
|
/*
|
|
* TabError extends IndentationError
|
|
*/
|
|
MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
|
|
"Improper mixture of spaces and tabs.");
|
|
|
|
|
|
/*
|
|
* LookupError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, LookupError,
|
|
"Base class for lookup errors.");
|
|
|
|
|
|
/*
|
|
* IndexError extends LookupError
|
|
*/
|
|
SimpleExtendsException(PyExc_LookupError, IndexError,
|
|
"Sequence index out of range.");
|
|
|
|
|
|
/*
|
|
* KeyError extends LookupError
|
|
*/
|
|
static PyObject *
|
|
KeyError_str(PyBaseExceptionObject *self)
|
|
{
|
|
/* If args is a tuple of exactly one item, apply repr to args[0].
|
|
This is done so that e.g. the exception raised by {}[''] prints
|
|
KeyError: ''
|
|
rather than the confusing
|
|
KeyError
|
|
alone. The downside is that if KeyError is raised with an explanatory
|
|
string, that string will be displayed in quotes. Too bad.
|
|
If args is anything else, use the default BaseException__str__().
|
|
*/
|
|
if (PyTuple_GET_SIZE(self->args) == 1) {
|
|
return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
|
|
}
|
|
return BaseException_str(self);
|
|
}
|
|
|
|
ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
|
|
0, 0, 0, KeyError_str, "Mapping key not found.");
|
|
|
|
|
|
/*
|
|
* ValueError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, ValueError,
|
|
"Inappropriate argument value (of correct type).");
|
|
|
|
/*
|
|
* UnicodeError extends ValueError
|
|
*/
|
|
|
|
SimpleExtendsException(PyExc_ValueError, UnicodeError,
|
|
"Unicode related error.");
|
|
|
|
static int
|
|
get_int(PyObject *attr, Py_ssize_t *value, const char *name)
|
|
{
|
|
if (!attr) {
|
|
PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
|
|
return -1;
|
|
}
|
|
|
|
if (PyLong_Check(attr)) {
|
|
*value = PyLong_AsSsize_t(attr);
|
|
if (*value == -1 && PyErr_Occurred())
|
|
return -1;
|
|
} else {
|
|
PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
set_ssize_t(PyObject **attr, Py_ssize_t value)
|
|
{
|
|
PyObject *obj = PyInt_FromSsize_t(value);
|
|
if (!obj)
|
|
return -1;
|
|
Py_CLEAR(*attr);
|
|
*attr = obj;
|
|
return 0;
|
|
}
|
|
|
|
static PyObject *
|
|
get_bytes(PyObject *attr, const char *name)
|
|
{
|
|
if (!attr) {
|
|
PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
|
|
return NULL;
|
|
}
|
|
|
|
if (!PyBytes_Check(attr)) {
|
|
PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
|
|
return NULL;
|
|
}
|
|
Py_INCREF(attr);
|
|
return attr;
|
|
}
|
|
|
|
static PyObject *
|
|
get_unicode(PyObject *attr, const char *name)
|
|
{
|
|
if (!attr) {
|
|
PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
|
|
return NULL;
|
|
}
|
|
|
|
if (!PyUnicode_Check(attr)) {
|
|
PyErr_Format(PyExc_TypeError,
|
|
"%.200s attribute must be unicode", name);
|
|
return NULL;
|
|
}
|
|
Py_INCREF(attr);
|
|
return attr;
|
|
}
|
|
|
|
static int
|
|
set_unicodefromstring(PyObject **attr, const char *value)
|
|
{
|
|
PyObject *obj = PyUnicode_FromString(value);
|
|
if (!obj)
|
|
return -1;
|
|
Py_CLEAR(*attr);
|
|
*attr = obj;
|
|
return 0;
|
|
}
|
|
|
|
PyObject *
|
|
PyUnicodeEncodeError_GetEncoding(PyObject *exc)
|
|
{
|
|
return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
|
|
}
|
|
|
|
PyObject *
|
|
PyUnicodeDecodeError_GetEncoding(PyObject *exc)
|
|
{
|
|
return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
|
|
}
|
|
|
|
PyObject *
|
|
PyUnicodeEncodeError_GetObject(PyObject *exc)
|
|
{
|
|
return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
|
|
}
|
|
|
|
PyObject *
|
|
PyUnicodeDecodeError_GetObject(PyObject *exc)
|
|
{
|
|
return get_bytes(((PyUnicodeErrorObject *)exc)->object, "object");
|
|
}
|
|
|
|
PyObject *
|
|
PyUnicodeTranslateError_GetObject(PyObject *exc)
|
|
{
|
|
return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
|
|
}
|
|
|
|
int
|
|
PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
|
|
{
|
|
if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
|
|
Py_ssize_t size;
|
|
PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
|
|
"object");
|
|
if (!obj) return -1;
|
|
size = PyUnicode_GET_SIZE(obj);
|
|
if (*start<0)
|
|
*start = 0; /*XXX check for values <0*/
|
|
if (*start>=size)
|
|
*start = size-1;
|
|
Py_DECREF(obj);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
|
|
{
|
|
if (!get_int(((PyUnicodeErrorObject *)exc)->start, start, "start")) {
|
|
Py_ssize_t size;
|
|
PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object,
|
|
"object");
|
|
if (!obj) return -1;
|
|
size = PyBytes_GET_SIZE(obj);
|
|
if (*start<0)
|
|
*start = 0;
|
|
if (*start>=size)
|
|
*start = size-1;
|
|
Py_DECREF(obj);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
|
|
{
|
|
return PyUnicodeEncodeError_GetStart(exc, start);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
|
|
{
|
|
return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
|
|
{
|
|
return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
|
|
{
|
|
return set_ssize_t(&((PyUnicodeErrorObject *)exc)->start, start);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
|
|
{
|
|
if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
|
|
Py_ssize_t size;
|
|
PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
|
|
"object");
|
|
if (!obj) return -1;
|
|
size = PyUnicode_GET_SIZE(obj);
|
|
if (*end<1)
|
|
*end = 1;
|
|
if (*end>size)
|
|
*end = size;
|
|
Py_DECREF(obj);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
|
|
{
|
|
if (!get_int(((PyUnicodeErrorObject *)exc)->end, end, "end")) {
|
|
Py_ssize_t size;
|
|
PyObject *obj = get_bytes(((PyUnicodeErrorObject *)exc)->object,
|
|
"object");
|
|
if (!obj) return -1;
|
|
size = PyBytes_GET_SIZE(obj);
|
|
if (*end<1)
|
|
*end = 1;
|
|
if (*end>size)
|
|
*end = size;
|
|
Py_DECREF(obj);
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
|
|
{
|
|
return PyUnicodeEncodeError_GetEnd(exc, start);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
|
|
{
|
|
return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
|
|
{
|
|
return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
|
|
{
|
|
return set_ssize_t(&((PyUnicodeErrorObject *)exc)->end, end);
|
|
}
|
|
|
|
PyObject *
|
|
PyUnicodeEncodeError_GetReason(PyObject *exc)
|
|
{
|
|
return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
|
|
}
|
|
|
|
|
|
PyObject *
|
|
PyUnicodeDecodeError_GetReason(PyObject *exc)
|
|
{
|
|
return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
|
|
}
|
|
|
|
|
|
PyObject *
|
|
PyUnicodeTranslateError_GetReason(PyObject *exc)
|
|
{
|
|
return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
|
|
{
|
|
return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
|
|
reason);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
|
|
{
|
|
return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
|
|
reason);
|
|
}
|
|
|
|
|
|
int
|
|
PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
|
|
{
|
|
return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
|
|
reason);
|
|
}
|
|
|
|
|
|
static int
|
|
UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,
|
|
PyTypeObject *objecttype)
|
|
{
|
|
Py_CLEAR(self->encoding);
|
|
Py_CLEAR(self->object);
|
|
Py_CLEAR(self->start);
|
|
Py_CLEAR(self->end);
|
|
Py_CLEAR(self->reason);
|
|
|
|
if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
|
|
&PyUnicode_Type, &self->encoding,
|
|
objecttype, &self->object,
|
|
&PyLong_Type, &self->start,
|
|
&PyLong_Type, &self->end,
|
|
&PyUnicode_Type, &self->reason)) {
|
|
self->encoding = self->object = self->start = self->end =
|
|
self->reason = NULL;
|
|
return -1;
|
|
}
|
|
|
|
Py_INCREF(self->encoding);
|
|
Py_INCREF(self->object);
|
|
Py_INCREF(self->start);
|
|
Py_INCREF(self->end);
|
|
Py_INCREF(self->reason);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
UnicodeError_clear(PyUnicodeErrorObject *self)
|
|
{
|
|
Py_CLEAR(self->encoding);
|
|
Py_CLEAR(self->object);
|
|
Py_CLEAR(self->start);
|
|
Py_CLEAR(self->end);
|
|
Py_CLEAR(self->reason);
|
|
return BaseException_clear((PyBaseExceptionObject *)self);
|
|
}
|
|
|
|
static void
|
|
UnicodeError_dealloc(PyUnicodeErrorObject *self)
|
|
{
|
|
_PyObject_GC_UNTRACK(self);
|
|
UnicodeError_clear(self);
|
|
self->ob_type->tp_free((PyObject *)self);
|
|
}
|
|
|
|
static int
|
|
UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
|
|
{
|
|
Py_VISIT(self->encoding);
|
|
Py_VISIT(self->object);
|
|
Py_VISIT(self->start);
|
|
Py_VISIT(self->end);
|
|
Py_VISIT(self->reason);
|
|
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
|
|
}
|
|
|
|
static PyMemberDef UnicodeError_members[] = {
|
|
{"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
|
|
PyDoc_STR("exception encoding")},
|
|
{"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
|
|
PyDoc_STR("exception object")},
|
|
{"start", T_OBJECT, offsetof(PyUnicodeErrorObject, start), 0,
|
|
PyDoc_STR("exception start")},
|
|
{"end", T_OBJECT, offsetof(PyUnicodeErrorObject, end), 0,
|
|
PyDoc_STR("exception end")},
|
|
{"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
|
|
PyDoc_STR("exception reason")},
|
|
{NULL} /* Sentinel */
|
|
};
|
|
|
|
|
|
/*
|
|
* UnicodeEncodeError extends UnicodeError
|
|
*/
|
|
|
|
static int
|
|
UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
|
|
return -1;
|
|
return UnicodeError_init((PyUnicodeErrorObject *)self, args,
|
|
kwds, &PyUnicode_Type);
|
|
}
|
|
|
|
static PyObject *
|
|
UnicodeEncodeError_str(PyObject *self)
|
|
{
|
|
Py_ssize_t start;
|
|
Py_ssize_t end;
|
|
|
|
if (PyUnicodeEncodeError_GetStart(self, &start))
|
|
return NULL;
|
|
|
|
if (PyUnicodeEncodeError_GetEnd(self, &end))
|
|
return NULL;
|
|
|
|
if (end==start+1) {
|
|
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
|
|
const char *fmt;
|
|
if (badchar <= 0xff)
|
|
fmt = "'%U' codec can't encode character u'\\x%02x' in position %zd: %U";
|
|
else if (badchar <= 0xffff)
|
|
fmt = "'%U' codec can't encode character u'\\u%04x' in position %zd: %U";
|
|
else
|
|
fmt = "'%U' codec can't encode character u'\\U%08x' in position %zd: %U";
|
|
return PyUnicode_FromFormat(
|
|
fmt,
|
|
((PyUnicodeErrorObject *)self)->encoding,
|
|
badchar,
|
|
start,
|
|
((PyUnicodeErrorObject *)self)->reason
|
|
);
|
|
}
|
|
return PyUnicode_FromFormat(
|
|
"'%U' codec can't encode characters in position %zd-%zd: %U",
|
|
((PyUnicodeErrorObject *)self)->encoding,
|
|
start,
|
|
(end-1),
|
|
((PyUnicodeErrorObject *)self)->reason
|
|
);
|
|
}
|
|
|
|
static PyTypeObject _PyExc_UnicodeEncodeError = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0,
|
|
"UnicodeEncodeError",
|
|
sizeof(PyUnicodeErrorObject), 0,
|
|
(destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
(reprfunc)UnicodeEncodeError_str, 0, 0, 0,
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
|
|
(inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
|
|
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
|
|
(initproc)UnicodeEncodeError_init, 0, BaseException_new,
|
|
};
|
|
PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
|
|
|
|
PyObject *
|
|
PyUnicodeEncodeError_Create(
|
|
const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
|
|
Py_ssize_t start, Py_ssize_t end, const char *reason)
|
|
{
|
|
return PyObject_CallFunction(PyExc_UnicodeEncodeError, "Uu#nnU",
|
|
encoding, object, length, start, end, reason);
|
|
}
|
|
|
|
|
|
/*
|
|
* UnicodeDecodeError extends UnicodeError
|
|
*/
|
|
|
|
static int
|
|
UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
|
|
{
|
|
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
|
|
return -1;
|
|
return UnicodeError_init((PyUnicodeErrorObject *)self, args,
|
|
kwds, &PyBytes_Type);
|
|
}
|
|
|
|
static PyObject *
|
|
UnicodeDecodeError_str(PyObject *self)
|
|
{
|
|
Py_ssize_t start = 0;
|
|
Py_ssize_t end = 0;
|
|
|
|
if (PyUnicodeDecodeError_GetStart(self, &start))
|
|
return NULL;
|
|
|
|
if (PyUnicodeDecodeError_GetEnd(self, &end))
|
|
return NULL;
|
|
|
|
if (end==start+1) {
|
|
int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start]&0xff);
|
|
return PyUnicode_FromFormat(
|
|
"'%U' codec can't decode byte 0x%02x in position %zd: %U",
|
|
((PyUnicodeErrorObject *)self)->encoding,
|
|
byte,
|
|
start,
|
|
((PyUnicodeErrorObject *)self)->reason
|
|
);
|
|
}
|
|
return PyUnicode_FromFormat(
|
|
"'%U' codec can't decode bytes in position %zd-%zd: %U",
|
|
((PyUnicodeErrorObject *)self)->encoding,
|
|
start,
|
|
(end-1),
|
|
((PyUnicodeErrorObject *)self)->reason
|
|
);
|
|
}
|
|
|
|
static PyTypeObject _PyExc_UnicodeDecodeError = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0,
|
|
"UnicodeDecodeError",
|
|
sizeof(PyUnicodeErrorObject), 0,
|
|
(destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
(reprfunc)UnicodeDecodeError_str, 0, 0, 0,
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
|
|
(inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
|
|
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
|
|
(initproc)UnicodeDecodeError_init, 0, BaseException_new,
|
|
};
|
|
PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
|
|
|
|
PyObject *
|
|
PyUnicodeDecodeError_Create(
|
|
const char *encoding, const char *object, Py_ssize_t length,
|
|
Py_ssize_t start, Py_ssize_t end, const char *reason)
|
|
{
|
|
assert(length < INT_MAX);
|
|
assert(start < INT_MAX);
|
|
assert(end < INT_MAX);
|
|
return PyObject_CallFunction(PyExc_UnicodeDecodeError, "Uy#nnU",
|
|
encoding, object, length, start, end, reason);
|
|
}
|
|
|
|
|
|
/*
|
|
* UnicodeTranslateError extends UnicodeError
|
|
*/
|
|
|
|
static int
|
|
UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
|
|
PyObject *kwds)
|
|
{
|
|
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
|
|
return -1;
|
|
|
|
Py_CLEAR(self->object);
|
|
Py_CLEAR(self->start);
|
|
Py_CLEAR(self->end);
|
|
Py_CLEAR(self->reason);
|
|
|
|
if (!PyArg_ParseTuple(args, "O!O!O!O!",
|
|
&PyUnicode_Type, &self->object,
|
|
&PyLong_Type, &self->start,
|
|
&PyLong_Type, &self->end,
|
|
&PyUnicode_Type, &self->reason)) {
|
|
self->object = self->start = self->end = self->reason = NULL;
|
|
return -1;
|
|
}
|
|
|
|
Py_INCREF(self->object);
|
|
Py_INCREF(self->start);
|
|
Py_INCREF(self->end);
|
|
Py_INCREF(self->reason);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
static PyObject *
|
|
UnicodeTranslateError_str(PyObject *self)
|
|
{
|
|
Py_ssize_t start;
|
|
Py_ssize_t end;
|
|
|
|
if (PyUnicodeTranslateError_GetStart(self, &start))
|
|
return NULL;
|
|
|
|
if (PyUnicodeTranslateError_GetEnd(self, &end))
|
|
return NULL;
|
|
|
|
if (end==start+1) {
|
|
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
|
|
const char *fmt;
|
|
if (badchar <= 0xff)
|
|
fmt = "can't translate character u'\\x%02x' in position %zd: %U";
|
|
else if (badchar <= 0xffff)
|
|
fmt = "can't translate character u'\\u%04x' in position %zd: %U";
|
|
else
|
|
fmt = "can't translate character u'\\U%08x' in position %zd: %U";
|
|
return PyUnicode_FromFormat(
|
|
fmt,
|
|
badchar,
|
|
start,
|
|
((PyUnicodeErrorObject *)self)->reason
|
|
);
|
|
}
|
|
return PyUnicode_FromFormat(
|
|
"can't translate characters in position %zd-%zd: %U",
|
|
start,
|
|
(end-1),
|
|
((PyUnicodeErrorObject *)self)->reason
|
|
);
|
|
}
|
|
|
|
static PyTypeObject _PyExc_UnicodeTranslateError = {
|
|
PyObject_HEAD_INIT(NULL)
|
|
0,
|
|
"UnicodeTranslateError",
|
|
sizeof(PyUnicodeErrorObject), 0,
|
|
(destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
(reprfunc)UnicodeTranslateError_str, 0, 0, 0,
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
|
|
(inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
|
|
0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
|
|
(initproc)UnicodeTranslateError_init, 0, BaseException_new,
|
|
};
|
|
PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
|
|
|
|
PyObject *
|
|
PyUnicodeTranslateError_Create(
|
|
const Py_UNICODE *object, Py_ssize_t length,
|
|
Py_ssize_t start, Py_ssize_t end, const char *reason)
|
|
{
|
|
return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
|
|
object, length, start, end, reason);
|
|
}
|
|
|
|
|
|
/*
|
|
* AssertionError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, AssertionError,
|
|
"Assertion failed.");
|
|
|
|
|
|
/*
|
|
* ArithmeticError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, ArithmeticError,
|
|
"Base class for arithmetic errors.");
|
|
|
|
|
|
/*
|
|
* FloatingPointError extends ArithmeticError
|
|
*/
|
|
SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
|
|
"Floating point operation failed.");
|
|
|
|
|
|
/*
|
|
* OverflowError extends ArithmeticError
|
|
*/
|
|
SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
|
|
"Result too large to be represented.");
|
|
|
|
|
|
/*
|
|
* ZeroDivisionError extends ArithmeticError
|
|
*/
|
|
SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
|
|
"Second argument to a division or modulo operation was zero.");
|
|
|
|
|
|
/*
|
|
* SystemError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, SystemError,
|
|
"Internal error in the Python interpreter.\n"
|
|
"\n"
|
|
"Please report this to the Python maintainer, along with the traceback,\n"
|
|
"the Python version, and the hardware/OS platform and version.");
|
|
|
|
|
|
/*
|
|
* ReferenceError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, ReferenceError,
|
|
"Weak ref proxy used after referent went away.");
|
|
|
|
|
|
/*
|
|
* MemoryError extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, MemoryError, "Out of memory.");
|
|
|
|
|
|
/* Warning category docstrings */
|
|
|
|
/*
|
|
* Warning extends Exception
|
|
*/
|
|
SimpleExtendsException(PyExc_Exception, Warning,
|
|
"Base class for warning categories.");
|
|
|
|
|
|
/*
|
|
* UserWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, UserWarning,
|
|
"Base class for warnings generated by user code.");
|
|
|
|
|
|
/*
|
|
* DeprecationWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, DeprecationWarning,
|
|
"Base class for warnings about deprecated features.");
|
|
|
|
|
|
/*
|
|
* PendingDeprecationWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
|
|
"Base class for warnings about features which will be deprecated\n"
|
|
"in the future.");
|
|
|
|
|
|
/*
|
|
* SyntaxWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, SyntaxWarning,
|
|
"Base class for warnings about dubious syntax.");
|
|
|
|
|
|
/*
|
|
* RuntimeWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, RuntimeWarning,
|
|
"Base class for warnings about dubious runtime behavior.");
|
|
|
|
|
|
/*
|
|
* FutureWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, FutureWarning,
|
|
"Base class for warnings about constructs that will change semantically\n"
|
|
"in the future.");
|
|
|
|
|
|
/*
|
|
* ImportWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, ImportWarning,
|
|
"Base class for warnings about probable mistakes in module imports");
|
|
|
|
|
|
/*
|
|
* UnicodeWarning extends Warning
|
|
*/
|
|
SimpleExtendsException(PyExc_Warning, UnicodeWarning,
|
|
"Base class for warnings about Unicode related problems, mostly\n"
|
|
"related to conversion problems.");
|
|
|
|
|
|
/* Pre-computed MemoryError instance. Best to create this as early as
|
|
* possible and not wait until a MemoryError is actually raised!
|
|
*/
|
|
PyObject *PyExc_MemoryErrorInst=NULL;
|
|
|
|
#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
|
|
Py_FatalError("exceptions bootstrapping error.");
|
|
|
|
#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
|
|
if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
|
|
Py_FatalError("Module dictionary insertion problem.");
|
|
|
|
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
|
|
/* crt variable checking in VisualStudio .NET 2005 */
|
|
#include <crtdbg.h>
|
|
|
|
static int prevCrtReportMode;
|
|
static _invalid_parameter_handler prevCrtHandler;
|
|
|
|
/* Invalid parameter handler. Sets a ValueError exception */
|
|
static void
|
|
InvalidParameterHandler(
|
|
const wchar_t * expression,
|
|
const wchar_t * function,
|
|
const wchar_t * file,
|
|
unsigned int line,
|
|
uintptr_t pReserved)
|
|
{
|
|
/* Do nothing, allow execution to continue. Usually this
|
|
* means that the CRT will set errno to EINVAL
|
|
*/
|
|
}
|
|
#endif
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
_PyExc_Init(void)
|
|
{
|
|
PyObject *bltinmod, *bdict;
|
|
|
|
PRE_INIT(BaseException)
|
|
PRE_INIT(Exception)
|
|
PRE_INIT(TypeError)
|
|
PRE_INIT(StopIteration)
|
|
PRE_INIT(GeneratorExit)
|
|
PRE_INIT(SystemExit)
|
|
PRE_INIT(KeyboardInterrupt)
|
|
PRE_INIT(ImportError)
|
|
PRE_INIT(EnvironmentError)
|
|
PRE_INIT(IOError)
|
|
PRE_INIT(OSError)
|
|
#ifdef MS_WINDOWS
|
|
PRE_INIT(WindowsError)
|
|
#endif
|
|
#ifdef __VMS
|
|
PRE_INIT(VMSError)
|
|
#endif
|
|
PRE_INIT(EOFError)
|
|
PRE_INIT(RuntimeError)
|
|
PRE_INIT(NotImplementedError)
|
|
PRE_INIT(NameError)
|
|
PRE_INIT(UnboundLocalError)
|
|
PRE_INIT(AttributeError)
|
|
PRE_INIT(SyntaxError)
|
|
PRE_INIT(IndentationError)
|
|
PRE_INIT(TabError)
|
|
PRE_INIT(LookupError)
|
|
PRE_INIT(IndexError)
|
|
PRE_INIT(KeyError)
|
|
PRE_INIT(ValueError)
|
|
PRE_INIT(UnicodeError)
|
|
PRE_INIT(UnicodeEncodeError)
|
|
PRE_INIT(UnicodeDecodeError)
|
|
PRE_INIT(UnicodeTranslateError)
|
|
PRE_INIT(AssertionError)
|
|
PRE_INIT(ArithmeticError)
|
|
PRE_INIT(FloatingPointError)
|
|
PRE_INIT(OverflowError)
|
|
PRE_INIT(ZeroDivisionError)
|
|
PRE_INIT(SystemError)
|
|
PRE_INIT(ReferenceError)
|
|
PRE_INIT(MemoryError)
|
|
PRE_INIT(Warning)
|
|
PRE_INIT(UserWarning)
|
|
PRE_INIT(DeprecationWarning)
|
|
PRE_INIT(PendingDeprecationWarning)
|
|
PRE_INIT(SyntaxWarning)
|
|
PRE_INIT(RuntimeWarning)
|
|
PRE_INIT(FutureWarning)
|
|
PRE_INIT(ImportWarning)
|
|
PRE_INIT(UnicodeWarning)
|
|
|
|
bltinmod = PyImport_ImportModule("__builtin__");
|
|
if (bltinmod == NULL)
|
|
Py_FatalError("exceptions bootstrapping error.");
|
|
bdict = PyModule_GetDict(bltinmod);
|
|
if (bdict == NULL)
|
|
Py_FatalError("exceptions bootstrapping error.");
|
|
|
|
POST_INIT(BaseException)
|
|
POST_INIT(Exception)
|
|
POST_INIT(TypeError)
|
|
POST_INIT(StopIteration)
|
|
POST_INIT(GeneratorExit)
|
|
POST_INIT(SystemExit)
|
|
POST_INIT(KeyboardInterrupt)
|
|
POST_INIT(ImportError)
|
|
POST_INIT(EnvironmentError)
|
|
POST_INIT(IOError)
|
|
POST_INIT(OSError)
|
|
#ifdef MS_WINDOWS
|
|
POST_INIT(WindowsError)
|
|
#endif
|
|
#ifdef __VMS
|
|
POST_INIT(VMSError)
|
|
#endif
|
|
POST_INIT(EOFError)
|
|
POST_INIT(RuntimeError)
|
|
POST_INIT(NotImplementedError)
|
|
POST_INIT(NameError)
|
|
POST_INIT(UnboundLocalError)
|
|
POST_INIT(AttributeError)
|
|
POST_INIT(SyntaxError)
|
|
POST_INIT(IndentationError)
|
|
POST_INIT(TabError)
|
|
POST_INIT(LookupError)
|
|
POST_INIT(IndexError)
|
|
POST_INIT(KeyError)
|
|
POST_INIT(ValueError)
|
|
POST_INIT(UnicodeError)
|
|
POST_INIT(UnicodeEncodeError)
|
|
POST_INIT(UnicodeDecodeError)
|
|
POST_INIT(UnicodeTranslateError)
|
|
POST_INIT(AssertionError)
|
|
POST_INIT(ArithmeticError)
|
|
POST_INIT(FloatingPointError)
|
|
POST_INIT(OverflowError)
|
|
POST_INIT(ZeroDivisionError)
|
|
POST_INIT(SystemError)
|
|
POST_INIT(ReferenceError)
|
|
POST_INIT(MemoryError)
|
|
POST_INIT(Warning)
|
|
POST_INIT(UserWarning)
|
|
POST_INIT(DeprecationWarning)
|
|
POST_INIT(PendingDeprecationWarning)
|
|
POST_INIT(SyntaxWarning)
|
|
POST_INIT(RuntimeWarning)
|
|
POST_INIT(FutureWarning)
|
|
POST_INIT(ImportWarning)
|
|
POST_INIT(UnicodeWarning)
|
|
|
|
PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);
|
|
if (!PyExc_MemoryErrorInst)
|
|
Py_FatalError("Cannot pre-allocate MemoryError instance\n");
|
|
|
|
Py_DECREF(bltinmod);
|
|
|
|
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
|
|
/* Set CRT argument error handler */
|
|
prevCrtHandler = _set_invalid_parameter_handler(InvalidParameterHandler);
|
|
/* turn off assertions in debug mode */
|
|
prevCrtReportMode = _CrtSetReportMode(_CRT_ASSERT, 0);
|
|
#endif
|
|
}
|
|
|
|
void
|
|
_PyExc_Fini(void)
|
|
{
|
|
Py_XDECREF(PyExc_MemoryErrorInst);
|
|
PyExc_MemoryErrorInst = NULL;
|
|
#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
|
|
/* reset CRT error handling */
|
|
_set_invalid_parameter_handler(prevCrtHandler);
|
|
_CrtSetReportMode(_CRT_ASSERT, prevCrtReportMode);
|
|
#endif
|
|
}
|